cmake 3.3 中配置boost1.59.0

我的电脑:

os:deepin 2014.3 桌面版 64位

cmake:3.3

boost:1.59.0

boost位置:/opt/boost

代码布局:

➜  filesystem  tree
.
├── build
├── CMakeLists.txt
└── tut1.cpp

1 directory, 2 files
➜  filesystem

build即为我执行cmake 和 make的目录

tut1.cpp[来自boost自带的示例代码]

//  filesystem tut1.cpp  ---------------------------------------------------------------//

//  Copyright Beman Dawes 2009

//  Distributed under the Boost Software License, Version 1.0.
//  See http://www.boost.org/LICENSE_1_0.txt

//  Library home page: http://www.boost.org/libs/filesystem

#include <iostream>
#include <boost/filesystem.hpp>

using namespace boost::filesystem;

int main(int argc, char* argv[])
{
  if (argc < 2)
  {
    std::cout << "Usage: tut1 path\n";
    return 1;
  }
  std::cout << argv[1] << " " << file_size(argv[1]) << '\n';
  return 0;
}

CMakeLists.txt

➜  filesystem  cat CMakeLists.txt 
cmake_minimum_required(VERSION 3.3)

project(cmaketest CXX)
#project(cmaketest)


set(BOOST_ROOT "/opt/boost")

#set(Boost_USE_STATIC_LIBS ON)
#set(Boost_USE_MULTITHREADED ON)
#set(Boost_USE_STATIC_RUNTIME OFF)


# filesystem依赖system
#
find_package(Boost REQUIRED COMPONENTS
#  regex
filesystem
system
)

if(NOT Boost_FOUND)
  message("未发现Boost")
endif()


aux_source_directory(. srcs)

include_directories(${Boost_INCLUDE_DIRS}
#  /opt/boost/lib
  )


add_executable(hello ${srcs})

target_link_libraries(hello ${Boost_LIBRARIES})
➜  filesystem


注意:

1、设置BOOST_ROOT

2、使用find_package时,不能简单的写find_package(Boost),应当把你需要使用的boost的库的名称也写下来,比如boost_filesystem,就写为filesystem,也就是去掉boost_前缀

3、注意库之间的依赖关系,我之前写的时候在find_package中只写了filesytem,cmake执行没错,但是make时错误提示如下:

➜  build  make
Scanning dependencies of target hello
[ 50%] Building CXX object CMakeFiles/hello.dir/tut1.cpp.o
[100%] Linking CXX executable hello
/usr/bin/ld.bfd.real: CMakeFiles/hello.dir/tut1.cpp.o: undefined reference to symbol '_ZN5boost6system15system_categoryEv'
/opt/boost/lib/libboost_system.so.1.59.0: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
make[2]: *** [hello] 错误 1
make[1]: *** [CMakeFiles/hello.dir/all] 错误 2
make: *** [all] 错误 2
➜  build


可以参考如下网址:

http://stackoverflow.com/questions/20057127/freeling-error-with-python-and-java-api-undefined-symbol-zn5boost6system15sys

具体提示:

183416_56C9_1442002.png

其实想要解决库之间的依赖很简单,我们把所有的库都添加上就可以了

至于find_package中

REQUIRED COMPONENTS

还有我注释提的那三行的意思,我也懒得查了,现在先能用再说

运行效果:

➜  filesystem  l
总用量 16K
drwxrwxr-x 2 laolang laolang 4.0K 10月  4 18:35 .
drwxrwxr-x 4 laolang laolang 4.0K 10月  4 17:04 ..
-rw-rw-r-- 1 laolang laolang  542 10月  4 18:28 CMakeLists.txt
-rw-r--r-- 1 laolang laolang  570 10月  3 16:38 tut1.cpp
➜  filesystem  mkdir build
➜  filesystem  cd build 
➜  build  cmake ..
-- The CXX compiler identification is GNU 4.8.4
-- 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.59.0
-- Found the following Boost libraries:
--   filesystem
--   system
-- Configuring done
-- Generating done
-- Build files have been written to: /home/laolang/code/cpp/test/boost/cmake/boost/filesystem/build
➜  build  make
Scanning dependencies of target hello
[ 50%] Building CXX object CMakeFiles/hello.dir/tut1.cpp.o
[100%] Linking CXX executable hello
[100%] Built target hello
➜  build  ./hello ../tut1.cpp 
../tut1.cpp 570
➜  build


我们可以清楚的看到cmake发现了filesystem和system这两个Boost Liraries,且程序运行良好






















转载于:https://my.oschina.net/iamhere/blog/513455

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值