我的电脑:
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
可以参考如下网址:
具体提示:
其实想要解决库之间的依赖很简单,我们把所有的库都添加上就可以了
至于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,且程序运行良好