CMake:如何为头文件设置COMPILE_FLAGS? [英] CMake: how to set COMPILE_FLAGS for header files?

查看:382
本文介绍了CMake:如何为头文件设置COMPILE_FLAGS?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经成功地使用CMake构建了一个共享库,但是大小并不理想。
我已经尝试了几个编译标志来减小大小,等等。

I've successfully used CMake to built a shared library, but the size is not so good. I've already tried several compile flags to cut size, etc.

set_source_files_properties(${TARGET_SOURCE_FILES} PROPERTIES COMPILE_FLAGS "-fexceptions")

上面的代码用于打开单个文件的异常处理,而-fno-exceptions已添加到CMAKE_CXX_FLAG。

Code above is used to open exception handling for single file while -fno-exceptions is added to CMAKE_CXX_FLAG. It works fine.

但是,当-fno-rtti添加到CMAKE_CXX_FLAGS时,我对* .hpp使用了相同的代码来打开rtti。不幸的是,它没有用。

However, I used the same code to an *.hpp to open rtti for it while -fno-rtti is added to CMAKE_CXX_FLAGS. Unfortunately, it didn't work.

因此,有没有办法将COMPILE_FLAGS添加到CMAKE的头文件中?我已经在官方网站上查看了纪录片,但仍然没有任何线索。

So, is there a way to add COMPILE_FLAGS to header files in CMAKE? I've viewd documentary on offical site, but still no clues.

推荐答案

这是不可能的,可能是一个有问题的事情

This is not possible and probably a questionable thing to do in the first place.

在同一二进制文件中混合不同的编译标志是危险的游戏。通常,您希望目标中的所有编译单元共享相同的编译标志,因为很容易将事情弄得一团糟,否则很糟。但是,如果确实确定要执行的操作,CMake仍然允许您使用源文件属性来执行此操作。

Mixing different compile flags within the same binary is a dangerous game. Usually you would want all compilation units in a target to share the same compile flags, as it is very easy to mess things up subtly but terribly otherwise. However, CMake still allows you to use source file properties the way you did to do this if you are really sure what you are doing.

尽管使用头文件,但是更差。头文件不会自行编译,因此您需要的是所有拉入该头文件的源文件都将继承与该头文件关联的特殊编译选项。但是,由于所有对编译器的调用都是基于源文件进行的,因此这将需要在每次源更改时重新运行CMake,检查所有源文件中的所有包含,并相应地调整编译器选项。希望您现在可以了解为什么CMake不想这样做。

With header files though, things are worse. Headers do not get compiled on their own, so what you are asking for is that all source files that pull in that header will inherit the special compilation options associated with that header. But since all calls to the compiler happen based on source files, this would require to re-run CMake on every source change, check all the includes in all the source files, and adapt the compiler options accordingly. Hopefully you can now see why CMake doesn't want to do this.

您可以做的是按目标指定这些选项。将标题移到接口目标,然后在其中添加相应的接口属性。然后,所有要使用标头的目标都必须将该目标作为依赖项拉入。结果,所有依赖的源文件都会获得特殊的编译标志,而不管它们是否确实包含标头,但这就是构建系统的工作方式:

What you can do though is specify those options on a per-target basis. Move your headers to an interface target and add a corresponding interface property there. Then, all targets that want to use the header will have to pull in that target as a dependency. As a consequence, all depending source files will get the special compile flags, regardless of whether they actually do include the header or not, but that's just how build systems work:

add_library(my_headers INTERFACE)
target_include_directories(my_headers INTERFACE ${PATH_TO_HEADERS})
target_compile_options(my_headers INTERFACE $<$<CXX_COMPILER_ID:GNU>:-fexceptions>)

add_executable(client a.cpp)
target_link_libraries(client PUBLIC my_headers)

在此示例中, a.cpp (以及所有其他 client 来源)现在将使用 -fexceptions 标志编译。

In this example, a.cpp (as well as all other sources of client) will now get compiled with the -fexceptions flag.

这篇关于CMake:如何为头文件设置COMPILE_FLAGS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