在 Windows 上使用 Boost_USE_STATIC_LIB OFF 链接 boost 库

Linking boost library with Boost_USE_STATIC_LIB OFF on Windows(在 Windows 上使用 Boost_USE_STATIC_LIB OFF 链接 boost 库)

本文介绍了在 Windows 上使用 Boost_USE_STATIC_LIB OFF 链接 boost 库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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()

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

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

cmake -G "Visual Studio 12 Win64" -DBOOST_LIBRARYDIR=D:DevelopmentToolsoost_1_57_0stagex64lib ..KServer

输出为:

-- 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

这一切都很好.

问题从这里开始:

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

When I change my cmake file to use:

set(Boost_USE_STATIC_LIBS OFF)

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

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:DevelopmentPrivateC++KServerProjectsrcLINK  foo

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

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

当手动添加文件夹 D:DevelopmentToolsoost_1_57_0stagex64libAdditional Library Directory 时,它构建得很好.

When manually adding the folder D:DevelopmentToolsoost_1_57_0stagex64lib to Additional Library Directories it builds fine.

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

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

推荐答案

我相信你需要补充

add_definitions( -DBOOST_ALL_NO_LIB )

参见 http://www.boost.org/doc/libs/1_57_0/libs/config/doc/html/index.html.我在我的 CMakeLists.txt 中设置了它,它适用于我的视觉工作室构建.作为测试,我删除了它并得到与您相同的错误.

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.

就其价值而言,以下是我将 boost 与 cmake 结合使用的方法.

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()

这篇关于在 Windows 上使用 Boost_USE_STATIC_LIB OFF 链接 boost 库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:在 Windows 上使用 Boost_USE_STATIC_LIB OFF 链接 boost 库

基础教程推荐