Reputation: 11
I am having some problems for creating the CMakeList for my project. I have the following structure:
ProjectAB
|
|--- include
| |--- A.h
| |--- B.h
|
| --- CMakeList.txt
Project1
|
|--- include
| |--- src1.h
|
|--- src
| |--- src1.cpp (#include "src1.h"; #include "A.h"; #include "B.h")
|
| --- CMakeList.txt
ProjectAB is just composed by headers, so I have been reading in the CMake guides and some other sources (link) and finally I wrote the following CMakeList for it:
cmake_minimum_required(VERSION 3.6.1)
project(lib_projectAB VERSION 1.0.0 LANGUAGES CXX)
add_library(projectAB INTERFACE)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)
target_include_directories(projectAB
INTERFACE
A.h
B.h
)
Now I want to use the headers of the ProjectAB in the Project1. I have to install them because of unrelated reasons. I am not sure how the CMakeLists for both projects should look like.
I thought of this for the Project1:
cmake_minimum_required(VERSION 3.6.1)
project(lib_project1 VERSION 1.0.0 LANGUAGES CXX)
add_subdirectory(src)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../projectAB/include)
add_library(project1
STATIC
src1.h
src1.cpp
)
target_link_libraries(project1
PUBLIC
third_party_LIBRARIES
lib_projectAB
)
install(
FILES
src1.h
A.h
B.h
DESTINATION
.
)
But it is not elegant and neither working.
How would be the proper way for doing this?
Upvotes: 1
Views: 1608
Reputation: 5668
First, you are marking the wrong directory as the location of include files:
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../projectAB)
As according to your directory tree, these files are located in projectAB/include, not in projectAB directly.
Depending on where the CMakeLists for projectAB is located, you could have a similar problem there.
Also, you aren't using the CMakeLists for projectAB for building project1, but you should specify include directories there, not actual header files - and the simple include_directories call is also unneccessary: by specifing interface include directories for a target, anything depending on that target will add those directories to it's include path.
Another problem is that your project1 doesn't actually depend on projectAB - that's why you try to modify the include path there manually.
Ideally you would would want to follow two possible paths:
Unfortunately, while it's possible to use add_subdirectory
to add a directory which isn't the child of the current directory, it isn't recommended.
So to follow this approach, you would have to either:
add_subdirectory
- which again would mean that project1 would use the projectAB target.Upvotes: 1