CMake introduces three-party libraries

NDK development on the mobile side often requires the introduction of a third-party library. This article uses a common JSON library as an example to illustrate

jsoncpp source code download address https://github.com/open-source-parsers/jsoncpp

Download the 1.9.5 tag

1. Pure source code dependency

testjson1.zip

The project directory is as follows:

The jsoncpp directory is the source code of json

main.cpp is the test code, as follows

#include <iostream>

#include "json/json.h"

int main() {

Json::Value json;

json["name"] = "Wiki";

json["age"] = 18;

std::cout << json.toStyledString() << std::endl;

return 0;

}

CMakeLists.txt

cmake_minimum_required(VERSION 3.17)

project(testjson)

set(CMAKE_CXX_STANDARD 14)

# 头文件引入

include_directories(./jsoncpp/include)

#查找当前目录下、及protocol目录下所有cpp文件,保存在变量SEC_LIST中

file(GLOB SRC_LIST "*.cpp" "jsoncpp/*.cpp")

add_executable(testjson ${SRC_LIST})

# add_library(testjson SHARED ${SRC_LIST})

Results of the

2. Internal engineering dependencies

testjson2.zip

Sometimes, the tripartite library is provided by other teams within the company. In addition to providing source code, it also comes with CMakeLists.txt

Taking jsoncpp as an example, the directory structure is as follows:

The CMakeLists.txt in the jsoncpp directory is as follows:

cmake_minimum_required(VERSION 3.17)

# 指定C++版本 1.9.5版本的jsoncpp需要用C++11版本的

set(CMAKE_CXX_STANDARD 11)

project(jsoncpp)

add_library(${PROJECT_NAME} json_tool.h json_valueiterator.inl json_reader.cpp json_value.cpp json_writer.cpp)

target_include_directories(${PROJECT_NAME} PUBLIC ${PROJECT_SOURCE_DIR}/include)

The CMakeLists.txt in the main project directory is as follows:

cmake_minimum_required(VERSION 3.17)

# 指定C++版本 1.9.5版本的jsoncpp需要用C++11版本的

set(CMAKE_CXX_STANDARD 11)

project(testjson)

# 添加子工程

add_subdirectory(jsoncpp)

add_executable(${PROJECT_NAME} main.cpp)

# 链接子工程,子工程addlibrary时生成的名字

target_link_libraries(${PROJECT_NAME} jsoncpp)

Results of the

3. Static library/dynamic library mode dependency

testjson3.zip

Sometimes, the third party does not provide source code, but only header files and static library files

Also take jsoncpp as an example, use the jsoncpp static file compiled in 2

The project directory is as follows:

The CMakeLists.txt in the main project directory is as follows:

cmake_minimum_required(VERSION 3.17)

# 指定C++版本

set(CMAKE_CXX_STANDARD 11)

project(testjson)

# 包含头文件

include_directories(jsoncpp/include)

add_executable(${PROJECT_NAME} main.cpp)

# 找到三方静态库

find_library(jsoncpp_lib NAMES jsoncpp PATHS ./jsoncpp)

target_link_libraries(${PROJECT_NAME} ${jsoncpp_lib})

Results of the

reference article

1. C++ project: Summary of CMake adding third-party library dependency methods git submodule, find_library, FetchContent, CPM, etc. https://www.jianshu.com/p/f181b5bd0a63

2. The difference between target_include_directories and include_directories cmake: PUBLIC, PRIVATE, INTERFACE in target_** bzdww

3. The difference between dynamic link and static link https://www.jianshu.com/p/c3ff6310f1f4

Guess you like

Origin blog.csdn.net/guo_zhen_qian/article/details/126379136