How to add prebuilt static library in project usin

2019-01-27 17:55发布

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

3条回答
再贱就再见
2楼-- · 2019-01-27 18:11

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.

查看更多
贪生不怕死
3楼-- · 2019-01-27 18:12

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.

查看更多
老娘就宠你
4楼-- · 2019-01-27 18:26

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)
查看更多
登录 后发表回答