Clion: how add or (use) prebuilt static library in my Project?
相关问题
- Aggregate static libraries
- Avoid cmake to add the flags -search_paths_first a
- CMakeList file to generate LLVM bitcode file from
- How to fix NDK build error for HelloCardboard samp
- Linking against GLEW with CMake
相关文章
- Weakly link static library via -weak_library
- Target requires the language dialect “CXX17” (with
- How do I tell cmake not to create a console window
- Defining preprocessor symbols for CLion analyzer
- Linker Trouble: How to determine where a “/DEFAULT
- Xcode error: Missing required module 'Firebase
- Do something for all targets
- CMake: Replace compile flags of an INTERFACE targe
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 theCMakeLists.txt
at the project root:First, find the library's location:
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:
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: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 thelibClassLibrary.a
.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.
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.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 librarymyLib.lib
, you can do like this: