3

I am porting project from Makefile to cmake. There is a CFLAG/CXXFLAG in Makefile -include "SystemTest.h". I tried to add this to my toolchain file directly to CMAKE_C_FLAGS and CMAKE_CXX_FLAGS but then test compilation fails because it cannot find SystemTest.h.

I searched and found answers like these: Can I force cmake to include my header file in specific targets? and cmake include header into every source file which looked promising but didn't work for me.

Here are the lines where I use add_executable. Commented code is what I already tried:

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 fails when compiling files from module_b complaining about stuff that should come from SystemTest.h.

Michał
  • 691
  • 1
  • 5
  • 22
  • 3
    When you use `-include SystemTest.h` the compiler adds `#include "SystemTest.h"` to the top of every source file. Since it's included using quotes instead of angle-brackets `<>`, the file must be either in the same directory as the source file, or in the standard include directory search path. You can of course provide a full path to the header file, which you might want to try first. – Some programmer dude Mar 27 '19 at 10:08
  • The problem is `SystemTest.h` has more dependencies. But it might be a way for a workaround. – Michał Mar 27 '19 at 10:28
  • 1
    You should add the directory where `SystemTest.h` is located with `target_include_directories` together with your `CMAKE_C/CXX_FLAGS -include SystemTest.h`. – super Mar 27 '19 at 10:49
  • 1
    Can you elaborate on why the solutions in the questions you linked are not suitable for you? – starball Jan 19 '23 at 02:18

0 Answers0