2

首先要做的事情:Windows 7 x64 使用 Microsoft CL.exe 命令行编译器和 CMake,在 amd64 Visual Studio 命令行上正确配置。我已经解决了一些基本问题(例如,我试图链接共享而不是静态 Boost,我试图链接 x86 Boost 与 x64 目标等),现在我已经弄清楚了,我是试图围绕 Boost 的 CMake 标志。

我知道 Boost 的不同命名法(比如 -mt-gd 的意思。)我的 Boost 库都是 -mt-s (发布多线程。)

我想知道 FindBoost 的哪个 CMake 变量对应于 -g 和 -d 后缀。

这是我的 CMakeLists:

cmake_minimum_required( VERSION 2.8 )

project( echoserv )

set( APP_NAME Echoserv )
set( MAIN_SOURCE echoserv.cpp )
set( CMAKE_BUILD_TYPE "RELEASE" )

set( Boost_USE_STATIC_RUNTIME ON )
set( Boost_USE_STATIC_LIBS ON )
set( Boost_DEBUG ON )

find_package( Boost COMPONENTS system REQUIRED )

add_executable( ${APP_NAME} ${MAIN_SOURCE} )

include_directories( ${Boost_INCLUDE_DIR} )
link_directories( ${Boost_LIBRARY_DIRS} )
target_link_libraries( ${APP_NAME} ${Boost_LIBRARIES} )

我尝试编译时产生的错误是:

LINK : fatal error LNK1104: cannot open file 'libboost_system-vc100-mt-gd-1_53.lib'

这是因为我的 Boost 库都是“-vc100-mt-s-1_53.lib”的形式。那么我应该使用什么标志来让它与 -mt-s 库链接并停止尝试链接到 -mt-gd?

资源:

http://www.boost.org/doc/libs/1_53_0/more/getting_started/windows.html#library-naming http://www.cmake.org/cmake/help/v2.8.8/cmake.html#module %3aFindBoost

显然 cmake.org 链接应该有用,但我认为我正确设置了变量,它仍在尝试链接 mt-gd。

编辑:这是设置 Boost_DEBUG ON 的输出:

(请注意,Boost 库安装到 %LIB%。)

E:\fastmeta\dev\workspace\personal\learncpp\socks\echoserv>nmake

Microsoft (R) Program Maintenance Utility Version 10.00.30319.01
Copyright (C) Microsoft Corporation.  All rights reserved.

