Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to compile with gcc 4.4.7 #126

Closed
carlosabalde opened this issue Mar 17, 2017 · 11 comments
Closed

Unable to compile with gcc 4.4.7 #126

carlosabalde opened this issue Mar 17, 2017 · 11 comments
Labels

Comments

@carlosabalde
Copy link

It seems gcc 4.4.7 (CentOS 6.6) does not support diagnostic inside functions:

cJSON.c: In function 'cJSON_AddItemToObjectCS':
cJSON.c:1594: error: #pragma GCC diagnostic not allowed inside functions
cJSON.c:1595: error: #pragma GCC diagnostic not allowed inside functions
cJSON.c:1597: error: #pragma GCC diagnostic not allowed inside functions

Not 100% sure why -Wcast-qual is being ignored here, but I think one possible solution would be:

+#if defined (__clang__) || ((__GNUC__)  && ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ > 5))))
 #pragma GCC diagnostic push
 #pragma GCC diagnostic ignored "-Wcast-qual"
+#endif
     item->string = (char*)string;
+#if defined (__clang__) || ((__GNUC__)  && ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ > 5))))
 #pragma GCC diagnostic pop
+#endif
@FSMaxB
Copy link
Collaborator

FSMaxB commented Mar 17, 2017

-Wcast-qual is ignored because const ist casted away in that line of code. See #83, #82 and the discussions in #84.

It is kind of hard to get these old compiler versions working in oder to check this. Can you check if it works when the pragma are moved before and after the function, that way it might be possible to avoid the GCC version check.

@FSMaxB
Copy link
Collaborator

FSMaxB commented Mar 17, 2017

I just checked with gcc 4.1.2 in CentOS 5 and it works, so older versions of GCC seem to just ignore the unknown pragma.

Therefore if putting the pragma in front of and after the cJSON_AddItemToObjectCS works with gcc 4.4.7, let's just do that.

@carlosabalde
Copy link
Author

I've tried that in 4.4.7 and now I get a warning:

cJSON.c:1583: warning: expected [error|warning|ignored] after ‘#pragma GCC diagnostic’
cJSON.c:1600: warning: expected [error|warning|ignored] after ‘#pragma GCC diagnostic’

Not ideal, but good enough I guess.

@FSMaxB
Copy link
Collaborator

FSMaxB commented Mar 17, 2017

That's still problematic with -Werror. I will try to get my hands on older compiler versions and check what I can do. Because just disabling the pragma will make it break again with -Wcast-qual

@carlosabalde
Copy link
Author

Not sure why disabling the pragma would make it break with -Wcast-qual. I've successfully compiled cJSON with -Wall -Werror using the GCC version check in CentOS 6.6, CentOS 7.2 and Ubuntu 14.04.

@FSMaxB
Copy link
Collaborator

FSMaxB commented Mar 17, 2017

But not with -Wall -Werror -Wcast-qual I guess.

The build system automatically sets these and other compiler flags if supported.

@FSMaxB FSMaxB added the bug label Mar 17, 2017
@carlosabalde
Copy link
Author

You're right:

cJSON.c: In function ‘cJSON_AddItemToObjectCS’:
cJSON.c:1598: error: cast discards qualifiers from pointer target type

@danomimanchego123
Copy link

danomimanchego123 commented Mar 18, 2017

Now that there is an ENABLE_CUSTOM_COMPILER_FLAGS in CMakeLists.txt, perhaps a

-DENABLE_CUSTOM_COMPILER_FLAGS

can be added when it is set, and the pragmas then encased in an #ifndef ENABLE_CUSTOM_COMPILER_FLAGS test. I.e., if you set custom flags, then you can leave out -Wcast-qual, so no need for the pragma.

@FSMaxB
Copy link
Collaborator

FSMaxB commented Mar 18, 2017

I now use the version check, but just around #pragma GCC diagnostic push, leaving the other diagnostic lines intact. Also moving the pragmas outside of the function.

I checked that it compiles with -Werror -Wcast-qual with gcc 4.3, 4.4, 4.5, 4.7, 4.8 and 4.9, but I didn't succeed in compiling gcc 4.6 yet, so I couldn't test with that yet. gcc 4.6 is the most interesting, because that is where the pop and push pragmas where supposedly introduced.

@FSMaxB
Copy link
Collaborator

FSMaxB commented Mar 18, 2017

You can take a look at it on the old-gcc branch.

@FSMaxB
Copy link
Collaborator

FSMaxB commented Mar 19, 2017

This is now fixed in cJSON 1.4.3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants