替换编译时宏为环境变量

huangapple go评论67阅读模式
英文:

Substitute compile time macro from environment variable

问题

如何在编译时从环境变量设置编译器宏,即在运行make时(而不是生成时),等效的命令行是:

g++ main.cpp -DFOO="${FOO}"

使用方法如下:

  1. 运行cmake(仅一次)。
  2. 设置环境变量。
  3. 运行make并构建一个使用环境变量值的二进制文件。
  4. 更改环境变量并重新构建。新的二进制文件应使用环境变量的新值。
英文:

How to set compiler macro from environment variable during compile time, i.e. when make is run (not when it generated), equivalent line to

g++ main.cpp -DFOO="${FOO}"

The usage is something like this:

  1. Run cmake (only once).
  2. Set the environment variable.
  3. Run make and build a binary that uses the environment variable's value.
  4. Change the environment variable and rebuild. New binary should use new value of the environment variable.

答案1

得分: 3

你可以尝试使用(CMAKE_)<LANG>_COMPILER_LAUNCHER来实现这个目的,但我强烈建议不要这样做,因为生成的项目无法确定是否需要重新编译源文件。而且,如果涉及到环境变量,重新构建项目以使用上次的配置并不是一件简单的事情。此外,在使用环境变量时,基本上无法仅构建项目的部分部分;很难确定项目的哪个部分是使用给定环境变量值编译的。

相反,我建议添加一个CMake缓存变量,通过简单重新配置项目就可以调整它,这将导致CMake进行必要的更新,以确保重新编译适用于预处理器定义的文件。

...

set(MY_EXE_FOO "bar" CACHE STRING "The value to use for the FOO preprocessor define for my_exe")

add_executable(my_exe main.cpp)
target_compile_definitions(my_exe PRIVATE "FOO=${MY_EXE_FOO}")

用法:

# 使用默认设置(您可以立即传递 -D MY_EXE_FOO=... 来更改默认设置)
cmake -S source_dir -B build_dir
cmake --build build_dir

# 重新配置CMake项目,将定义从 bar 更改为 baz
cmake -D MY_EXE_FOO=baz build_dir
# 使用新配置重新构建
cmake --build build_dir

注意:您甚至可以并行设置多个构建目录用于项目。(假设没有任何CMake逻辑生成文件到源树中。)

cmake -D MY_EXE_FOO=bar -S source_dir -B build_dir_bar
cmake -D MY_EXE_FOO=baz -S source_dir -B build_dir_baz
cmake --build build_dir_bar
cmake --build build_dir_baz
英文:

You may be able to use (CMAKE_)<LANG>_COMPILER_LAUNCHER for this purpose, but I strongly advise against doing this, since the generated project won't be able to tell that recompilation of your sources is required. Furthermore if a environment variable is involved, it's not trivial to rebuild the project with the last configuration. Furthermore building only parts of the project is made basically impossible when using the environment variable; it's difficult to tell, which part of a project was compiled with a given value of a environment variable.

Instead I recommend adding a cmake cache variable that could be adjusted by simply reconfiguring the project which does result in cmake making the necessary updates to ensure a recompilation of the files the preprocessor define applies to.

...

set(MY_EXE_FOO "bar" CACHE STRING "The value to use for the FOO preprocessor define for my_exe")

add_executable(my_exe main.cpp)
target_compile_definitions(my_exe PRIVATE "FOO=${MY_EXE_FOO}")

Usage:

# set up with default (you could pass -D MY_EXE_FOO=... immediately to change the default)
cmake -S source_dir -B build_dir
cmake --build build_dir

# reconfigure the cmake project changing the define from bar to baz
cmake -D MY_EXE_FOO=baz build_dir
# rebuild with the new configuration
cmake --build build_dir

Note: You're even able to set up multiple build dirs for your project in parallel. (This assumes no files are generated to the source tree by any of the cmake logic involved.)

cmake -D MY_EXE_FOO=bar -S source_dir -B build_dir_bar
cmake -D MY_EXE_FOO=baz -S source_dir -B build_dir_baz
cmake --build build_dir_bar
cmake --build build_dir_baz

huangapple
  • 本文由 发表于 2023年5月10日 19:30:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76217877.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定