在为 CMake 预编译头生成的源文件上应用“set_property”,无需硬编码 PCH 路径

发布于 2025-01-10 18:48:42 字数 760 浏览 0 评论 0原文

我正在使用 CMake 的 target_precompile_headers从我正在参与的项目中的 PCH.hpp 文件生成预编译头。

我需要确保生成的源文件与项目中的其他源文件具有完全相同的编译和警告标志,以避免 -Winvalid-pch 问题。

目前,我正在这样做:

target_precompile_headers(sfml-system PRIVATE "${CMAKE_SOURCE_DIR}/src/SFML/PCH.hpp")

set_property(SOURCE "${CMAKE_BINARY_DIR}/src/SFML/System/CMakeFiles/sfml-system.dir/cmake_pch.hxx.cxx" 
             APPEND_STRING PROPERTY COMPILE_FLAGS " ${WARNINGS}")

但是,必须对生成的 cmake_pch.hxx.cxx 文件的路径进行硬编码似乎很脆弱,可能不可移植,并且是维护负担。

有什么方法可以检索 cmake_pch.hxx.cxx 文件的路径而不对其进行硬编码吗?

I am using CMake's target_precompile_headers to generate a precompiled header from a PCH.hpp file in a project I'm contributing to.

I need to ensure that the resultant generated source file has the exact same compilation and warning flags as the other source files in my project, to avoid -Winvalid-pch issues.

At the moment, I am doing this:

target_precompile_headers(sfml-system PRIVATE "${CMAKE_SOURCE_DIR}/src/SFML/PCH.hpp")

set_property(SOURCE "${CMAKE_BINARY_DIR}/src/SFML/System/CMakeFiles/sfml-system.dir/cmake_pch.hxx.cxx" 
             APPEND_STRING PROPERTY COMPILE_FLAGS " ${WARNINGS}")

However, having to hardcode the path to the generated cmake_pch.hxx.cxx file seems brittle, potentially non-portable, and a maintenance burden.

Is there any way I can retrieve the path to the cmake_pch.hxx.cxx file without hardcoding it?

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

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

发布评论

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

评论(1

情绪失控 2025-01-17 18:48:42

据我所知,与 sfml-system 的目标参数关联的标志已经应用于 PCH。这是我的重现器:

cmake_minimum_required(VERSION 3.22)
project(test)

add_executable(test main.cpp)
target_precompile_headers(test PRIVATE "PCH.hpp")

# This is just to probe CMake's behavior. Don't actually
# add defines this way!!
target_compile_options(test PRIVATE "-DVIA_TCO")

然后我创建空的 main.cppPCH.hpp 文件并进行构建的试运行:

$ cmake -G Ninja -S . -B build -DCMAKE_CXX_FLAGS="-DVIA_FLAGS"
[1/3] /usr/bin/c++   -DVIA_FLAGS -DVIA_TCO -Winvalid-pch -x c++-header -include /home/alex/test/build/CMakeFiles/test.dir/cmake_pch.hxx -MD -MT CMakeFiles/test.dir/cmake_pch.hxx.gch -MF CMakeFiles/test.dir/cmake_pch.hxx.gch.d -o CMakeFiles/test.dir/cmake_pch.hxx.gch -c /home/alex/test/build/CMakeFiles/test.dir/cmake_pch.hxx.cxx
[2/3] /usr/bin/c++   -DVIA_FLAGS -DVIA_TCO -Winvalid-pch -include /home/alex/test/build/CMakeFiles/test.dir/cmake_pch.hxx -MD -MT CMakeFiles/test.dir/main.cpp.o -MF CMakeFiles/test.dir/main.cpp.o.d -o CMakeFiles/test.dir/main.cpp.o -c /home/alex/test/main.cpp
[3/3] : && /usr/bin/c++ -DVIA_FLAGS  CMakeFiles/test.dir/main.cpp.o -o test   && :

如您所见,两个标志都来自 < code>target_compile_options 和 CMAKE_CXX_FLAGS (在命令行)应用于 PCH。

所以到目前为止:

我需要确保生成的源文件与项目中的其他源文件具有完全相同的编译和警告标志,以避免 -Winvalid-pch 问题。

任何添加标志的合理方式都应该自动起作用。

As far as I can tell, the flags associated with the target argument to sfml-system are already applied to the PCH. Here's my reproducer:

cmake_minimum_required(VERSION 3.22)
project(test)

add_executable(test main.cpp)
target_precompile_headers(test PRIVATE "PCH.hpp")

# This is just to probe CMake's behavior. Don't actually
# add defines this way!!
target_compile_options(test PRIVATE "-DVIA_TCO")

Then I create empty main.cpp and PCH.hpp files and do a dry-run of the build:

$ cmake -G Ninja -S . -B build -DCMAKE_CXX_FLAGS="-DVIA_FLAGS"
[1/3] /usr/bin/c++   -DVIA_FLAGS -DVIA_TCO -Winvalid-pch -x c++-header -include /home/alex/test/build/CMakeFiles/test.dir/cmake_pch.hxx -MD -MT CMakeFiles/test.dir/cmake_pch.hxx.gch -MF CMakeFiles/test.dir/cmake_pch.hxx.gch.d -o CMakeFiles/test.dir/cmake_pch.hxx.gch -c /home/alex/test/build/CMakeFiles/test.dir/cmake_pch.hxx.cxx
[2/3] /usr/bin/c++   -DVIA_FLAGS -DVIA_TCO -Winvalid-pch -include /home/alex/test/build/CMakeFiles/test.dir/cmake_pch.hxx -MD -MT CMakeFiles/test.dir/main.cpp.o -MF CMakeFiles/test.dir/main.cpp.o.d -o CMakeFiles/test.dir/main.cpp.o -c /home/alex/test/main.cpp
[3/3] : && /usr/bin/c++ -DVIA_FLAGS  CMakeFiles/test.dir/main.cpp.o -o test   && :

As you can see, both the flags from target_compile_options and CMAKE_CXX_FLAGS (at the command line) applied to the PCH.

So to this point:

I need to ensure that the resultant generated source file has the exact same compilation and warning flags as the other source files in my project, to avoid -Winvalid-pch issues.

Any halfway reasonable way of adding flags should work automatically.

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