强制 CMake/VisualStudio 使用 Boost.Python 的静态库

Force CMake/VisualStudio to use static libs of Boost.Python

我目前正在尝试在 Windows(使用 Intel 编译器)上构建一个大型项目,该项目在 UNIX 上使用 CMake 编译得非常好。这是我的问题的简化示例。

运行 下面是使用 Boost.Python 的简单代码示例:

#include <iostream>
#include <Python.h>
#include <boost/python.hpp>

int main()
{
    std::string python_home = "C:\softs\python\2.7.9\64";
    char* python_home_char = new char[python_home.length() + 1];
    strcpy(python_home_char, python_home.c_str());
    Py_SetPythonHome(python_home_char);
    Py_Initialize();

    boost::python::object pyobj_main = boost::python::import("__main__");
    boost::python::object glob = pyobj_main.attr("__dict__");
    boost::python::exec("print \"Hello from Python\"", glob, glob);

    Py_Finalize();

    return 0;
}

我在执行代码时收到以下错误消息:

The program can't start because boost_python-iw-mt-1_57.dll is missing from your computer. Try reinstalling the program to fix this problem.

请注意,如果删除代码末尾的 3 行 boost::python::***,我不会收到错误消息。此外,如果我使用 Boost.Thread 编译以下示例,我不会有任何错误(在我看来,它也使用库(不只是 header,对吗?):

#include <iostream>
#include <boost/thread.hpp>

boost::mutex mutex_hello;
void hello(unsigned long int thread_number)
{
    boost::mutex::scoped_lock lock_hello(mutex_hello);
    std::cout << "Hello from thread " << thread_number << std::endl;
}

int main()
{
    boost::thread_group group;
    for(unsigned long int i = 0; i < 9; ++i)
        group.create_thread(boost::bind(hello, i + 1));
    group.join_all();

    return 0;
}

实际上,我不想使用共享库,我希望我的可执行文件尽可能静态化

我使用以下 CMake 文件构建此代码:

cmake_minimum_required(VERSION 2.8.9)

# Project
project(TestBoost)
enable_language(C)
enable_language(CXX)
enable_language(Fortran)

# Compiler info
if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
    set(CompilerName "${CMAKE_CXX_COMPILER_ID}")
    set(CompilerVersion "${CMAKE_CXX_COMPILER_VERSION}")
    if(CMAKE_CL_64)
        set(CompilerArch "64")
    else()
        set(CompilerArch "32")
    endif()
endif()
string(TOLOWER "${CompilerName}" CompilerName)
if("${CompilerVersion}" MATCHES "([0-9]+\.[0-9]+\.[0-9]+)\..*")
    string(REGEX REPLACE "([0-9]+\.[0-9]+\.[0-9]+)\..*" "\1" CompilerVersion "${CompilerVersion}")
endif()

# Libs
set(CMAKE_FIND_LIBRARY_SUFFIXES .lib .a ${CMAKE_FIND_LIBRARY_SUFFIXES})

# Intel
if(CompilerName STREQUAL "intel")
    string(REGEX REPLACE "(.*)/bin/.*" "\1" IntelPath "${CMAKE_C_COMPILER}")
    if(CompilerArch STREQUAL "32")
        set(IntelArchStr "ia32")
    elseif(CompilerArch STREQUAL "64")
        set(IntelArchStr "intel64")
    endif()
    set(Compiler_LIBRARY_DIRS "${IntelPath}/compiler/lib/${IntelArchStr}")
endif()

# Boost
set(BOOST_ROOT "C:/softs/boost/1.57.0/${CompilerArch}/${CompilerName}/${CompilerVersion}")
set(Boost_USE_MULTITHREAD ON)
set(Boost_USE_STATIC_LIBS ON)
set(Boost_NO_SYSTEM_PATHS ON)
set(Boost_ADDITIONAL_VERSIONS "1.57.0" "1.57")
find_package(Boost 1.57.0 REQUIRED COMPONENTS thread system filesystem python)

# Python
set(PythonPath C:/softs/python/2.7.9/${CompilerArch})
set(Python_INCLUDE_DIRS ${PythonPath}/include)
set(Python_LIBRARY_DIRS ${PythonPath}/libs)
set(Python_LIBRARIES python27)

# Executable
include_directories(SYSTEM ${Boost_INCLUDE_DIRS} ${Python_INCLUDE_DIRS})
link_directories(${Boost_LIBRARY_DIRS} ${Python_LIBRARY_DIRS} ${Compiler_LIBRARY_DIRS})
add_executable(test_boost test_boost.cpp)
target_link_libraries(test_boost ${Boost_LIBRARIES} ${Python_LIBRARIES})

我 运行 此 CMake 脚本的初始条目 CMAKE_GENERATOR_TOOLSET 设置为 Intel C++ Compiler XE 14.0,并将英特尔编译器指定为本机编译器。在 运行ning CMake 上我没有错误或警告(见下面的日志)。这会生成一个文件 TestBoost.sln。在 Visual Studio Professional 2013 中加载时,我将编译类型更改为 Release 并修改目标 test_boost 的链接器选项以添加 /NODEFAULTLIB:LIBCMT (解决方法是由于 CMake 中的错误).然后我编译没有任何错误。

从 CMake 登录:

The C compiler identification is Intel 14.0.4.20140805
The CXX compiler identification is Intel 14.0.4.20140805
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
The Fortran compiler identification is Intel
Check for working Fortran compiler using: Visual Studio 12 2013 Win64
Check for working Fortran compiler using: Visual Studio 12 2013 Win64  -- works
Detecting Fortran compiler ABI info
Detecting Fortran compiler ABI info - done
Determine Intel Fortran Compiler Implicit Link Path
Determine Intel Fortran Compiler Implicit Link Path -- done
Checking whether C:/Program Files (x86)/Intel/Composer XE 2013 SP1/bin/intel64/icl.exe supports Fortran 90
Checking whether C:/Program Files (x86)/Intel/Composer XE 2013 SP1/bin/intel64/icl.exe supports Fortran 90 -- yes
[ C:/Program Files (x86)/CMake/share/cmake-3.1/Modules/FindBoost.cmake:515 ] _boost_TEST_VERSIONS = 1.57.0;1.57
[ C:/Program Files (x86)/CMake/share/cmake-3.1/Modules/FindBoost.cmake:517 ] Boost_USE_MULTITHREADED = TRUE
[ C:/Program Files (x86)/CMake/share/cmake-3.1/Modules/FindBoost.cmake:519 ] Boost_USE_STATIC_LIBS = ON
[ C:/Program Files (x86)/CMake/share/cmake-3.1/Modules/FindBoost.cmake:521 ] Boost_USE_STATIC_RUNTIME = 
[ C:/Program Files (x86)/CMake/share/cmake-3.1/Modules/FindBoost.cmake:523 ] Boost_ADDITIONAL_VERSIONS = 1.57.0;1.57
[ C:/Program Files (x86)/CMake/share/cmake-3.1/Modules/FindBoost.cmake:525 ] Boost_NO_SYSTEM_PATHS = ON
[ C:/Program Files (x86)/CMake/share/cmake-3.1/Modules/FindBoost.cmake:577 ] Declared as CMake or Environmental Variables:
[ C:/Program Files (x86)/CMake/share/cmake-3.1/Modules/FindBoost.cmake:579 ]   BOOST_ROOT = C:/softs/boost/1.57.0/64/intel/14.0.4
[ C:/Program Files (x86)/CMake/share/cmake-3.1/Modules/FindBoost.cmake:581 ]   BOOST_INCLUDEDIR = 
[ C:/Program Files (x86)/CMake/share/cmake-3.1/Modules/FindBoost.cmake:583 ]   BOOST_LIBRARYDIR = 
[ C:/Program Files (x86)/CMake/share/cmake-3.1/Modules/FindBoost.cmake:585 ] _boost_TEST_VERSIONS = 1.57.0;1.57
[ C:/Program Files (x86)/CMake/share/cmake-3.1/Modules/FindBoost.cmake:654 ] Include debugging info:
[ C:/Program Files (x86)/CMake/share/cmake-3.1/Modules/FindBoost.cmake:656 ]   _boost_INCLUDE_SEARCH_DIRS = C:/softs/boost/1.57.0/64/intel/14.0.4/include;C:/softs/boost/1.57.0/64/intel/14.0.4;NO_CMAKE_SYSTEM_PATH
[ C:/Program Files (x86)/CMake/share/cmake-3.1/Modules/FindBoost.cmake:658 ]   _boost_PATH_SUFFIXES = boost-1_57_0;boost_1_57_0;boost/boost-1_57_0;boost/boost_1_57_0;boost-1_57;boost_1_57;boost/boost-1_57;boost/boost_1_57
[ C:/Program Files (x86)/CMake/share/cmake-3.1/Modules/FindBoost.cmake:678 ] location of version.hpp: C:/softs/boost/1.57.0/64/intel/14.0.4/include/boost-1_57/boost/version.hpp
[ C:/Program Files (x86)/CMake/share/cmake-3.1/Modules/FindBoost.cmake:702 ] version.hpp reveals boost 1.57.0
[ C:/Program Files (x86)/CMake/share/cmake-3.1/Modules/FindBoost.cmake:787 ] guessed _boost_COMPILER = -iw
[ C:/Program Files (x86)/CMake/share/cmake-3.1/Modules/FindBoost.cmake:797 ] _boost_MULTITHREADED = -mt
[ C:/Program Files (x86)/CMake/share/cmake-3.1/Modules/FindBoost.cmake:840 ] _boost_RELEASE_ABI_TAG = -
[ C:/Program Files (x86)/CMake/share/cmake-3.1/Modules/FindBoost.cmake:842 ] _boost_DEBUG_ABI_TAG = -gd
[ C:/Program Files (x86)/CMake/share/cmake-3.1/Modules/FindBoost.cmake:890 ] _boost_LIBRARY_SEARCH_DIRS = C:/softs/boost/1.57.0/64/intel/14.0.4/lib;C:/softs/boost/1.57.0/64/intel/14.0.4/stage/lib;C:/softs/boost/1.57.0/64/intel/14.0.4/include/boost-1_57/lib;C:/softs/boost/1.57.0/64/intel/14.0.4/include/boost-1_57/../lib;C:/softs/boost/1.57.0/64/intel/14.0.4/include/boost-1_57/stage/lib;NO_CMAKE_SYSTEM_PATH
[ C:/Program Files (x86)/CMake/share/cmake-3.1/Modules/FindBoost.cmake:1001 ] Searching for THREAD_LIBRARY_RELEASE: libboost_thread-iw-mt-1_57;libboost_thread-iw-mt;libboost_thread-mt-1_57;libboost_thread-mt;libboost_thread;libboost_thread-iw-mt-s-1_57;libboost_thread-iw-mt-s;libboost_thread-mt-s-1_57;libboost_thread-mt-s
[ C:/Program Files (x86)/CMake/share/cmake-3.1/Modules/FindBoost.cmake:1037 ] Searching for THREAD_LIBRARY_DEBUG: libboost_thread-iw-mt-gd-1_57;libboost_thread-iw-mt-gd;libboost_thread-mt-gd-1_57;libboost_thread-mt-gd;libboost_thread-mt;libboost_thread;libboost_thread-iw-mt-s-gd-1_57;libboost_thread-iw-mt-s-gd;libboost_thread-mt-s-gd-1_57;libboost_thread-mt-s-gd
[ C:/Program Files (x86)/CMake/share/cmake-3.1/Modules/FindBoost.cmake:1001 ] Searching for SYSTEM_LIBRARY_RELEASE: libboost_system-iw-mt-1_57;libboost_system-iw-mt;libboost_system-mt-1_57;libboost_system-mt;libboost_system;libboost_system-iw-mt-s-1_57;libboost_system-iw-mt-s;libboost_system-mt-s-1_57;libboost_system-mt-s
[ C:/Program Files (x86)/CMake/share/cmake-3.1/Modules/FindBoost.cmake:1037 ] Searching for SYSTEM_LIBRARY_DEBUG: libboost_system-iw-mt-gd-1_57;libboost_system-iw-mt-gd;libboost_system-mt-gd-1_57;libboost_system-mt-gd;libboost_system-mt;libboost_system;libboost_system-iw-mt-s-gd-1_57;libboost_system-iw-mt-s-gd;libboost_system-mt-s-gd-1_57;libboost_system-mt-s-gd
[ C:/Program Files (x86)/CMake/share/cmake-3.1/Modules/FindBoost.cmake:1001 ] Searching for FILESYSTEM_LIBRARY_RELEASE: libboost_filesystem-iw-mt-1_57;libboost_filesystem-iw-mt;libboost_filesystem-mt-1_57;libboost_filesystem-mt;libboost_filesystem;libboost_filesystem-iw-mt-s-1_57;libboost_filesystem-iw-mt-s;libboost_filesystem-mt-s-1_57;libboost_filesystem-mt-s
[ C:/Program Files (x86)/CMake/share/cmake-3.1/Modules/FindBoost.cmake:1037 ] Searching for FILESYSTEM_LIBRARY_DEBUG: libboost_filesystem-iw-mt-gd-1_57;libboost_filesystem-iw-mt-gd;libboost_filesystem-mt-gd-1_57;libboost_filesystem-mt-gd;libboost_filesystem-mt;libboost_filesystem;libboost_filesystem-iw-mt-s-gd-1_57;libboost_filesystem-iw-mt-s-gd;libboost_filesystem-mt-s-gd-1_57;libboost_filesystem-mt-s-gd
[ C:/Program Files (x86)/CMake/share/cmake-3.1/Modules/FindBoost.cmake:1001 ] Searching for PYTHON_LIBRARY_RELEASE: libboost_python-iw-mt-1_57;libboost_python-iw-mt;libboost_python-mt-1_57;libboost_python-mt;libboost_python;libboost_python-iw-mt-s-1_57;libboost_python-iw-mt-s;libboost_python-mt-s-1_57;libboost_python-mt-s
[ C:/Program Files (x86)/CMake/share/cmake-3.1/Modules/FindBoost.cmake:1037 ] Searching for PYTHON_LIBRARY_DEBUG: libboost_python-iw-mt-gd-1_57;libboost_python-iw-mt-gd;libboost_python-mt-gd-1_57;libboost_python-mt-gd;libboost_python-mt;libboost_python;libboost_python-iw-mt-s-gd-1_57;libboost_python-iw-mt-s-gd;libboost_python-mt-s-gd-1_57;libboost_python-mt-s-gd
[ C:/Program Files (x86)/CMake/share/cmake-3.1/Modules/FindBoost.cmake:1088 ] Boost_FOUND = 1
Boost version: 1.57.0
Found the following Boost libraries:
  thread
  system
  filesystem
  python
Configuring done

如何强制编译只使用静态库?我以为是CMake文件中的选项。此外,它正在处理使用线程的示例...... 也许 Boost.Python 安装有问题?

您的 finding dll's on your machine. As a temporary fix 似乎有问题,您可以尝试将您的 .dll 文件重命名为 .pyd。

使用Boost.Python时,默认配置是动态链接。要强制静态链接,请在编译中定义 BOOST_PYTHON_STATIC_LIB。考虑:

  • 在 CMake
  • 中添加 add_definitions(-DBOOST_PYTHON_STATIC_LIB)
  • 在之前添加#define BOOST_PYTHON_STATIC_LIB包括boost/python.hpp
  • boost/config/user.hpp
  • 中添加#define BOOST_PYTHON_STATIC_LIB

Boost.Python 是 Boost_USE_STATIC_LIBS CMake 变量的一个例外。 FindBoost documentation 注释:

On Visual Studio and Borland compilers Boost headers request automatic linking to corresponding libraries. [...] Boost automatic linking typically requests static libraries with a few exceptions (such as Boost.Python).

遗憾的是,BOOST_PYTHON_STATIC_LIB 选项未在 Boost user settable options, Choosing a Boost.Python Library Binary, nor Boost.Python Configuration Information 文档中列出。


根据您的示例代码提出的其他一些建议:

  • 根据 Boost.Python 的 embedding documentation,不要调用 Py_Finalize()。某些内部 Boost.Python objects 将在 Py_Finalize() 期间保持活动状态,并且仅在 Boost.Python 卸载时才尝试删除,导致 objects 尝试使用 non-existent 解释器。
  • 不要直接包含,python.h。如果必须,请考虑包括 boost/python/detail/wrap_python.hpp
  • 不要在 python.h(或 Boost.Python 文件)之前包含任何系统 headers。此限制由 Python 强加(参见 here)。

后两项建议记录在 #include issues 部分。