Vincent
Vincent

Reputation: 60341

CMake : link a produced library?

I have a huge project with 2 main directories : - /myproject/src - /myproject/app

The strategy is that src produces libraries in the /myproject/lib directory and then apps use these libraries to produce executables in /myproject/bin directory.

But the problem is the following. The classic stategy to link libraries is to use the FIND_LIBRARY(). But how to link a library that is not already produced ?

Thank you.

Upvotes: 0

Views: 692

Answers (2)

PANKAJ LADE
PANKAJ LADE

Reputation: 37

In CMake, There is add_subdirectory() command which you can use to include the build of subdirectories in your project. Using this way, you can confirm that the libraries are built before the executables that depend on them. Here's a basic example of how you might structure your project:

Assuming your project structure looks like this:

myproject/
|-- CMakeLists.txt
|-- src/
|   |-- CMakeLists.txt
|   |-- lib1/
|   |   |-- source files for lib1
|   |-- lib2/
|   |   |-- source files for lib2
|-- app/
|   |-- CMakeLists.txt
|   |-- app1/
|   |   |-- source files for app1
|   |-- app2/
|   |   |-- source files for app2
|-- lib/
|-- bin/

Here is how you can set up your CMakeLists.txt files:

Root level CMakeLists.txt(myproject/CMakeLists.txt)

cmake_minimum_required(VERSION 3.12)
project(MyProject)

# Add the source files directory for src
add_subdirectory(src)

# Add the source files directory for app
add_subdirectory(app)

src/CMakeLists.txt

# Add 1st library lib1
add_library(lib1 STATIC lib1/source_files.cpp)
target_include_directories(lib1 PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/lib1)

# Add 2nd library lib2
add_library(lib2 STATIC lib2/source_files.cpp)
target_include_directories(lib2 PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/lib2)

# Mention the output directory for libraries 
set(LIB_OUTPUT_PATH ${CMAKE_CURRENT_SOURCE_DIR}/../lib)

app/CMakeLists.txt

# Add executable name app1
add_executable(app1 app1/source_files.cpp)

# Link application app1 with librairies lib1 and lib2
target_link_libraries(app1 PRIVATE lib1 lib2)

# Mention the output directory for executables
set(EXEC_OUTPUT_PATH ${CMAKE_CURRENT_SOURCE_DIR}/../bin)

# Add executable app2
add_executable(app2 app2/source_files.cpp)

# Link application app2 with lib1 and lib2
target_link_libraries(app2 PRIVATE lib1 lib2)

This structure confirms that libraries lib1 and lib2 are built before the executables that depend on them. The output files are placed in the lib and bin directories, respectively.

Upvotes: 0

Tobias Schlegel
Tobias Schlegel

Reputation: 3970

find_library() is a little more than the name suggests, it not only finds the path to a (preinstalled) lib, but often also prepares a lot of variables and functions. Also find_library() only works with specific library modules, which reside in the cmake/share directory.

When you build your own library you have to add it to the CMakeLists.txt with the add_library() command, which works exactly like the add_executable() command.

When you have done that, you can actually add the library to the executable using the target_link_libraries() command.

To sum it up:

add_library(myLib libsourceA.c libSourceB.c)

add_executable(myProgram prgsourceA.c prgsourceB.c)
target_link_libraries(myProgram myLib)

You actually don't have to know or specify the exact location of the library-file, cmake will manage that for you.

Upvotes: 3

Related Questions