-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
Ignore unused function warnings from external headers when compiling with GCC and Clang #3235
Conversation
…with GCC and Clang
@ashn-dot-dev I'm merging this review BUT personally I'm not a fan of |
@raysan5 I am also not a huge fan of the pragmas. This PR was made based on some of the information in #2952, specifically:
I think with libs like |
Sorry for reposting, but (considering solving these warnings for any platform) would this be worth trying?
|
Since static bindings are desired in the first place, isn't another way by (1) compiling such a header-only-library into a standalone implementation object file (and not recompiling it unless there is a new source for it). (2) Then use the header-only-library as a header case without implementation in the app, referencing externals instead. (3) Then include that standalone implementation object at link time. There should then be no problem about unused functions and unused functions will not clutter the resulting executable. This is pretty much the pattern for static binding to Maybe this simple (and economical) case could be explained as an alternative to the #pragma approach. I am uncertain that suppressing the warning does anything about the clutter of an app with unused function code, and all the wasted compile-time that goes into creating such a thing. This approach also works well if the app executable is itself built from multiple source-file modules and app-specific headers. The linking of it all will still include functions from the implementation object at most once each. |
I might be misunderstanding your suggestion, but wouldn't separate compilation require the linkage of the definitions to be |
@ubkp RE the |
That's correct. Of course it is also the default for function declarations.
I don't understand what the concern is here. What is your insight about that? The compiled implementation objects are in the possession of the developer. Likewise for the compiled objects of the developer's app. Once linking has been performed there is static binding among the components and with appropriate options, the unused implementations of functions just disappear. The used implementations of functions will be there in compiled form and statically linked. Is there some mistaken notion of privacy or efficiency here? I am ignoring debugging compiles, speaking only of release versions. Although one can export symbols for release builds I presume that would not the case here. And, as far as I know, compiling internal linkage doesn't prevent discovery of the functions used and the symbols used for them in debug builds. PS: I can imagine a case where one might want to have the equivalent of static/internal linking within a linkable object. Making a new library object that employed the header-only privately and not wanting to expose its functions in the distributed linkable object. There might be a name-collision avoidance issue as well. That might take some linker magic. I don't think it is an use case that should have any priority in something like raylib. |
Bingo, this was my thinking. Although I do suppose you are correct in noting that this is a use case that is probably not the highest priority for something like raylib. If a project depending on raylib got to the point where this mattered, then that project would most likely be customizing their build of raylib and additional dependencies anyway. So perhaps separate compilation would be a viable way to go in this case. 👍 |
@ashn-dot-dev @ubkp @orcmid thank you very much for your comments and detailed analysis of this issue and possible approaches. Personally I think the best approach is in the build side ( In any case, it's not a dramatic issue, just some compilation warnings, current @ubkp Your approach is very interesting, good to have a list of the unused functions but I'm afraid the downsides you comment on the PR (#3237) are considerable concerns, not to mention that maybe some of those functions get used on raylib at some point. |
Removes existing unused function warnings generated when compiling functions declared with
static
linkage in external headers. This changeset suppresses warnings with a set of pragmas around external header includes using the pattern:This significantly reduces the warning noise when compiling on Linux, and has been tested with
CC=gcc
andCC=clang
. I do not have a machine with MSVC set up, so I cannot test or confirm the behavior on Windows, but the#if defined(__GNUC__)
guard should only have an effect with GCC and Clang and not produce any "unknown pragma GCC" warnings on Windows.Putting the
#pragma GCC diagnostic push
and#pragma GCC diagnostic pop
code around every external include is a little ugly, but is the simplest way to suppress these warnings for a specific set of includes as far as I can tell.