0

I'm trying to compile a little boost::logger demo application using cmake but my paths aren't being interpreted correctly. This is what I have:
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 ouput:

$ 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

and on compilation I get:

$ 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

even though equal_to.hpp is located in the specified directory:

$ 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

What am I doing wrong here? And how can I fix it?

stdcerr
  • 13,725
  • 25
  • 71
  • 128
  • 1
    Run cmake with `-DCMAKE_VERBOSE_MAKEFILE:BOOL=ON ` to see the exact commands, and check whether the including path is set correctly. – halfelf Sep 05 '18 at 04:56
  • 2
    You commented the line `#find_package(boost)` in your CMakeLists.txt. Package names are case sensitive. Add `find_package(Boost REQUIRED)` to your CMakeLists.txt before the `add_executable` call. After that call add `target_link_libraries(logger Boost::boost ${Boost_LIBRARIES})`. – vre Sep 05 '18 at 06:42
  • 1
    you can influence the search of the `find_package(Boost)` command using a `-DBoost_DIR=[path-to-Boost-root]` option during configuration (see [doc](https://cmake.org/cmake/help/latest/command/find_package.html)). However, this needs to contain a configuration file. – compor Sep 05 '18 at 11:03
  • @vre I've updated `CMakeLists.txt` above as per your suggestions but: #1 I don't see the Boost path (as defined by the `message()` command) and #2 I get a warning about Policy CMP0028 not being set & and that `logger` links to target `Boost::boost` but was not found (hence the missing status message above, I guess). But why does it not stop as `REQUIRED` is defined in `find_package()`? – stdcerr Sep 05 '18 at 13:32
  • 1
    The please add a CMake variable `set(BOOST_ROOT /work/cloudparc/env/include)` to your CMakeLists.txt and make sure you have deleted the CMakeCache.txt file, otherwise your changes may not apply.. – vre Sep 05 '18 at 13:41
  • @vre I updated `CMakeLists.txt` again above, I however still get the same messages (and I don't see the boost path being displayed). I always do `rm -rf *; cmake ..` from `build/` I also added the `cmake` output on top.... – stdcerr Sep 05 '18 at 13:56
  • 1
    As you are not referencing `COMPONENTS` in the `find_package` call `Boost_LIBRARIES` is empty by default. What is your CMake version and your platform? – vre Sep 05 '18 at 14:00
  • @vre `make version 3.5.2` & Linux: Ubuntu 16.04 - the cmake site seems to have problems right now, I can't access the doc... :( – stdcerr Sep 05 '18 at 14:10
  • 1
    That's relevant information. My solution works only with newer CMake versions that provide Boost::boost imported target. So the solution given in the answer is nearly correct: use `target_include_directories(logger Boost_INCLUDE_DIR)` Note the camel case Boost and the underscore! When in doubt see the documentation of the `FindBoost.cmake` module in CMake. – vre Sep 05 '18 at 14:20
  • 1
    Boost::boost target for header only dependencies has been introduced with CMake 3.10. – vre Sep 05 '18 at 14:39
  • @vre So, I'm at `3.5.2` i.e. this should be fine, should it not? – stdcerr Sep 06 '18 at 12:12

1 Answers1

1

Seems like you're not actually including the directory where boost is located. Try adding e.g. this line after setting the directory variable:

target_include_directories(logger BOOST_INCLUDEDIR)

You might also want to consider adding boost as imported target like so:

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

This way the correct directories will be included on linking with boost libraries. Above way to include Boost in project is shamelessly copied from this cmake guide.

paler123
  • 976
  • 6
  • 18