我正在尝试使用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
我在这里做错了什么?我该如何解决?
答案 0 :(得分: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库链接时将包含正确的目录。从this cmake guide毫不客气地复制了将Boost包含在项目中的上述方法。