How CMake automatically detects header dependencies
When I run cmake . -B build
it creates the following make target in ./build/CMakeFiles/main.dir/build.make
CMakeFiles/main.dir/main.cpp.o: CMakeFiles/main.dir/compiler_depend.ts
@$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --green --progress-dir=/home/nikolay/Cpp/Train/Cppref/build/CMakeFiles --progress-num=$(CMAKE_PROGRESS_1) "Building CXX object CMakeFiles/main.dir/main.cpp.o"
/usr/bin/c++ $(CXX_DEFINES) $(CXX_INCLUDES) $(CXX_FLAGS) -MD -MT CMakeFiles/main.dir/main.cpp.o -MF CMakeFiles/main.dir/main.cpp.o.d -o CMakeFiles/main.dir/main.cpp.o -c /home/nikolay/Cpp/Train/Cppref/main.cpp
Pay attention to the -MD compiler option, as
it is used to dump dependencies visible to the preprocessor.
So after the first build it will create ./build/CMakeFiles/main.dir/main.cpp.o.d
with the following content
CMakeFiles/main.dir/main.cpp.o: /home/nikolay/Cpp/Train/Cppref/main.cpp \
/home/nikolay/Cpp/Train/Cppref/header.h
So whenever you change header.h
, the target main.o
will be rebuilt.
CMAKE auto header file dependency
As mentioned in my comment, I have tried out your example and things were working fine: if main.h
was modified then main.c
would be recompiled.
My installation of CMake (version 2.8.0) told me to add
cmake_minimum_required(VERSION 2.8)
to the CMakeLists.txt
file, but that is all of the adjustments I needed.
CMake get dependent header files like gcc -M
Check the build folder for your CMake project. For each target, CMake should generate a file called C.includecache
. This file contains the include dependency information.
If, for example, you have a main.c
file which is including the stdio.h
and math.h
headers. The C.includecache
file will contain an entry like:
/path/to/main.c
stdio.h
-
math.h
-
CMake dependency on defined (preprocessor) header file
You can add an explicit dependency to a source file by setting the OBJECT_DEPENDS
property:
set_property(SOURCE source.cpp APPEND PROPERTY OBJECT_DEPENDS "somefile.h")
You'll have to do this for any source file that includes your configuration file.
CMake - dependencies (headers) between apps/libraries in same project
Here's one possible solution:
Root CMakeLists.txt:
cmake_minimum_required(VERSION 2.8 FATAL_ERROR)
project(${PROJECT_NAME})
add_subdirectory(lib1)
add_subdirectory(lib2)
add_subdirectory(app)
lib1/CMakeLists.txt:
project(Lib1)
add_library(lib1 lib1.cpp lib1.h)
lib2/CMakeLists.txt:
project(Lib2)
add_library(lib2 lib2.cpp lib2.h)
# Add /lib1 to #include search path
include_directories(${Lib1_SOURCE_DIR})
# Specify lib2's dependency on lib1
target_link_libraries(lib2 lib1)
app/CMakeLists.txt:
project(App)
add_executable(app main.cpp some_header.h)
# Add /lib1 and /lib2 to #include search path
include_directories(${Lib1_SOURCE_DIR} ${Lib2_SOURCE_DIR})
# Specify app's dependency on lib2.
# lib2's dependency on lib1 is automatically added.
target_link_libraries(app lib2)
There are plenty of different ways to achieve the same end result here. For a relatively small project, I'd probably just use a single CMakeLists.txt:
cmake_minimum_required(VERSION 2.8 FATAL_ERROR)
project(Test)
add_library(lib1 lib1/lib1.cpp lib1/lib1.h)
add_library(lib2 lib2/lib2.cpp lib2/lib2.h)
add_executable(app app/main.cpp app/some_header.h)
include_directories(${CMAKE_SOURCE_DIR}/lib1 ${CMAKE_SOURCE_DIR}/lib2)
target_link_libraries(lib2 lib1)
target_link_libraries(app lib2)
For further info on the relevant commands and their rationale, run:
cmake --help-command add_subdirectory
cmake --help-command include_directories
cmake --help-command target_link_libraries
CMake header only dependency
Alright, i removed catch and just played around with mingw and apparently i get the same error just by using std::string
. Someone said that it has to do with missing DLL files. I ran dependency walker on the executable and indeed a bunch of DLLs was missing. I didn't now what to do or where to get these so i ditched mingw and tried the cygwin approach.
But using cmake with cygwin i did not find any compatable generators for my development environment (windows).
I then switched to generating a visual studio project instead (which i was avoiding from the start because i did not want to develop in an IDE). But i found out that i can use msbuild to build the executable from the generated visual studio project and it works like a charm, with catch.
Related Topics
How to Bake Credential into Docker Image for Git
String Comparison Not Working Properly
Qt Creator: Add Qt Module to Project
How to Activate Programs on Windows from Linux MAChine
Why Does Perf Show That Sleep Takes All Cores
Number of Processors/Cores in Command Line
What Is the Linux Process Table? What Does It Consist Of
Ubuntu "No Space Left on Device" But There Is Tons of Space
Creating PDF Invoices - Are There Any Templating Solutions
Linux: Writes Are Split into 512K Chunks
In Bash, Why 'X=100 Echo $X' Doesn't Print Anything
Cmake Auto Header File Dependency
Cuda Compiler Not Working with Gcc 4.5 +
Embedding the Password in the Bash Script
How to Set Up Curl to Permanently Use a Proxy