使用cmake错误链接boost日志 [英] linking boost log using cmake error

查看:302
本文介绍了使用cmake错误链接boost日志的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么会导致以下问题: 我试图在cmake项目中使用boost :: log.

我的CMakeList.txt文件如下:

cmake_minimum_required(VERSION 3.10)
project(boostLogTest)
set(CMAKE_CXX_STANDARD 11)
add_executable(boostLogTest main.cpp)
ADD_DEFINITIONS(-DBOOST_LOG_DYN_LINK) #####  edit after DiCri's answer #####
find_package(Boost REQUIRED COMPONENTS log system)

if (Boost_FOUND)
    include_directories(${Boost_INCLUDE_DIRS})

    message("Boost VERSION: ${Boost_VERSION}")
    message("Boost INCLUDE_DIRS: ${Boost_INCLUDE_DIRS}")
    message("Boost Boost_LIBRARY_DIRS: ${Boost_LIBRARY_DIRS}")
    message("Boost LIBRARIES: ${Boost_LIBRARIES}")

    TARGET_LINK_LIBRARIES(boostLogTest ${Boost_LIBRARIES})
endif ()

我的main.cpp文件看起来像这样:

#include <iostream>

#include <boost/log/trivial.hpp>

int main(int, char*[])
{
    BOOST_LOG_TRIVIAL(trace) << "A trace severity message";
    BOOST_LOG_TRIVIAL(debug) << "A debug severity message";
    BOOST_LOG_TRIVIAL(info) << "An informational severity message";
    BOOST_LOG_TRIVIAL(warning) << "A warning severity message";
    BOOST_LOG_TRIVIAL(error) << "An error severity message";
    BOOST_LOG_TRIVIAL(fatal) << "A fatal severity message";

    return 0;
}

我的CMake输出如下

-Boost版本:1.64.0

-找到了以下Boost库:

-日志

-系统

-date_time

-log_setup

-文件系统

-线程

-正则表达式

-计时

-原子Boost版本:106400 Boost INCLUDE_DIRS:/usr/local/include Boost Boost_LIBRARY_DIRS:/usr/local/lib Boost

图书馆: /usr/local/lib/libboost_log.so;/usr/local/lib/libboost_system.so;/usr/local/lib/libboost_date_time.so;/usr/local/lib/libboost_log_setup.so;/usr/local/lib/libboost_log.set.so;/usr/local/lib/libboost_filesystem.so;/usr/local/lib/libboost_thread.so;/usr/local/lib/libboost_regex.so;/usr/local/lib/libboost_chrono.so;/usr/local/lib/libboost_atomic.so

-配置完成

-生成完成

-构建文件已写入:/home/.../CLionProjects/boostLogTest/cmake-build-debug

但是在链接时出现以下错误消息:

[50%]链接CXX可执行文件boostLogTest

CMakeFiles/boostLogTest.dir/main.cpp.o:在函数"main"中:

/home/.../CLionProjects/boostLogTest/main.cpp:7:对`boost :: log :: v2s_mt_posix :: trivial :: logger :: get()'的未定义引用

/home/.../CLionProjects/boostLogTest/main.cpp:7:对`boost :: log :: v2s_mt_posix :: trivial :: logger :: get()'的未定义引用

/home/.../CLionProjects/boostLogTest/main.cpp:8:对`boost :: log :: v2s_mt_posix :: trivial :: logger :: get()'的未定义引用

/home/.../CLionProjects/boostLogTest/main.cpp:8:对`boost :: log :: v2s_mt_posix :: trivial :: logger :: get()'的未定义引用

/home/.../CLionProjects/boostLogTest/main.cpp:9:对boost::log::v2s_mt_posix::trivial::logger::get()' CMakeFiles/boostLogTest.dir/main.cpp.o:/home/.../CLionProjects/boostLogTest/main.cpp:9: more undefined references to boost :: log :: v2s_mt_posix :: trivial :: logger :: get()'的未定义引用... .../p>

在DiCri的答案后进行 现在,我收到以下错误消息:

[50%]链接CXX可执行文件boostLogTest

/usr/bin/ld:CMakeFiles/boostLogTest.dir/main.cpp.o:对符号'pthread_rwlock_unlock @@ GLIBC_2.2.5'的未定义引用

///lib/x86_64-linux-gnu/libpthread.so.0:添加符号时出错:命令行中缺少DSO

collect2:错误:ld返回1个退出状态

CMakeFiles/boostLogTest.dir/build.make:103:目标"boostLogTest"的配方失败

make [3]:*** [boostLogTest]错误1

CMakeFiles/Makefile2:67:目标'CMakeFiles/boostLogTest.dir/all'的配方失败

make [2]:*** [CMakeFiles/boostLogTest.dir/all]错误2

CMakeFiles/Makefile2:79:目标'CMakeFiles/boostLogTest.dir/rule'的配方失败

make [1]:*** [CMakeFiles/boostLogTest.dir/rule]错误2

Makefile:118:目标"boostLogTest"的配方失败

