0

我正在尝试使用编译一个小boost::logger演示应用程序,cmake但我的路径没有被正确解释。这就是我所拥有的
logger.cpp::

#include <iostream>
#include <boost/fusion/iterator/equal_to.hpp>
#include <boost/log/core.hpp>
#include <boost/log/trivial.hpp>
#include <boost/log/expressions.hpp>

namespace logging = boost::log;
namespace src = boost::log::sources;
namespace sinks = boost::log::sinks;
namespace keywords = boost::log::keywords;
namespace expr = boost::log::expressions;


void init()
{
    logging::add_file_log("sample.log");

    logging::core::get()->set_filter
    (   
        logging::trivial::severity >= logging::trivial::info
    );
}

int main(void) {
    init();

    std::cout <<"Hello World!";

CMakeLists.txt

cmake_minimum_required(VERSION 2.6)
project(LOGGER)

set(BOOST_INCLUDEDIR "/path/to/env/include")
set(BOOST_ROOT "/path/to/env/include")

find_package(Boost REQUIRED)

message (STATUS ${Boost_LIBRARIES})

ADD_EXECUTABLE(logger logger.cpp)

target_link_libraries(logger Boost::boost ${Boost_LIBRARIES})

set (CMAKE_CXX_FLAGS "-g -Wall")

cmake输出:

$ rm -rf *;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.67.0
-- 
-- Configuring done
CMake Warning (dev) at CMakeLists.txt:11 (ADD_EXECUTABLE):
  Policy CMP0028 is not set: Double colon in target name means ALIAS or
  IMPORTED target.  Run "cmake --help-policy CMP0028" for policy details.
  Use the cmake_policy command to set the policy and suppress this warning.

  Target "logger" links to target "Boost::boost" but the target was not
  found.  Perhaps a find_package() call is missing for an IMPORTED target, or
  an ALIAS target is missing?
This warning is for project developers.  Use -Wno-dev to suppress it.

-- Generating done
-- Build files have been written to: /path/to/src/tmp/logger/build

在编译时我得到:

$ make
[ 50%] Building CXX object CMakeFiles/logger.dir/logger.cpp.o
/path/to/src/tmp/logger/logger.cpp:4:46: fatal error: boost/fusion/iterator/equal_to.hpp: No such file or directory
compilation terminated.
CMakeFiles/logger.dir/build.make:62: recipe for target 'CMakeFiles/logger.dir/logger.cpp.o' failed
make[2]: *** [CMakeFiles/logger.dir/logger.cpp.o] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/logger.dir/all' failed
make[1]: *** [CMakeFiles/logger.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2

即使equal_to.hpp位于指定目录中:

$ ls -l /path/to/env/include/boost/fusion/iterator/equal_to.hpp
-rw-rw-rw- 1 ron ron 3330 Apr 17 02:01 /path/to/env/include/boost/fusion/iterator/equal_to.hpp

我在这里做错了什么?我该如何解决?

4

1 回答 1

1

似乎您实际上并未包括 boost 所在的目录。在设置目录变量后尝试添加例如这一行:

target_include_directories(logger BOOST_INCLUDEDIR)

您可能还想考虑添加 boost 作为导入的目标,如下所示:

find_package(Boost REQUIRED)
add_library(boost INTERFACE IMPORTED)
set_property(TARGET boost PROPERTY
    INTERFACE_INCLUDE_DIRECTORIES ${Boost_INCLUDE_DIR})

这样,正确的目录将包含在与 boost 库的链接中。以上在项目中包含 Boost 的方法是从这个 cmake 指南中无耻地复制的。

于 2018-09-05T14:01:10.207 回答