Reputation: 2510
I'm new to CMake and I need help.
I'm trying to develop the following cmaketutorial
project:
.
├── CMakeLists.txt
├── lib
│ ├── CMakeLists.txt
│ ├── lib1
│ │ ├── CMakeLists.txt
│ │ └── lib1.h
│ └── lib2
│ ├── CMakeLists.txt
│ ├── lib2.c
│ └── lib2.h
└── cmaketutorial
├── CMakeLists.txt
└── cmaketutorial.c
lib1
: general purpose library. It only contains a .h and its CMakeFiles.txt allows you to install it in /usr/local/include
.
lib2
: general purpose library. It contains a .c and .h and its CMakeFiles.txt allows you to install it in /usr/local/include
.
lib1 and lib2
: they are two libraries that exist by themselves and are taken from their respective projects.
cmaketutorial
: this is the project in question, it contains a .c
that includes lib1
and lib2
.
CMakeLists.txt (root)
cmake_minimum_required(VERSION 3.12)
project(
cmaketutorial
VERSION 0.0.1
LANGUAGES C
)
add_definitions(-D_GNU_SOURCE)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Werror -pedantic -Wno-unused-parameter")
add_subdirectory(lib)
add_subdirectory(cmaketutorial)
CMakeLists.txt
(root/lib
)
add_subdirectory(lib1)
add_subdirectory(lib2)
CMakeLists.txt
(root/lib/lib1
)
cmake_minimum_required(VERSION 3.12)
project(
lib1
VERSION 0.0.1
LANGUAGES C
)
add_library(${PROJECT_NAME} INTERFACE)
target_include_directories(
lib1
INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}
)
# install rules
install(FILES lib1.h DESTINATION include)
CMakeLists.txt
(root/lib/lib2
)
cmake_minimum_required(VERSION 3.12)
project(
lib2
VERSION 0.0.1
LANGUAGES C
)
if(CMAKE_CURRENT_BINARY_DIR)
set(CMAKE_CURRENT_BINARY_DIR ${CMAKE_CURRENT_SOURCE_DIR})
endif(CMAKE_CURRENT_BINARY_DIR)
add_library(
${PROJECT_NAME}
lib2.c
${CMAKE_CURRENT_BINARY_DIR}/lib2.h
)
target_include_directories(
${PROJECT_NAME}
INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}
PRIVATE ${CMAKE_CURRENT_BINARY_DIR}
)
install(TARGETS ${PROJECT_NAME} DESTINATION lib)
install(FILES lib2.h DESTINATION include)
CMakeLists.txt
(root/cmaketutorial
)
# add the executable
add_executable(cmaketutorial cmaketutorial.c)
target_include_directories(
cmaketutorial PUBLIC
"${PROJECT_BINARY_DIR}"
)
With current CMakeFiles *
when compiling cmaketutorial.c
it cannot link lib1
and lib2
.
How can I modify the general CMakeFiles.txt
so that everything goes as it should?
Can I use the original CMakeFiles.txt
of lib1
and lib2
?
Also, could I set up the project for which includes in cmaketutorial.c
have the form #include <lib/lib1.h>
and #include <lib/lib2.h>
?
Upvotes: 0
Views: 200
Reputation: 141985
How can I modify the general CMakeFiles.txt so that everything goes as it should?
You have to link cmaketutorial
executable with lib1
and lib2
libraries. Add target_link_libraries (cmaketutorial PUBLIC lib1 lib2)
to cmaketutorial/CMakeLists.txt
.
could I set up the project for which includes in cmaketutorial.c have the form #include and #include ?
Move those files into appriopriate directories. So do:
mkdir lib/lib1/lib lib/lib2/lib
mv lib/lib1/lib1.h lib/lib1/lib
mv lib/lib2/lib2.h lib/lib2/lib/lib2.h
and then update lib/lib2/CMakeLists.txt
with add_library(... lib/lib2.h)
and update all the install
rules when needed.
Upvotes: 2