目录
代码依赖
自己写的工程,非第三方工程
- 工程目录
├── 3rdparty
│ └── jsoncpp
│ ├── include
│ └── src
├── build
├── CMakeLists.txt
└── main.cpp
- CMakeLists.txt
cmake_minimum_required(VERSION 3.11)
project(cmake_3rd)
set(CMAKE_CXX_STANDARD 17)
set(SRC_MAIN main.cpp)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/jsoncpp/include)
aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/jsoncpp/src jsoncpp)
add_executable(main ${SRC_MAIN} ${jsoncpp})
- main.cpp
#include <iostream>
#include "json/json.h"
int main(int argc, char const *argv[])
{
Json::Value json;
json["name"] = "wiki";
json["age"] = 18;
std::cout << json.toStyledString() << std::endl;
return 0;
}
引入他人的第三方代码库,独立的库
- 工程目录
├── 3rdparty
│ └── jsoncpp
│ ├── CMakeLists.txt
│ ├── include
│ └── src
├── build
├── CMakeLists.txt
└── main.cpp
- 3rdparty/jsoncpp/CMakeLists.txt
project(jsoncpp)
aux_source_directory(./src SRC_JSONCPP)
add_library(jsoncpp ${SRC_JSONCPP})
- CMakeLists.txt
cmake_minimum_required(VERSION 3.11)
project(cmake_3rd)
set(CMAKE_CXX_STANDARD 17)
set(SRC_MAIN main.cpp)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/jsoncpp/include)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/jsoncpp)
add_executable(main ${SRC_MAIN})
target_link_libraries(main jsoncpp)
目标文件依赖
jsoncpp工程,生成lib库
- 过程目录
├── build
├── CMakeLists.txt
├── include
│ └── json
│ ├── allocator.h
│ ├── assertions.h
│ ├── autolink.h
│ ├── config.h
│ ├── features.h
│ ├── forwards.h
│ ├── json.h
│ ├── reader.h
│ ├── value.h
│ ├── version.h
│ └── writer.h
└── src
├── json_reader.cpp
├── json_tool.h
├── json_value.cpp
├── json_valueiterator.inl
├── json_writer.cpp
└── version.h.in
- CMakeLists.txt
cmake_minimum_required(VERSION 3.11)
project(jsoncpp)
include_directories(${PROJECT_SOURCE_DIR}/include)
aux_source_directory(./src SRC_JSONCPP)
add_library(lib_jsoncpp_static STATIC ${SRC_JSONCPP})
add_library(lib_jsoncpp_dynamic SHARED ${SRC_JSONCPP})
set_target_properties(lib_jsoncpp_static PROPERTIES OUTPUT_NAME "jsoncpp")
set_target_properties(lib_jsoncpp_dynamic PROPERTIES OUTPUT_NAME "jsoncpp")
set(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib)
将jsoncpp/build/lib文件夹拷贝到使用者目录(main.cpp所在目录的build目录下)
main.cpp工程
- 工程目录
├── 3rdparty
│ └── jsoncpp
│ └── include
├── build
│ ├── lib
│ │ ├── libjsoncpp.a
│ │ └── libjsoncpp.so
├── CMakeLists.txt
└── main.cpp
- 方法1:CMakeLists.txt
cmake_minimum_required(VERSION 3.11)
project(cmake_3rd)
set(CMAKE_CXX_STANDARD 17)
set(SRC_MAIN main.cpp)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/jsoncpp/include)
link_directories(${PROJECT_BINARY_DIR}/lib)
add_executable(main ${SRC_MAIN})
target_link_libraries(main libjsoncpp.a)
- link_directories
:执行后可以链接该目录下的多个库文件
- 方法2:CMakeLists.txt
cmake_minimum_required(VERSION 3.11)
project(cmake_3rd)
set(CMAKE_CXX_STANDARD 17)
set(SRC_MAIN main.cpp)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/jsoncpp/include)
add_executable(main ${SRC_MAIN})
find_library(lib_jsoncpp NAMES libjsoncpp.so PATHS ${PROJECT_BINARY_DIR}/lib)
target_link_libraries(main ${lib_jsoncpp})
- find_library
:执行后,只能使用该库(如果工程需要链接其他目标,则需要再次find_library)
find_package
- 工程目录
├── build
├── CMakeLists.txt
└── main.cpp
- CMakeLists.txt
cmake_minimum_required(VERSION 3.11)
project(cmake_3rd)
set(CMAKE_CXX_STANDARD 17)
set(SRC_MAIN main.cpp)
find_package(Boost COMPONENTS atomic REQUIRED)
if (Boost_FOUND)
message("Boost_INCLUDE_DIRS = ${Boost_INCLUDE_DIRS}")
message("Boost_LIBRARIES = ${Boost_LIBRARIES}")
message("Boost_VERSION = ${Boost_VERSION}")
include_directories(${Boost_INCLUDE_DIRS})
add_executable(main ${SRC_MAIN})
target_link_libraries(main ${Boost_LIBRARIES})
endif()
- main.cpp
#include <iostream>
#include "boost/atomic.hpp"
int main(int argc, char const *argv[])
{
boost::atomic_bool b = true;
std::cout << b << std::endl;
return 0;
}