CMake基础 第8节 包含第三方库

介绍#

几乎所有重要的项目都需要包含第三方库、头文件或程序。CMake支持使用find_package()函数查找这些工具的路径。这将从CMAKE_MODULE_PATH中的文件夹列表中搜索格式为FindXXX.cmake的CMake模块。在Linux上,默认搜索路径将包含/usr/share/cmake/Modules。在我的系统上,这包括对大约1420个通用第三方库的支持。

本教程中的文件如下:

$ tree
.
├── CMakeLists.txt
├── main.cpp
  • [CMakeLists.txt] - 包含要运行的CMake命令

    cmake_minimum_required(VERSION 3.5)
    
    # Set the project name
    project (third_party_include)
    
    
    # find a boost install with the libraries filesystem and system
    find_package(Boost 1.46.1 REQUIRED COMPONENTS filesystem system)
    
    # check if boost was found
    if(Boost_FOUND)
        message ("boost found")
    else()
        message (FATAL_ERROR "Cannot find Boost")
    endif()
    
    # Add an executable
    add_executable(third_party_include main.cpp)
    
    # link against the boost libraries
    target_link_libraries( third_party_include
        PRIVATE
            Boost::filesystem
    )
    
  • [main.cpp] - 具有main的源文件

    #include <iostream>
    #include <boost/shared_ptr.hpp>
    #include <boost/filesystem.hpp>
    
    int main(int argc, char *argv[])
    {
        std::cout << "Hello Third Party Include!" << std::endl;
    
        // use a shared ptr
        boost::shared_ptr<int> isp(new int(4));
    
        // trivial use of boost filesystem
        boost::filesystem::path path = "/usr/share/cmake/modules";
        if(path.is_relative())
        {
            std::cout << "Path is relative" << std::endl;
        }
        else
        {
            std::cout << "Path is not relative" << std::endl;
        }
    
       return 0;
    }
    

要求#

此示例要求将Boost库安装在默认系统位置。

sudo apt-get install libboost-all-dev -y

概念#

查找一个包#

如上所述,find_package()函数将从CMAKE_MODULE_PATH中的文件夹列表中搜索格式为FindXXX.cmake的CMake模块。find_package的参数的确切格式将取决于你要查找的模块。这通常记录在文件FindXXX.cmake的顶部

下面是查找Boost的基本示例:

find_package(Boost 1.46.1 REQUIRED COMPONENTS filesystem system)

这些参数是:

  • Boost -库的名称。这是用于查找模块文件FindBoost.cmake的一部分。
  • 1.46.1 - 要查找的Boost的最低版本。
  • REQUIRED - 告诉模块这是必需的,如果失败,则找不到该模块。
  • COMPONENTS - 要查找的库列表。

Boost includes可以接受更多参数,还可以利用其他变量。更复杂的设置将在后面的示例中提供。

检查是否找到该包#

大多数包含的软件包都会设置一个变量XXX_FOUND,该变量可用于检查该软件包在系统上是否可用。

在本例中,变量为BOOST_FOUND

if(Boost_FOUND)
    message ("boost found")
    include_directories(${Boost_INCLUDE_DIRS})
else()
    message (FATAL_ERROR "Cannot find Boost")
endif()

导出变量#

在找到包之后,它通常会导出变量,这些变量可以告诉用户在哪里可以找到库、头文件或可执行文件。与XXX_FOUND变量类似,它们是特定于包的,通常记录在FindXXX.cmake文件的顶部。

本例中导出的变量包括:

  • Boost_INCLUDE_DIRS - Boost头文件的路径

在某些情况下,你还可以通过使用ccmake或cmake-gui检查缓存来检查这些变量。

别名/导入目标#

大多数现代CMake库在其模块文件中导出别名目标。导入目标的好处在于,它们还可以填充头文件目录和链接库。

例如,从CMake的3.5版开始,Boost模块就支持此功能。

类似于将你自己的别名目标用于库,模块中的别名可以让引用找到的目标变得更容易。

在Boost的例子中,所有目标通过使用标识符Boost::加子模块的名字来导出。例如,你可以使用:

  • Boost::boost 仅适用于库的头文件
  • Boost::system 对于Boost系统库
  • Boost::filesystem 对于文件系统库

与你自己的目标一样,这些目标包含它们的依赖项,因此链接到 Boost::filesystem 将自动添加 Boost::boostBoost::system依赖。

要链接到导入的目标,可以使用以下命令:

  target_link_libraries( third_party_include
      PRIVATE
          Boost::filesystem
  )

非别名目标#

虽然大多数现代库使用导入的目标,但并非所有模块都已更新。在库尚未更新的情况下,你通常会发现以下变量可用:

  • xxx_INCLUDE_DIRS - 指向库的include目录的变量
  • xxx_LIBRARY - 指向库路径的变量.

然后,可以将这些文件添加到target_include_directory和target_link_library中:

# Include the boost headers
target_include_directories( third_party_include
    PRIVATE ${Boost_INCLUDE_DIRS}
)

# link against the boost libraries
target_link_libraries( third_party_include
    PRIVATE
    ${Boost_SYSTEM_LIBRARY}
    ${Boost_FILESYSTEM_LIBRARY}
)

构建示例#

$ mkdir build

$ cd build/

$ cmake ..
-- The C compiler identification is GNU 4.8.4
-- The CXX compiler identification is GNU 4.8.4
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Boost version: 1.54.0
-- Found the following Boost libraries:
--   filesystem
--   system
boost found
-- Configuring done
-- Generating done
-- Build files have been written to: /home/matrim/workspace/cmake-examples/01-basic/H-third-party-library/build

