wiki:CompilerEnvironmentVariables

Command Line Options

The behavior of compilers are primarily controlled with switches. For example, -I${prefix}/include and -L${prefix}/lib tell the C preprocessor and linker to look in the MacPorts prefix for header files and libraries respectively. However, changing compiler behavior with switches can cause problems.

  • Order matters.
    • -I${prefix}/include -I${worksrcpath}/include and -I${worksrcpath}/include -I${prefix}/include produce different results if different header files with the same name are in both location.
    • Such errors can be subtle and difficult to track down, especially when the header files are from different versions of the same packages (e.g. during the upgrade process).
    • The MacPorts construct conflicts_build can be used to prevent the conflicting header files from being simultaneously installed, but this not ideal.
    • Using -isystem instead of -I can more precisely control search order, but this also causes problems.
    • The search order for libraries has similar problems, but it is more difficult to precisely control library search order (there is just -L or using the full library path name).
    • The MacPorts buildbot is careful to build a port only with the minimal set of dependencies active, which prevents most search order issues.
  • Switches can be “baked in” the installed files.
    • Some packages record the history of the switches they were built with.
    • Some packages use the same switches they were built with to build other packages.
    • At best, this is unnecessary noise. At worst, it causes build problems that can be difficult to diagnose.
    • The switches -isysroot and -Wl,-syslibroot are particularly problematic.

Environment Variables

To a limited extent, the behavior of compilers can also be controlled with environment variables instead of switches.

Switch Variable Affect Notes
-I CPATH treated as a delimited list of paths to be added to the default system include path list
-isystem C_INCLUDE_PATH, OBJC_INCLUDE_PATH, CPLUS_INCLUDE_PATH, OBJCPLUS_INCLUDE_PATH like CPATH except only used when processing the appropriate language
-L LIBRARY_PATH tries the directories specified when searching for special linker files
-mmacosx-version-min MACOSX_DEPLOYMENT_TARGET the default deployment target
-isysroot, -Wl,-syslibroot SDKROOT specifies the SDK
DEVELOPER_DIR tells xcrun where to search for development tools and properties
CC_PRINT_OPTIONS like -v but logs the commands interferes with-v
CC_PRINT_OPTIONS_FILE the file to log CC_PRINT_OPTIONS output to

Using environment variables can alleviate some of the problems with switches.

  • They are less likely to be “baked in” the installed files.
  • Directories in CPATH and LIBRARY_PATH are searched after those specified by the command line options.

However, an environment variable is no panacea.

Last modified 10 months ago Last modified on May 12, 2020, 1:39:28 AM