make:*** [boostLogTest]错误2

如果我添加

set(Boost_USE_STATIC_LIBS        ON) 

库的路径更改为* .a文件,例如:

Boost LIBRARIES: /usr/local/lib/libboost_log.a;/usr/local/lib/libboost_system.a;...

所有列出的库(* .so和* .a)都存在于此文件夹中.

我想念什么?

解决方案

只需尝试添加此行

#define BOOST_LOG_DYN_LINK 1

作为main.cpp文件的第一行,我认为它应该可以工作,有人遇到了相同的问题

and my main.cpp file looks like this:

#include <iostream>

#include <boost/log/trivial.hpp>

int main(int, char*[])
{
    BOOST_LOG_TRIVIAL(trace) << "A trace severity message";
    BOOST_LOG_TRIVIAL(debug) << "A debug severity message";
    BOOST_LOG_TRIVIAL(info) << "An informational severity message";
    BOOST_LOG_TRIVIAL(warning) << "A warning severity message";
    BOOST_LOG_TRIVIAL(error) << "An error severity message";
    BOOST_LOG_TRIVIAL(fatal) << "A fatal severity message";

    return 0;
}

My CMake output is the following

-- Boost version: 1.64.0

-- Found the following Boost libraries:

-- log

-- system

-- date_time

-- log_setup

-- filesystem

-- thread

-- regex

-- chrono

-- atomic Boost VERSION: 106400 Boost INCLUDE_DIRS: /usr/local/include Boost Boost_LIBRARY_DIRS: /usr/local/lib Boost

LIBRARIES: /usr/local/lib/libboost_log.so;/usr/local/lib/libboost_system.so;/usr/local/lib/libboost_date_time.so;/usr/local/lib/libboost_log_setup.so;/usr/local/lib/libboost_filesystem.so;/usr/local/lib/libboost_thread.so;/usr/local/lib/libboost_regex.so;/usr/local/lib/libboost_chrono.so;/usr/local/lib/libboost_atomic.so

-- Configuring done

-- Generating done

-- Build files have been written to: /home/.../CLionProjects/boostLogTest/cmake-build-debug

but while linking I get the following error messages:

[ 50%] Linking CXX executable boostLogTest

CMakeFiles/boostLogTest.dir/main.cpp.o: In function `main':

/home/.../CLionProjects/boostLogTest/main.cpp:7: undefined reference to `boost::log::v2s_mt_posix::trivial::logger::get()'

/home/.../CLionProjects/boostLogTest/main.cpp:7: undefined reference to `boost::log::v2s_mt_posix::trivial::logger::get()'

/home/.../CLionProjects/boostLogTest/main.cpp:8: undefined reference to `boost::log::v2s_mt_posix::trivial::logger::get()'

/home/.../CLionProjects/boostLogTest/main.cpp:8: undefined reference to `boost::log::v2s_mt_posix::trivial::logger::get()'

/home/.../CLionProjects/boostLogTest/main.cpp:9: undefined reference to boost::log::v2s_mt_posix::trivial::logger::get()' CMakeFiles/boostLogTest.dir/main.cpp.o:/home/.../CLionProjects/boostLogTest/main.cpp:9: more undefined references toboost::log::v2s_mt_posix::trivial::logger::get()' follow ...

Edit after DiCri's answer: Now I get the following error messages:

[ 50%] Linking CXX executable boostLogTest

/usr/bin/ld: CMakeFiles/boostLogTest.dir/main.cpp.o: undefined reference to symbol 'pthread_rwlock_unlock@@GLIBC_2.2.5'

//lib/x86_64-linux-gnu/libpthread.so.0: error adding symbols: DSO missing from command line

collect2: error: ld returned 1 exit status

CMakeFiles/boostLogTest.dir/build.make:103: recipe for target 'boostLogTest' failed

make[3]: *** [boostLogTest] Error 1

CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/boostLogTest.dir/all' failed

make[2]: *** [CMakeFiles/boostLogTest.dir/all] Error 2

CMakeFiles/Makefile2:79: recipe for target 'CMakeFiles/boostLogTest.dir/rule' failed

make[1]: *** [CMakeFiles/boostLogTest.dir/rule] Error 2

Makefile:118: recipe for target 'boostLogTest' failed

make: *** [boostLogTest] Error 2

If I add

set(Boost_USE_STATIC_LIBS        ON) 

the paths to the libraries change to a *.a file, like:

Boost LIBRARIES: /usr/local/lib/libboost_log.a;/usr/local/lib/libboost_system.a;...

All listed libraries (the *.so and the *.a) exist in this folders.

What am I missing?

解决方案

Just try to add this line

#define BOOST_LOG_DYN_LINK 1

As the first line of the main.cpp file, i think it should work, someone got your same issue here

If that doesn't work, try to add this line

ADD_DEFINITIONS(-DBOOST_LOG_DYN_LINK)

To CMake file You need to add OpenMP package too

这篇关于使用cmake错误链接boost日志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