Reputation: 3996
I use CMake to generate a config.h file. I thought it was better to generate it in the build folder and not the source folder, because it will not be committed in the versioning system.
Because of that, I had to add an include directory to my target, and include my file like <config.h>
instead of "config.h"
.
set(GENERATED_HEADER_DIR ${CMAKE_BINARY_DIR}/geninclude)
execute_process(COMMAND git describe
OUTPUT_VARIABLE version_description
OUTPUT_STRIP_TRAILING_WHITESPACE)
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/src/config.h.in"
"${GENERATED_HEADER_DIR}/config.h")
# ... target configuration
target_include_directories(my_target
PRIVATE ${GENERATED_HEADER_DIR}
PRIVATE ${SOME_LIB_I_NEED})
My problem is: some lib has aslo exposed a config.h file and the both files are messing up each other during the build.
Edit: the project is built to webassembly so apparently it needs to compile everything at once and cannot link to bin libs as it is usually done with C++ projects.
Is there a way to to include my own generated file with ".h"
instead of <.h>
without putting the file into my src dir?
Or it there a way to prioritize the include so my code will include my generated file and the lib includes its own? I don't think I can modify the lib CMake config though...
Upvotes: 0
Views: 498
Reputation: 54589
You should aim to keep your include paths unique, so that this cannot happen.
One way to achieve this is by moving the majority of includes to their own subdirectory. So instead of #include <config.h>
you could use #include <MyProject/config.h>
.
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/src/config.h.in"
"${GENERATED_HEADER_DIR}/MyProject/config.h")
If you end up in a situation where this is not possible, eg. because both of the headers are part of third-party libraries A and B not under your control, you need to solve the problem by allowing each .cpp
file to only make use of either lib A or lib B, but never both. As soon as both libraries are usable by the same source file, you get the conflict on the include files, which is not resolvable. Any clever hackery regarding the order of include directories on the build command line or #include <>
vs. include ""
is extremely fragile and non-portable.
Upvotes: 1