C++ Build / CMake Tricks

Multiple Static -> Static

MSVC: lib.exe
Linux: ar -rcT libgaea.a libgaea_base.a libgaea_config.a
  lipo -create libgaea_base.a libgaea_config.a -output libgaea.a

XMake supports merging static libraries: Ref

Multiple Static -> Shared

There was a CMake snippet which is a cross-platform solution on gist. Unfortunately cannot be found again.

Now CMake provides a generator expression, LINK_LIBRARY, to solve this issue. 🎉

gcc: --whole-archive
Clang: --force_load

Circular dependencies

CMake: target_link_libraries(foobar PRIVATE $<LINK_GROUP:RESCAN,foo,bar>)

Other Problems

  1. CMake will treat all $PATH and their parent paths as system path (e.g. /opt/brew/bin & /opt/brew). This can be problematic while i only use those binary executables in paths, and do not want to use libraries there.

    Well we can use CMAKE_IGNORE_PREFIX_PATH(CMake 3.23+) or CMAKE_IGNORE_PATH to ignore them, or else we may need to make $PATH clean before run cmake configuring.