How can I add a prebuilt static library in a project using CMake?
Asked Answered
M

4

19

CLion: How can I add (or use) a prebuilt static library in my project?

Misdate answered 31/3, 2015 at 11:53 Comment(0)
B
22

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

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

target_link_libraries(myProj myLib)
Bastardy answered 10/4, 2015 at 15:10 Comment(2)
@Andreyua That will simply be target_link_libraries(myProj /tmp/balabla/mylib.a). :)Bastardy
@Bastardy I am still getting Following error cannot find -llibusb1.0where my target_link_libraries( native-lib libusb1.0)error Can you help me plz?Guileless
T
10

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.

Tillman answered 16/8, 2018 at 1:43 Comment(0)
C
5

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.

Congregate answered 12/1, 2020 at 10:44 Comment(0)
B
1

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.

Bennink answered 4/4, 2015 at 13:28 Comment(1)
add_library is for building your own library. As @Bastardy stated, the answer is to use target_link_libraries to add libraries to the link line for a target.Normalize

© 2022 - 2024 — McMap. All rights reserved.