$ make
Scanning dependencies of target third_party_include
[100%] Building CXX object CMakeFiles/third_party_include.dir/main.cpp.o
Linking CXX executable third_party_include
[100%] Built target third_party_include
matrim@freyr:~/workspace/cmake-examples/01-basic/H-third-party-library/build$ ./
CMakeFiles/          third_party_include
matrim@freyr:~/workspace/cmake-examples/01-basic/H-third-party-library/build$ ./third_party_include
Hello Third Party Include!
Path is not relative
$ cmake ..
-- The C compiler identification is GNU 4.8.4
-- The CXX compiler identification is GNU 4.8.4
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Boost version: 1.54.0
-- Found the following Boost libraries:
--   filesystem
--   system
boost found
-- Configuring done
-- Generating done
-- Build files have been written to: /home/matrim/workspace/cmake-examples/01-basic/H-third-party-library/build

$ make
Scanning dependencies of target third_party_include
[100%] Building CXX object CMakeFiles/third_party_include.dir/main.cpp.o
Linking CXX executable third_party_include
[100%] Built target third_party_include

$ ./third_party_include
Hello Third Party Include!
Path is not relative
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: 。 cmake_minimum_required(VERSION 3.0) # Add asio find_package(asio REQUIRED) include_directories(${asio_INCLUDE_DIR}) # Add boost find_package(Boost REQUIRED) include_directories(${Boost_INCLUDE_DIRS}) # Add websocketpp find_package(websocketpp REQUIRED) include_directories(${websocketpp_INCLUDE_DIR}) # Add source files add_executable(main main.cpp) # Link libraries to executable target_link_libraries(main ${asio_LIBRARY} ${Boost_LIBRARIES} ${websocketpp_LIBRARY}) ### 回答2: 在CMakeLists.txt中添加第三方库asio、boost和websocketpp,可以按照以下步骤进行操作: 1. 导入CMake模块,以支持使用find_package命令查找库的位置。 ```cmake cmake_minimum_required(VERSION 3.0) project(MyProject) ``` 2. 查找并包含asio库。 ```cmake find_package(Boost REQUIRED COMPONENTS system) include_directories(${BOOST_INCLUDE_DIRS}) # 或者手动指定asio库的路径 # set(ASIO_INCLUDE_DIR /path/to/asio) # include_directories(${ASIO_INCLUDE_DIR}) ``` 3. 查找并包含boost库。 ```cmake find_package(Boost REQUIRED COMPONENTS system) include_directories(${BOOST_INCLUDE_DIRS}) ``` 4. 查找并包含websocketpp库。 ```cmake # 通过git下载websocketpp源码 execute_process(COMMAND git clone https://github.com/zaphoyd/websocketpp.git) # 设置websocketpp库的路径 set(WEBSOCKETPP_INCLUDE_DIR ${CMAKE_SOURCE_DIR}/websocketpp) include_directories(${WEBSOCKETPP_INCLUDE_DIR}) ``` 5. 配置源代码文件和可执行文件。 ```cmake # 添加源代码文件 add_executable(MyExecutable main.cpp) # 链接库 target_link_libraries(MyExecutable Boost::system) ``` 这样,CMakeLists.txt中添加的代码就可以支持在项目中使用asio、boost和websocketpp库了。在进行编译时,CMake会自动查找并链接这些库。记得将路径替换为实际安装的库的正确路径。 ### 回答3: 在使用CMake编写CMakeLists.txt文件时,我们可以通过以下步骤添加第三方库asio、boost和websocketpp: 1. 下载、安装并配置asio库、boost库和websocketpp库,确保可以正确地找到它们的安装路径。 2. 在项目的根目录下创建一个名为CMakeLists.txt的文件。 3. 在CMakeLists.txt文件中添加以下内容: ``` cmake_minimum_required(VERSION 3.0) # 指定CMake的最低版本 project(MyProject) # 设置项目名称 # 设置编译选项,如编译器标志等 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") # 设置C++编译标志 # 添加包含目录 include_directories(/path/to/asio/include) # 替换为你的asio库的包含目录 include_directories(/path/to/boost/include) # 替换为你的boost库的包含目录 include_directories(/path/to/websocketpp/include) # 替换为你的websocketpp库的包含目录 # 添加链接库目录 link_directories(/path/to/asio/lib) # 替换为你的asio库的链接库目录 link_directories(/path/to/boost/lib) # 替换为你的boost库的链接库目录 link_directories(/path/to/websocketpp/lib) # 替换为你的websocketpp库的链接库目录 # 添加源文件 add_executable(MyExecutable main.cpp) # 替换为你的源文件 # 链接库 target_link_libraries(MyExecutable asio boost websocketpp) # 添加所需的链接库 ``` 注意替换对应的路径和库名称。 4. 保存并关闭CMakeLists.txt文件。 5. 打开终端,进入项目的根目录。 6. 创建一个名为build的文件夹,作为构建目录。 7. 在终端中执行以下命令: ``` cmake -S . -B ./build # 生成构建文件 cmake --build ./build # 编译项目 ``` 上述命令将在build文件夹中生成构建文件,并通过构建文件编译项目。 8. 编译完成后,可在build文件夹中找到生成的可执行文件。 9. 运行可执行文件,该应用程序将会使用asio、boost和websocketpp库。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值