How to change compilation flags for MyFramework_vers.c in Xcode?

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

  • xcode project-/target-settings-syntax for linker flag force_load on iPhone
  • clang: error: linker command failed with exit code 1 - library not found
  • libiconv not linking to iOS project
  • macOS 'wchar.h' File Not Found
  • Install xgboost on Mac - ld: library not found
  • Ignore all warnings in a specific file using LLVM/Clang
  • 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.)

    Solutions Collect From Internet About “How to change compilation flags for MyFramework_vers.c in Xcode?”

    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";