我的CMake文件在将我的程序与Ubuntu下的Boost库链接时应该是什么样的?
运行期间显示的错误make:
main.cpp:(.text+0x3b): undefined reference to `boost::program_options::options_description::m_default_line_length'
Run Code Online (Sandbox Code Playgroud)
主文件非常简单:
#include <boost/program_options/options_description.hpp>
#include <boost/program_options/option.hpp>
using namespace std;
#include <iostream>
namespace po = boost::program_options;
int main(int argc, char** argv) {
po::options_description desc("Allowed options");
desc.add_options()
("help", "produce help message")
;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我成功地做到了.我添加到CMake文件的唯一行是:
target_link_libraries(
my_target_file
${Boost_PROGRAM_OPTIONS_LIBRARY}
)
Run Code Online (Sandbox Code Playgroud)
MOn*_*DaR 140
在CMake中,您可以使用find_package查找所需的库.通常会有一个FindBoost.cmakeCMake安装.
据我所知,它将/usr/share/cmake/Modules/与公共库的其他查找脚本一起安装.您可以查看该文件中的文档,了解有关其工作原理的更多信息.
我头脑中的一个例子:
FIND_PACKAGE( Boost 1.40 COMPONENTS program_options REQUIRED )
INCLUDE_DIRECTORIES( ${Boost_INCLUDE_DIR} )
ADD_EXECUTABLE( anyExecutable myMain.cpp )
TARGET_LINK_LIBRARIES( anyExecutable LINK_PUBLIC ${Boost_LIBRARIES} )
Run Code Online (Sandbox Code Playgroud)
我希望这段代码有所帮助.
Dea*_*hen 49
以下是我的配置:
cmake_minimum_required(VERSION 2.8)
set(Boost_INCLUDE_DIR /usr/local/src/boost_1_46_1)
set(Boost_LIBRARY_DIR /usr/local/src/boost_1_46_1/stage/lib)
find_package(Boost COMPONENTS system filesystem REQUIRED)
include_directories(${Boost_INCLUDE_DIR})
link_directories(${Boost_LIBRARY_DIR})
add_executable(main main.cpp)
target_link_libraries( main ${Boost_LIBRARIES} )
Run Code Online (Sandbox Code Playgroud)
oLe*_*Len 17
使用导入的目标调整@MOnsDaR答案的现代CMake语法,这将是:
find_package(Boost 1.40 COMPONENTS program_options REQUIRED)
add_executable(anyExecutable myMain.cpp)
target_link_libraries(anyExecutable Boost::program_options)
Run Code Online (Sandbox Code Playgroud)
请注意,没有必要手动指定包含目录,因为它已经通过导入的目标进行处理Boost::program_options.
两种方式,使用系统默认安装路径,通常/usr/lib/x86_64-linux-gnu/:
find_package(Boost REQUIRED regex date_time system filesystem thread graph)
include_directories(${BOOST_INCLUDE_DIRS})
message("boost lib: ${Boost_LIBRARIES}")
message("boost inc:${Boost_INCLUDE_DIR}")
add_executable(use_boost use_boost.cpp)
target_link_libraries(use_boost
${Boost_LIBRARIES}
)
Run Code Online (Sandbox Code Playgroud)
如果你在本地目录安装Boost或者选择本地安装而不是系统安装,你可以这样做:
set( BOOST_ROOT "/home/xy/boost_install/lib/" CACHE PATH "Boost library path" )
set( Boost_NO_SYSTEM_PATHS on CACHE BOOL "Do not search system for Boost" )
find_package(Boost REQUIRED regex date_time system filesystem thread graph)
include_directories(${BOOST_INCLUDE_DIRS})
message("boost lib: ${Boost_LIBRARIES}, inc:${Boost_INCLUDE_DIR}")
add_executable(use_boost use_boost.cpp)
target_link_libraries(use_boost
${Boost_LIBRARIES}
)
Run Code Online (Sandbox Code Playgroud)
请注意,上面的目录/home/xy/boost_install/lib/是我安装 Boost 的位置:
xy@xy:~/boost_install/lib$ ll -th
total 16K
drwxrwxr-x 2 xy xy 4.0K May 28 19:23 lib/
drwxrwxr-x 3 xy xy 4.0K May 28 19:22 include/
xy@xy:~/boost_install/lib$ ll -th lib/
total 57M
drwxrwxr-x 2 xy xy 4.0K May 28 19:23 ./
-rw-rw-r-- 1 xy xy 2.3M May 28 19:23 libboost_test_exec_monitor.a
-rw-rw-r-- 1 xy xy 2.2M May 28 19:23 libboost_unit_test_framework.a
.......
xy@xy:~/boost_install/lib$ ll -th include/
total 20K
drwxrwxr-x 110 xy xy 12K May 28 19:22 boost/
Run Code Online (Sandbox Code Playgroud)
如果您对如何使用本地安装的 Boost 感兴趣,可以看到这个问题How can I get CMake to find my Alternative Boost Installation? 。
| 归档时间: |
|
| 查看次数: |
125146 次 |
| 最近记录: |