Linking boost library with Boost_USE_STATIC_LIB OFF on Windows
Asked Answered
W

1

17

My CMakeFiles.txt looks like this:

cmake_minimum_required ( VERSION 2.6 )

# Set warnings on and enable debugging
SET( CMAKE_C_FLAGS "-Wall -q" )

include(FindBoost)

set(Boost_USE_STATIC_LIBS ON)
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME OFF)

find_package( Boost 1.57.0 COMPONENTS system filesystem REQUIRED )

if( Boost_FOUND )
    message( STATUS "Boost found!" )
    include_directories(${Boost_INCLUDE_DIRS})
    add_executable(foo main.cpp)

    # Needed for asio
    if(WIN32)
      target_link_libraries(foo wsock32 ws2_32)
    endif()

    target_link_libraries(foo ${Boost_LIBRARIES})
endif()

I render the project for Visual Studio 2013 64-bit:

cmake -G "Visual Studio 12 Win64" -DBOOST_LIBRARYDIR=D:\Development\Tools\boost_1_57_0\stage\x64\lib ..\KServer

The output is:

-- The C compiler identification is MSVC 18.0.31101.0
-- The CXX compiler identification is MSVC 18.0.31101.0
-- Check for working C compiler using: Visual Studio 12 2013 Win64
-- Check for working C compiler using: Visual Studio 12 2013 Win64 -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler using: Visual Studio 12 2013 Win64
-- Check for working CXX compiler using: Visual Studio 12 2013 Win64 -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Boost version: 1.57.0
-- Boost version: 1.57.0
-- Found the following Boost libraries:
--   system
--   filesystem
-- Boost found!
-- Configuring done
-- Generating done
-- Build files have been written to: D:/Development/Private/C++/KServerProject

This is all good and well.

Problem starts here:

When I change my cmake file to use:

set(Boost_USE_STATIC_LIBS OFF)

I then get the following error in Visual Studio when building:

error LNK1104: cannot open file 'libboost_filesystem-vc120-mt-gd-1_57.lib'  D:\Development\Private\C++\KServerProject\src\LINK  foo

Checking the Property Pages in the studio the library is added as a dependency:

enter image description here

When manually adding the folder D:\Development\Tools\boost_1_57_0\stage\x64\lib to Additional Library Directories it builds fine.

How can I get it to create project using dynamic libs?

Walli answered 5/3, 2015 at 21:4 Comment(7)
In my boost build that I use with cmake I have 3 folders in the root of the build. build, include and lib. Although I build out of source.Lynch
I use the following build-type with bjam --build-type=complete stage installLynch
It's not a problem with boost. I've built with complete. I need the project using boost to work with dynamic linking when generating using cmake.Walli
I thought the problem was the path but it looks like the problem is the naming convention. boost_filesystem-vc120-mt-gd-1_57.lib versus libboost_filesystem-vc120-mt-gd-1_57.libLynch
cmake.org/pipermail/cmake/2008-July/022740.htmlLynch
I've heard about that one and I guess it could be but why does it work with static and not dymanic?Walli
In my experience Boost's auto-linking in general works very poorly in combination with CMake, regardless of the build type. You should always switch it off completely (by setting the BOOST_ALL_NO_LIB preprocessor flag) and rely solely on CMake for linking the correct libraries.Annamarieannamese
M
23

I believe you need to add

add_definitions( -DBOOST_ALL_NO_LIB )

See http://www.boost.org/doc/libs/1_57_0/libs/config/doc/html/index.html. I have it set in my CMakeLists.txt and it works for my visual studio builds with boost. As a test, I removed it and got the same error you did.

For what it is worth, here is how I use boost with cmake.

# boost
set(Boost_NO_SYSTEM_PATHS true)
set (Boost_USE_STATIC_LIBS OFF CACHE BOOL "use static libraries from Boost")
set (Boost_USE_MULTITHREADED ON)
find_package(Boost REQUIRED 
  COMPONENTS
  system program_options thread filesystem
  date_time chrono timer regex serialization
  )
include_directories(${Boost_INCLUDE_DIRS})
link_libraries(${Boost_LIBRARIES})

if (WIN32)
  # disable autolinking in boost
  add_definitions( -DBOOST_ALL_NO_LIB )

  # force all boost libraries to dynamic link (we already disabled
  # autolinking, so I don't know why we need this, but we do!)
  add_definitions( -DBOOST_ALL_DYN_LINK )
endif()
Monkery answered 6/3, 2015 at 15:39 Comment(1)
WOW! that works so well......! I don't need BOOST_ALL_DYN_LINK in my case: github.com/lumenitb/vpercnodePhysiotherapy

© 2022 - 2024 — McMap. All rights reserved.