1
votes

I've checked several questions, yet none of the answers helped my case. Here are the ones I checked

How to link C++ program with Boost using CMake

Whats the proper way to link Boost with CMake and Visual Studio in Windows?

Error linking Boost with CMake

I've built Boost 1.61 with

b2 variant=debug,release link=static,shared threading=single,multi

Under stage/lib, I have all dlls and libs. Following Boost::System libs are present under stage/lib

boost_system-vc140-mt-1_61.dll
boost_system-vc140-mt-1_61.lib
boost_system-vc140-mt-gd-1_61.dll
boost_system-vc140-mt-gd-1_61.lib
libboost_system-vc140-mt-1_61.lib
libboost_system-vc140-mt-gd-1_61.lib

I want to compile a basic boost asio example (which requires system lib from boost). I make my cmake file with cmake-gui in windows, then generate VS2015 project files. After attempting to build the project, following error occurs.

1>------ Build started: Project: ZERO_CHECK, Configuration: Debug Win32 ------
2>------ Build started: Project: boost-asio, Configuration: Debug Win32 ------
2>  main.cpp
2>  Please define _WIN32_WINNT or _WIN32_WINDOWS appropriately. For example:
2>  - add -D_WIN32_WINNT=0x0501 to the compiler command line; or
2>  - add _WIN32_WINNT=0x0501 to your project's Preprocessor Definitions.
2>  Assuming _WIN32_WINNT=0x0501 (i.e. Windows XP target).
2>LINK : fatal error LNK1104: cannot open file 'libboost_system-vc140-mt-gd-1_61.lib'
========== Build: 1 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

The contents of my CMakeLists.txt

cmake_minimum_required(VERSION 3.6)

project(boost-asio)

find_package(Boost 1.61.0 COMPONENTS system REQUIRED)

# set cmake variables
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}/bin)
set(Boost_USE_STATIC_LIBS OFF)
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME OFF)

# definitions
add_definitions(-DBOOST_LOG_DYN_LINK=1)

# set sources
set(SOURCES src/main.cpp)


# manage compilation and linkage
if(Boost_FOUND)
    include_directories(${Boost_INCLUDE_DIRS})
    add_executable(boost-asio ${SOURCES})
    target_link_libraries(boost-asio ${Boost_LIBRARIES})

    message(${Boost_INCLUDE_DIRS})
    message(${Boost_LIBRARIES})
endif()

Please note that the definition in the cmake source is added according to an answer in some question I checked. FindBoost finds the boost library, here is an output from cmake-gui

Boost version: 1.61.0
Found the following Boost libraries:
  system
C:/Boost/boost_1_61_0
optimizedC:/Boost/boost_1_61_0/stage/lib/boost_system-vc140-mt-1_61.libdebugC:/Boost/boost_1_61_0/stage/lib/boost_system-vc140-mt-gd-1_61.lib

Now there are two problems here. First, it can not find the library and second, although it clearly directs the linker to link dynamically, it still looks for static library. I don't know if these problems are related. How to solve this problem? How to dynamically link Boost using Cmake?

EDIT1: Here is the code I am using, if you want to test it out.

#include <iostream>
#include <boost/asio.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>

int main() {

    boost::asio::io_service ioservice;

    boost::asio::deadline_timer t(ioservice, boost::posix_time::seconds(5));

    t.wait();

    std::cout << "Hello, world!" << std::endl;

    return 0;
}
1
Boost_USE_* variables should be set before find_package(Boost) call for have an effect.Tsyvarev
Tried building after adding find_package(Boost) right before if(Boost_FOUND), it doesn't seem to have an effectCengiz Kandemir
You need to clear CMake cache for regenerate result of find_package().Tsyvarev
I deleted cache and re-configured and regenerated the project files. Still the same.Cengiz Kandemir
Shouldn't you add asio as a component in find_package() since that is what you want not just system??drescherjm

1 Answers

1
votes

Adding the following flags in your CMakeLists.txt solves the problem:

add_definitions( -DBOOST_ALL_NO_LIB )
add_definitions( -DBOOST_ALL_DYN_LINK )