Recently I tried to use CLion, the IDE to write C++ programs. Since CLion's projects are all built on CMake, importing third-party libraries requires configuration in the CMake file. Here we record the process of using CMake to import third-party libraries.
CMake configuration information is written in the file. In the file, we first define two variables INC_DIR and LINK_DIR to represent the path of the header file and the path of the library. Here, take the wfdb library I placed under the Downloads file as an example, the code is as follows:
set(INC_DIR /Users/haoran/Downloads/wfdb/include)
set(LINK_DIR /Users/haoran/Downloads/wfdb/lib)
Then set the header file directory, library directory, and library to be linked in sequence, as follows:
include_directories(${INC_DIR})
link_directories(${LINK_DIR})
link_libraries(wfdb)
Note that the above code must be placed before the add_executable statement, and the next link library operation must be placed after the add_executable statement.
Use the following statement to complete the link operation of the library:
target_link_libraries(wfdb_demo wfdb)
wfdb_demo in brackets is the project name, and wfdb is the library name.
At this point, we will complete the linking process of the third-party library.
For reference, the complete CMake code for this project is as follows:
cmake_minimum_required(VERSION 3.6)
project(wfdb_demo)
set(CMAKE_CXX_STANDARD 11)
set(SOURCE_FILES )
set(INC_DIR /Users/haoran/Downloads/wfdb/include)
set(LINK_DIR /Users/haoran/Downloads/wfdb/lib)
include_directories(${INC_DIR})
link_directories(${LINK_DIR})
link_libraries(wfdb)
add_executable(wfdb_demo ${SOURCE_FILES})
target_link_libraries(wfdb_demo wfdb)