0

I am using boost-beast with Visual Studio 2019 on Windows. Everything was working until I added an existing class to main.cpp. Then one of the .h files could not open a boost file. I moved the #include statements into the .cpp files. It still fails with the same error.

The simplest code is:

#include <algorithm>
#include <iostream>
#include <boost/system/error_code.hpp>
#include "pml_webserver/pml_webserver.h"
#include "pml_webserver/product_factory.h"
#include "pml_webserver/config.h"

int main(int argc, char* argv[])
{
    // omitted
}

All of the other files compiled. Main.cpp caused this error:

[4/12] Building CXX object CMakeFiles\pml_webserver.dir\src\main.cpp.obj

FAILED: CMakeFiles/pml_webserver.dir/src/main.cpp.obj 
  C:\PROGRA~2\MICROS~2\2019\PROFES~1\VC\Tools\MSVC\1421~1.277\bin\HostX64\x64\cl.exe  /nologo /TP  -IC:\Users\me\source\xplatform\core_pml_webserver\include -Iinclude /DWIN32 /D_WINDOWS /W3 /GR /EHsc /MDd /ZI /Ob0 /Od /RTC1 /JMC   -std:c++17 /showIncludes /FoCMakeFiles\pml_webserver.dir\src\main.cpp.obj /FdCMakeFiles\pml_webserver.dir\ /FS -c C:\Users\me\source\xplatform\core_pml_webserver\src\main.cpp

C:\Users\me\source\xplatform\core_pml_webserver\include\pml_webserver\base_ws_handler.h(10): fatal error C1083: Cannot open include file: 'boost/system/error_code.hpp': No such file or directory

I cannot determine whether the problem is with Cmake, which I have little experience with, or whether it is caused by a namespace.

Edit: portions of CMakeLists.txt

cmake_minimum_required(VERSION 3.12)
project(pml_webserver CXX)

find_package(Boost 1.70.0 REQUIRED COMPONENTS system date_time)
configure_file(include/pml_webserver/config.h.in
    ${CMAKE_BINARY_DIR}/include/pml_webserver/config.h
)

add_library(pml_webserver_lib
    src/webserver.cpp
    src/base_ws_handler.cpp
    src/ws_handler_factory.cpp
    src/product_ws_handler.cpp
    src/product_factory.cpp

set_target_properties(pml_webserver_lib PROPERTIES PREFIX "")

target_include_directories(pml_webserver_lib PUBLIC ${CMAKE_SOURCE_DIR}/include PUBLIC ${CMAKE_BINARY_DIR}/include)
target_link_libraries(pml_webserver_lib
 PRIVATE Boost::system
 PRIVATE Boost::date_time
 )

add_executable(pml_webserver src/main.cpp)
target_link_libraries(pml_webserver pml_webserver_lib)

I do not know where CMAKE_SOURCE_DIR and CMAKE_BINARY_DIR are defined.

JoeAB
  • 53
  • 9
  • ***I cannot determine whether the problem is with Cmake, which I have little experience with, or whether it is caused by a namespace.*** I don't think the problem is either. I believe the problem is your include path for boost is not properly setup. – drescherjm Jul 21 '19 at 04:04
  • It is CMake code (`CMakeLists.txt`) which affects on include directories and libraries used by your executable. As you don't provide that code, we can only point you to other questions about using Boost with CMake, – Tsyvarev Jul 21 '19 at 08:39

1 Answers1

1

As @drescherjm commented, your issue is that the include path for boost is not properly setup,

If you are using cmake, then your CMakeLists.txt file should contain something like:

find_package(Boost REQUIRED COMPONENTS system)
if(Boost_FOUND)
  target_include_directories(${PROJECT_NAME} PRIVATE ${Boost_INCLUDE_DIRS})
  target_link_libraries(${PROJECT_NAME} PRIVATE Boost::system)
endif(Boost_FOUND)

Note: you should also set the environment variables: BOOST_ROOT and BOOST_LIBRARYDIR to the locations of the boost root and library directories respectively, so that cmake can find boost.

If you aren't comfortable using cmake then you can add the the locations of the boost root and library binary directories directly to Visual Studio as described here.

kenba
  • 4,303
  • 1
  • 23
  • 40
  • I installed Boost using vcpkg. I set `BOOST_ROOT` and `BOOST_LIBRARYDIR` to `~\vcpkg\installed\x64-windows`. Are these correct? – JoeAB Jul 22 '19 at 15:22
  • I set up boost directly and haven't used `vcpkg`. However, the directory name looks wrong to me: it should be an absolute, not a relative path name, i.e. it should start with `C:\`. Also `BOOST_ROOT` and `BOOST_LIBRARYDIR` are usually different directories... – kenba Jul 23 '19 at 11:59