首页 > 解决方案 > 如何强制在cmake中包含标头?

问题描述

我正在将项目从 Makefile 移植到 cmake。Makefile 中有一个 CFLAG/CXXFLAG -include "SystemTest.h"。我试图将它直接添加到我的工具链文件中CMAKE_C_FLAGSCMAKE_CXX_FLAGS但随后测试编译失败,因为它找不到SystemTest.h.

我搜索并找到了这样的答案:我可以强制 cmake 将我的头文件包含在特定目标中吗?并且cmake 在每个看起来很有希望但对我不起作用的源文件中包含头文件。

这是我使用 add_executable 的行。注释代码是我已经尝试过的:

add_executable(my_target ${SRC})
if (WIN32)
  target_link_libraries(my_target PRIVATE module_a module_b module_c module_d module_e module_f)
elseif(UNIX)
  target_link_libraries(my_target PRIVATE module_a module_b module_c module_d module_e module_f ${CURSES_LIBRARIES})
else(WIN32)
  message(FATAL_ERROR "Not supported OS.")
endif(WIN32)

# set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -include SystemTest.h")
# set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -include SystemTest.h")

# add_definitions(-include SystemTest.h)

# target_compile_definitions(cherrySim_runner
#   PRIVATE $<$<COMPILE_LANGUAGE:C>:-include SystemTest.h>
#           $<$<COMPILE_LANGUAGE:CXX>:-include SystemTest.h>
# )

# target_compile_definitions(cherrySim_runner "-include SystemTest.h")

CMake/gcc 在编译文件时module_b抱怨应该来自SystemTest.h.

标签: c++cmake

解决方案


推荐阅读