在Windows上将boost库与Boost_USE_STATIC_LIB关闭

Ask*_*ken 10 c++ boost cmake dynamic-linking visual-studio-2013

我的CMakeFiles.txt看起来像这样:

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()
Run Code Online (Sandbox Code Playgroud)

我为Visual Studio 2013 64位渲染项目:

cmake -G "Visual Studio 12 Win64" -DBOOST_LIBRARYDIR=D:\Development\Tools\boost_1_57_0\stage\x64\lib ..\KServer
Run Code Online (Sandbox Code Playgroud)

输出是:

-- 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
Run Code Online (Sandbox Code Playgroud)

这一切都很好,很好.

问题从这里开始:

当我更改我的cmake文件使用时:

set(Boost_USE_STATIC_LIBS OFF)
Run Code Online (Sandbox Code Playgroud)

然后,我在构建时在Visual Studio中收到以下错误:

error LNK1104: cannot open file 'libboost_filesystem-vc120-mt-gd-1_57.lib'  D:\Development\Private\C++\KServerProject\src\LINK  foo
Run Code Online (Sandbox Code Playgroud)

检查Property Pages工作室中的库是作为依赖项添加的:

在此输入图像描述

手动添加文件夹D:\Development\Tools\boost_1_57_0\stage\x64\libAdditional Library Directories,构建正常.

如何使用动态库创建项目?

Phi*_*hil 16

我相信你需要补充一下

add_definitions( -DBOOST_ALL_NO_LIB )
Run Code Online (Sandbox Code Playgroud)

请参阅http://www.boost.org/doc/libs/1_57_0/libs/config/doc/html/index.html.我把它设置在我的CMakeLists.txt中,它适用于我的视觉工作室构建与增强.作为测试,我删除它并得到你做的同样的错误.

对于它的价值,这里是我如何使用cmake的boost.

# 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()
Run Code Online (Sandbox Code Playgroud)