How to change compilation flags for MyFramework_ve

2019-07-17 05:00发布

问题:

With Apple Generic Versioning enabled, Xcode autogenerates a MyFramework_vers.c file in the DERIVED_SOURCES_DIR, which contains the version string and number defined as const unsigned char[] and const double.

However, with -Wmissing-variable-declarations enabled (part of -Weverything), this produces the warnings

no previous extern declaration for non-static variable 'MyFrameworkVersionString'
no previous extern declaration for non-static variable 'MyFrameworkVersionNumber'

It seems the possible solutions are:

  • add -Wno-missing-variable-declarations to the cflags for this file
  • add extern declarations above the variable definitions
  • add a #import that pulls in the extern declarations from the umbrella header

But I can't figure out how to do any of these since the file lives in DerivedSources and isn't a member of the Compile Sources phase. What am I missing?

(I found the VERSION_INFO_EXPORT_DECL setting which would allow me to mark the variables extern, but then I get the "extern variable has an initializer" warning, from -Wextern-initializer, so that doesn't help.)

回答1:

I don't know of a way to modify the compiler arguments for this file, but it's possible to address the warning by abusing VERSION_INFO_EXPORT_DECL. Set the value to a literal newline followed by #import "HeaderWithExternDeclarations.h" and another literal newline. In the pbxproj it should look something like this:

VERSION_INFO_EXPORT_DECL = "\n#import \"MyFramework.h\"\n";

If you want to import a header containing Objective-C you'll also need to change VERSION_INFO_FILE to a value with a .m extension such as MyFramework_vers.m so the generated file is compiled as Objective-C source.

Alternatively, you can use the same hack to insert a pragma to disable the warning:

VERSION_INFO_EXPORT_DECL = "\n#pragma clang diagnostic ignored \"-Wmissing-variable-declarations\"\n";