Reputation:
So here's my tree directory :
*common_folder
\
*main1_folder---CMakeLists.txt & file1.c & files2.c
*
*main2_folder
*
*common_librayX---file_libraryX.c (and header)
*
*common_libraryY--file_libraryY.c (and header)
So what I want to do is simulate the following line : gcc -Wall -o MAIN file1.c file2.c file_libraryX.c file_libraryY.c
So here's how I have tried with my CMakeLists.txt :
cmake_minimum_required(VERSION 3.0.0)
#Déclaration du projet
project(MAIN1 VERSION 1.0)
#Include headers
get_filename_component(PARENT_DIR ../ ABSOLUTE)
include_directories(${PARENT_DIR}/commonlibrayX)
include_directories(${PARENT_DIR}/commonlibraryY)
add_executable(
MAIN1
file1.c
file2.c
file_libraryX.c
file_libraryY.c
)
install (TARGETS Project DESTINATION bin)
Of course I end up with the error that it cannot find the files file_libraryX.c and file_libraryy.c.
I have looked up everywhere and I don't seem to understand at all how do you include the libraries (not headers) from different directories than the current one where's CMakeLists.txt. In my case the parent directory.
Also the reason as to why I don't have my CMakeLists.txt at a higher level in the tree directory is because I want to create different binaries in each main folders without having to rewrite the CMakeLists.txt. They happened to share the common libraries.
Could someone please help me ?
Upvotes: 5
Views: 6060
Reputation: 18009
The modern CMake approach would be:
common_folder/CMakeLists.txt
cmake_minimum_required(VERSION 3.0.0)
project(MAIN1 VERSION 1.0)
add_subdirectory(common_librayX)
add_subdirectory(common_librayY)
add_subdirectory(main1_folder)
common_folder/common_librayX/CMakeLists.txt
add_library(LIBX STATIC file_libraryX.c)
target_include_directories(LIBX PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
common_folder/common_librayY/CMakeLists.txt
add_library(LIBY STATIC file_libraryY.c)
target_include_directories(LIBY PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
common_folder/main1_folder/CMakeLists.txt
add_executable(MAIN1 file1.c file2.c)
target_link_libraries(MAIN1 PRIVATE LIBX LIBY)
Building the project:
mkdir build
cd build
cmake ..
cmake --build .
Upvotes: 7