4

Let's say I have main.cpp, a.h and b.h in the same folder. main.cpp includes a.h, and a.h includes b.h.

Checking the generated depends.make file, main.cpp.o depends both on a.h and b.h, as expected.

If b.h is moved to a folder B, and used the -iquote B flag, main.cpp.o does not depend any more on b.h.

I can put back the dependency by adding include_directories(${CMAKE_SOURCE_DIR}/B) to the CMakeLists.txt file. This however has a side effect: when compiling main.cpp, cmake uses the flag -I/pathTo/B. I prefer using -iquote instead of -I.

Is there a way in this second case to let cmake to detect the dependencies automatically without introducing the -I compiler flag?

1
  • The dependency checking is not done by CMake. The compiler will detect/track/output header dependencies. Just e.g. -MMD flags are added by CMake to the compiler call (see e.g. CMAKE_DEPFILE_FLAGS_CXX). I'm not sure if it would help the generally change CMAKE_INCLUDE_FLAG_CXX (which does define the -I flag). Or long story short: CMake does not support -iquote out-of-the-box. Commented Mar 25, 2017 at 21:39

1 Answer 1

0

You might find this SO answer helpful: https://stackoverflow.com/a/9899919/5820799

It boils down to not use include_directories(${CMAKE_SOURCE_DIR}/B), but to manually add the -iquote directive instead:

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -iquote ${CMAKE_SOURCE_DIR}/B")

This will generate -iquote <path> instead of -I <path>. It is not pretty, but does the job. :-)

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.