Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add prebuilt static library in project using CMake?

Clion: how add or (use) prebuilt static library in my Project?

like image 420
ilw Avatar asked Mar 31 '15 11:03

ilw


4 Answers

You're probably asking about how to link your project to the pre-built static library. If so, you can do like this by calling target_link_libraries.

Assume your project called myProj and the pre-built library myLib.lib, you can do like this:

target_link_libraries(myProj myLib)
like image 74
herohuyongtao Avatar answered Nov 12 '22 20:11

herohuyongtao


I had great difficulty making this work as I was completely new to CLion and CMake.

In my scenario I was taking a class that required us to use the course library in every project.

Assuming you have a library called libClassLibrary.a, do the following in the CMakeLists.txt at the project root:

First, find the library's location:

find_library(LIB_TO_INCLUDE ClassLibrary /path/to/your/library)

LIB_TO_INCLUDE will contain the location of the library assuming it is found. Note that hardcoding the path could be problematic if you'd like your solution to be portable to other systems. You can add additional search paths separated by a space if the library could exist in multiple locations. A typical example is to include common install locations such as /usr/bin /usr/local/bin etc.

Next, ensure that header files (if applicable) are included in the header search paths:

find_path (LIB_INCLUDES ClassLibrary.h /path/to/header/files)

Again, include multiple search paths if the headers could be loaded in multiple locations. If there is more than one header file, you'll need to include all of them.

Now, include the directories using the include_directories command:

include_directories(${LIB_INCLUDES})

The above will instruct the build system to search all directories contained in LIB_INCLUDES or whatever you decide to call it.

Finally, add the executable and use the target_link_libraries command to link the libClassLibrary.a.

add_executable(MyExecutable main.cpp)
target_link_libraries(MyExecutable ${LIB_TO_INCLUDE})

That's all. You'll notice that under "External Libraries" > "Header Search Paths" in the Project organizer window the directories containing your header files appears.

PS - The book Mastering CMake by Ken Martin and Bill Hoffmann is an invaluable resource.

like image 43
ajw170 Avatar answered Nov 12 '22 21:11

ajw170


I've linked my static lib test.a with the related header files as following:

Project
├── main.cpp
├── CmakeLists.txt
├── libs
│   ├── includes
│   │   ├── *.h
│   ├── lib
│   │   ├── test.a

I've added this to the ./CMakeLists.txt:

include_directories(${CMAKE_SOURCE_DIR}/libs/include)
find_library(Test_LIB test "${CMAKE_SOURCE_DIR}/libs/lib")
add_executable(TestApp main.cpp)
target_link_libraries(TestApp ${Test_LIB})

By adding message(${Test_LIB}), you can print out and check the path.

like image 3
Manuel Schmitzberger Avatar answered Nov 12 '22 19:11

Manuel Schmitzberger


Your question is unrelated to CLion, it is pure CMake. Modify the CMakeLists.txt from your project and use add_library. The CMake documentation might be helpful.

I misunderstood the question, target_link_library is probably the answer to the question.

like image 1
usr1234567 Avatar answered Nov 12 '22 21:11

usr1234567