ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

第11节 导入目标

2021-07-28 11:33:49  阅读:228  来源: 互联网

标签:11 imported -- 目标 CXX 导入 Boost targets compiler


介绍

正如前面在第8节中提到的,较新版本的CMake允许你使用导入的别名目标链接第三方库。

本教程中的文件如下:

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

    cmake_minimum_required(VERSION 3.5)
    
    # Set the project name
    project (imported_targets)
    
    
    # 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(imported_targets main.cpp)
    
    # link against the boost libraries
    target_link_libraries( imported_targets
        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;
    }
    

要求

此示例需要以下条件:

  • CMake v3.5+
  • 安装在默认系统位置的Boost库

概念

导入目标

导入目标是由FindXXX模块导出的只读目标(例如Boost::filesystem)。

要包括Boost文件系统,你可以执行以下操作:

  target_link_libraries( imported_targets
      PRIVATE
          Boost::filesystem
  )

这将自动链接Boost::FileSystemBoost::System库,同时还包括Boost include目录(即不必手动添加include目录)。

构建示例

$ mkdir build

$ cd build/

$ cmake ..
-- The C compiler identification is GNU 5.4.0
-- The CXX compiler identification is GNU 5.4.0
-- 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
-- Detecting C compile features
-- Detecting C compile features - 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
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Boost version: 1.58.0
-- Found the following Boost libraries:
--   filesystem
--   system
boost found
-- Configuring done
-- Generating done
-- Build files have been written to: /data/code/01-basic/K-imported-targets/build

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


$ ./imported_targets
Hello Third Party Include!
Path is not relative

标签:11,imported,--,目标,CXX,导入,Boost,targets,compiler
来源: https://www.cnblogs.com/juzaizai/p/15069682.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有