-- [ E:/fastmeta/dev/tools/cmake/CMake 2.8/share/cmake-2.8/Modules/FindBoost.cmake:566 ] _boost_TEST_VERSIONS = 1.56.0;1.56;1.55.0;1.55;1.54.0;1.54;1.53.0;1.53;1.52.0;1.52;1.51.0;1.51;1.50.0;1.50;1.49.0;1.49;1.48.0;1.48;1.47.0;1.47;1.46.1;1.46.0;1.46;1.45.0;1.45;1.44.0;1.44;1.43.0;1.43;1.42.0;1.42;1.41.0;1.41;1.40.0;1.40;1.39.0;1.39;1.38.0;1.38;1.37.0;1.37;1.36.1;1.36.0;1.36;1.35.1;1.35.0;1.35;1.34.1;1.34.0;1.34;1.33.1;1.33.0;1.33
-- [ E:/fastmeta/dev/tools/cmake/CMake 2.8/share/cmake-2.8/Modules/FindBoost.cmake:568 ] Boost_USE_MULTITHREADED = TRUE
-- [ E:/fastmeta/dev/tools/cmake/CMake 2.8/share/cmake-2.8/Modules/FindBoost.cmake:570 ] Boost_USE_STATIC_LIBS = ON
-- [ E:/fastmeta/dev/tools/cmake/CMake 2.8/share/cmake-2.8/Modules/FindBoost.cmake:572 ] Boost_USE_STATIC_RUNTIME = ON
-- [ E:/fastmeta/dev/tools/cmake/CMake 2.8/share/cmake-2.8/Modules/FindBoost.cmake:574 ] Boost_ADDITIONAL_VERSIONS =
-- [ E:/fastmeta/dev/tools/cmake/CMake 2.8/share/cmake-2.8/Modules/FindBoost.cmake:576 ] Boost_NO_SYSTEM_PATHS =
-- [ E:/fastmeta/dev/tools/cmake/CMake 2.8/share/cmake-2.8/Modules/FindBoost.cmake:644 ] Declared as CMake or Environmental Variables:
-- [ E:/fastmeta/dev/tools/cmake/CMake 2.8/share/cmake-2.8/Modules/FindBoost.cmake:646 ]   BOOST_ROOT =
-- [ E:/fastmeta/dev/tools/cmake/CMake 2.8/share/cmake-2.8/Modules/FindBoost.cmake:648 ]   BOOST_INCLUDEDIR =
-- [ E:/fastmeta/dev/tools/cmake/CMake 2.8/share/cmake-2.8/Modules/FindBoost.cmake:650 ]   BOOST_LIBRARYDIR =
-- [ E:/fastmeta/dev/tools/cmake/CMake 2.8/share/cmake-2.8/Modules/FindBoost.cmake:652 ] _boost_TEST_VERSIONS = 1.56.0;1.56;1.55.0;1.55;1.54.0;1.54;1.53.0;1.53;1.52.0;1.52;1.51.0;1.51;1.50.0;1.50;1.49.0;1.49;1.48.0;1.48;1.47.0;1.47;1.46.1;1.46.0;1.46;1.45.0;1.45;1.44.0;1.44;1.43.0;1.43;1.42.0;1.42;1.41.0;1.41;1.40.0;1.40;1.39.0;1.39;1.38.0;1.38;1.37.0;1.37;1.36.1;1.36.0;1.36;1.35.1;1.35.0;1.35;1.34.1;1.34.0;1.34;1.33.1;1.33.0;1.33
-- [ E:/fastmeta/dev/tools/cmake/CMake 2.8/share/cmake-2.8/Modules/FindBoost.cmake:734 ] location of version.hpp: C:/Program Files (x86)/Microsoft Visual Studio 10.0/VC/include/boost/version.hpp
-- [ E:/fastmeta/dev/tools/cmake/CMake 2.8/share/cmake-2.8/Modules/FindBoost.cmake:754 ] version.hpp reveals boost 1.53.0
-- [ E:/fastmeta/dev/tools/cmake/CMake 2.8/share/cmake-2.8/Modules/FindBoost.cmake:786 ] guessed _boost_COMPILER = -vc100
-- [ E:/fastmeta/dev/tools/cmake/CMake 2.8/share/cmake-2.8/Modules/FindBoost.cmake:796 ] _boost_MULTITHREADED = -mt
-- [ E:/fastmeta/dev/tools/cmake/CMake 2.8/share/cmake-2.8/Modules/FindBoost.cmake:839 ] _boost_RELEASE_ABI_TAG = -s
-- [ E:/fastmeta/dev/tools/cmake/CMake 2.8/share/cmake-2.8/Modules/FindBoost.cmake:841 ] _boost_DEBUG_ABI_TAG = -sgd
-- [ E:/fastmeta/dev/tools/cmake/CMake 2.8/share/cmake-2.8/Modules/FindBoost.cmake:884 ] _boost_LIBRARY_SEARCH_DIRS = C:/Program Files (x86)/Microsoft Visual Studio 10.0/VC/include/lib;C:/Program Files (x86)/Microsoft Visual Studio 10.0/VC/include/../lib;C:/Program Files (x86)/Microsoft Visual Studio 10.0/VC/include/stage/lib;C:/boost/lib;C:/boost;C:\Program Files (x86)/boost/boost_1_53_0/lib;C:\Program Files (x86)/boost/boost_1_53/lib;C:\Program Files (x86)/boost/lib;C:\Program Files (x86)/boost;/sw/local/lib
-- [ E:/fastmeta/dev/tools/cmake/CMake 2.8/share/cmake-2.8/Modules/FindBoost.cmake:962 ] Searching for SYSTEM_LIBRARY_RELEASE: libboost_system-vc100-mt-s-1_53;libboost_system-vc100-mt-s;libboost_system-mt-s-1_53;libboost_system-mt-s;libboost_system
-- [ E:/fastmeta/dev/tools/cmake/CMake 2.8/share/cmake-2.8/Modules/FindBoost.cmake:994 ] Searching for SYSTEM_LIBRARY_DEBUG: libboost_system-vc100-mt-sgd-1_53;libboost_system-vc100-mt-sgd;libboost_system-mt-sgd-1_53;libboost_system-mt-sgd;libboost_system-mt;libboost_system
-- [ E:/fastmeta/dev/tools/cmake/CMake 2.8/share/cmake-2.8/Modules/FindBoost.cmake:1108 ] Boost_FOUND = TRUE
-- Boost version: 1.53.0
-- Found the following Boost libraries:
--   system
-- Configuring done
-- Generating done
-- Build files have been written to: E:/fastmeta/dev/workspace/personal/learncpp/socks/echoserv
Linking CXX executable Echoserv.exe
LINK : fatal error LNK1104: cannot open file 'libboost_system-vc100-mt-1_53.lib'

LINK failed. with 2
NMAKE : fatal error U1077: '"E:\fastmeta\dev\tools\cmake\CMake 2.8\bin\cmake.exe
"' : return code '0xffffffff'
Stop.
NMAKE : fatal error U1077: '"C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\BIN\amd64\nmake.exe"' : return code '0x2'
Stop.
NMAKE : fatal error U1077: '"C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\BIN\amd64\nmake.exe"' : return code '0x2'
Stop.
4

1 回答 1

2

在 Windows 上,如果您同时构建了这两个版本,则无法告诉 CMake 仅查找 Boost 的发布版本。在 Windows 上,如果您计划在 Debug 和 Release 中构建项目,您通常也会构建 Boost 的 Debug 和 Release 版本,因为它们每个都链接到不同版本的 C 运行时库;即通常你不仅有 libboost_system-vc100-mt-s-1_53.lib,而且 libboost_system-vc100-mt-sgd-1_53.lib 在同一个文件夹中。

因此,对于您的 CMakeLists.txt,就像现在一样(假设正确找到了 boost 根目录),值${Boost_LIBRARIES}应该类似于:

optimized;<path to boost root>/stage/lib/libboost_system-vc100-mt-s-1_53.lib;debug;<path to boost root>/stage/lib/libboost_system-vc100-mt-sgd-1_53.lib

然后,当您这样做时,您会说要链接哪个版本的 boost lib,具体取决于它是 Debug 还是 Release 构建。target_link_libraries( ${APP_NAME} ${Boost_LIBRARIES} )

VS 正在寻找 libboost_system-vc100-mt-gd-1_53.lib 的事实让我认为这可能是由于 boost 的自动链接功能。当然set(Boost_USE_STATIC_RUNTIME ON),CMake 甚至不应该寻找 libboost_system-vc100-mt-gd-1_53.lib。

您可以通过添加以下行来关闭 Boost 的自动链接功能:

add_definitions(-DBOOST_ALL_NO_LIB)
于 2013-05-05T14:49:52.210 回答