我可以在工具链文件中调用add_compile_definitions吗?

发布于 2025-02-05 18:00:05 字数 301 浏览 4 评论 0原文

Cmake提供了诸如cmake_< lang> _flags_init之类的变量,以允许您在工具链文件中设置编译器标志,但我不知道设置预处理程序定义的等效性。实际上,Cmake似乎并没有真正区分编译器标志的预处理器标志。

在工具链文件中设置预处理器宏定义的“正确”或“支持”的方法是什么?调用add_compile_definitions()是否可以接受,我应该将它们包含在cmake_c_flags_init中,还是违反某些CMAKE哲学以使其甚至在工具链文件中设置它们?

CMake provides variables such as CMAKE_<LANG>_FLAGS_INIT to allow you to set compiler flags in a toolchain file, but I don't know of an equivalent for setting preprocessor definitions. In fact, CMake doesn't really seem to distinguish between preprocessor flags an compiler flags.

What is the "correct", or "supported" way to set preprocessor macro definitions in a toolchain file? Is it acceptable to call add_compile_definitions(), should I include them in CMAKE_C_FLAGS_INIT, or does it go against some CMake philosophy to even set them in the toolchain file at all?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夏日浅笑〃 2025-02-12 18:00:05

我可以在工具链文件中调用add_compile_definitions吗?

当然,它等效于string(Append cmake_c_flags

在工具链文件中设置预处理器宏定义的“正确”或“支持”的方法是什么?致电add_compile_definitions()可以接受,我应该将它们包含在cmake_c_flags_init中,还是与某些cmake pholisionphone违反了某些cmake pholisonphy,甚至完全将它们设置在工具链文件中?

最好从工具链文件设置*_ INIT变量,还将其中的任何定义放在其中。在文档中
https://cmake.org/cmake/help/latest/variable/CMAKE_LANG_FLAGS_INIT. html

此变量是由工具链文件设置的

在实践中由工具链文件设置,使用:

string(APPEND CMAKE_${lang}_FLAGS_INIT " your_flag")

cmake的示例:

$ cd /usr/share/cmake/Modules
$ grep _FLAGS_INIT -r .
./Compiler/ARMCC.cmake:  string(APPEND CMAKE_${lang}_FLAGS_INIT " ")
./Compiler/ARMClang.cmake:        string(APPEND CMAKE_${lang}_FLAGS_INIT " -march=${CMAKE_SYSTEM_ARCH}")
./Compiler/ARMClang.cmake:        string(APPEND CMAKE_${lang}_FLAGS_INIT " -mcpu=${CMAKE_SYSTEM_PROCESSOR}")
./Compiler/Absoft-Fortran.cmake:string(APPEND CMAKE_Fortran_FLAGS_INIT " ")
./Compiler/Bruce-C.cmake:string(APPEND CMAKE_C_FLAGS_INIT " -D__CLASSIC_C__")
./Compiler/Fujitsu.cmake:  string(APPEND CMAKE_${lang}_FLAGS_INIT " ")
./Platform/Android-Common.cmake:        string(APPEND CMAKE_${lang}_FLAGS_INIT " -stdlib=libstdc++")
./Platform/Android-Common.cmake:        string(APPEND CMAKE_${lang}_FLAGS_INIT " -stdlib=libc++")
./Platform/Android-Common.cmake:        string(APPEND CMAKE_${lang}_FLAGS_INIT " -stdlib=libc++")
./Platform/ARTOS-GNU-C.cmake:string(APPEND CMAKE_C_FLAGS_INIT " -DARTOS -Xp -+")

....

请参阅上面的bruce-c.cmake上面的示例。

Can I call add_compile_definitions in a toolchain file?

Sure, it's equivalent to string(APPEND CMAKE_C_FLAGS.

What is the "correct", or "supported" way to set preprocessor macro definitions in a toolchain file? Is it acceptable to call add_compile_definitions(), should I include them in CMAKE_C_FLAGS_INIT, or does it go against some CMake philosophy to even set them in the toolchain file at all?

It's best to set *_INIT variables from toolchain file, and also put there any definitions that you want. It's in the doc
https://cmake.org/cmake/help/latest/variable/CMAKE_LANG_FLAGS_INIT.html :

This variable is meant to be set by a toolchain file

In practice, use:

string(APPEND CMAKE_${lang}_FLAGS_INIT " your_flag")

Examples from cmake:

$ cd /usr/share/cmake/Modules
$ grep _FLAGS_INIT -r .
./Compiler/ARMCC.cmake:  string(APPEND CMAKE_${lang}_FLAGS_INIT " ")
./Compiler/ARMClang.cmake:        string(APPEND CMAKE_${lang}_FLAGS_INIT " -march=${CMAKE_SYSTEM_ARCH}")
./Compiler/ARMClang.cmake:        string(APPEND CMAKE_${lang}_FLAGS_INIT " -mcpu=${CMAKE_SYSTEM_PROCESSOR}")
./Compiler/Absoft-Fortran.cmake:string(APPEND CMAKE_Fortran_FLAGS_INIT " ")
./Compiler/Bruce-C.cmake:string(APPEND CMAKE_C_FLAGS_INIT " -D__CLASSIC_C__")
./Compiler/Fujitsu.cmake:  string(APPEND CMAKE_${lang}_FLAGS_INIT " ")
./Platform/Android-Common.cmake:        string(APPEND CMAKE_${lang}_FLAGS_INIT " -stdlib=libstdc++")
./Platform/Android-Common.cmake:        string(APPEND CMAKE_${lang}_FLAGS_INIT " -stdlib=libc++")
./Platform/Android-Common.cmake:        string(APPEND CMAKE_${lang}_FLAGS_INIT " -stdlib=libc++")
./Platform/ARTOS-GNU-C.cmake:string(APPEND CMAKE_C_FLAGS_INIT " -DARTOS -Xp -+")

....

See the Bruce-C.cmake example above.

挽心 2025-02-12 18:00:05

工具链文件通常设置环境和特定目标所需的标志。因此,添加此类目标所需的编译定义是可以接受的。

The toolchain files usually set up the environment and the flags needed for a specific target. So it is acceptable to add compile definitions required for such targets.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文