为什么升压Python库的静态版本对Windows的依赖到Python?(Why does stat

2019-10-16 12:53发布

我正在开发一个Python ++库使用Boost Python的一个C绑定。 为了避免分配Boost库的问题,我使用的是静态版本。 它工作在Linux和MacOSX的罚款,但在Windows中,仍需要Python的依赖。 我CMakeLists.txt设置为:

if((${CMAKE_SYSTEM_NAME} STREQUAL "Linux") OR APPLE)
     target_link_libraries(my_python_module ${Boost_LIBRARIES})
elseif(WIN32 AND MSVC)
    add_definitions(/DBOOST_PYTHON_STATIC_LIB) # This is also required to force the usage of libboost_python37-vc141-mt-x64-1_70.lib instead of boost_python37-vc141-mt-x64-1_70.lib
    target_link_libraries(my_python_module ${Boost_LIBRARIES}) # This includes the Boost Python library
    # Even though Boost Python library is included statically, in Windows it has a dependency to the Python library.
    target_link_libraries(my_python_module ${Python_LIBRARIES})
endif()

因此,因为在Windows中,如果删除target_link_libraries(my_python_module ${Python_LIBRARIES})线,我得到一个:

LINK : fatal error LNK1104: Can't open file 'python37.lib'

我得到的DLL的确已经Python作为一个依赖关系:

C:\PythonModule\bin\Release\Release>"C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\bin\Hostx64\x64\dumpbin.exe" /DEPENDENTS my_python_module.dll
Microsoft (R) COFF/PE Dumper Version 14.16.27031.1
Copyright (C) Microsoft Corporation.  All rights reserved.


Dump of file my_python_module.dll

File Type: DLL

  Image has the following dependencies:

    python37.dll <------- HERE
    MSVCP140.dll
    VCRUNTIME140.dll
    api-ms-win-crt-runtime-l1-1-0.dll
    api-ms-win-crt-heap-l1-1-0.dll
    KERNEL32.dll

  Summary

        B000 .data
        3000 .pdata
       18000 .rdata
        1000 .reloc
        1000 .rsrc
       27000 .text

由于我使用升压静态库,该过程针对链接libboost_python37-vc141-mt-x64-1_70.lib ,不boost_python37-vc141-mt-x64-1_70.lib (其相关联的动态库的库模块, boost_python37-vc141-mt-x64-1_70.dll )。 我可以传递到Visual Studio中的命令行上检查这个(感谢-DCMAKE_VERBOSE_MAKEFILE:BOOL=ON ):

Link:
  C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\bin\HostX86\x64\link.exe /ERRORREPORT:QUEUE /OUT:"C:\PythonModule\bin\Release\Release\my_python_module.dll" /INCREMENTAL:NO /NOLOGO "C:\local\boost_1_70_0\lib64-msvc-14.1\libboost_filesystem-vc141-mt-x64-1_70.lib"
  "C:\local\boost_1_70_0\lib64-msvc-14.1\libboost_program_options-vc141-mt-x64-1_70.lib"
  "C:\local\boost_1_70_0\lib64-msvc-14.1\libboost_python37-vc141-mt-x64-1_70.lib"  <------- HERE
  "C:\local\boost_1_70_0\lib64-msvc-14.1\libboost_thread-vc141-mt-x64-1_70.lib"
  "C:\local\boost_1_70_0\lib64-msvc-14.1\libboost_system-vc141-mt-x64-1_70.lib"
  "C:\local\boost_1_70_0\lib64-msvc-14.1\libboost_stacktrace_windbg-vc141-mt-x64-1_70.lib"
  "C:\local\boost_1_70_0\lib64-msvc-14.1\libboost_chrono-vc141-mt-x64-1_70.lib"
  "C:\local\boost_1_70_0\lib64-msvc-14.1\libboost_date_time-vc141-mt-x64-1_70.lib"
  "C:\local\boost_1_70_0\lib64-msvc-14.1\libboost_atomic-vc141-mt-x64-1_70.lib"
  "C:\Program Files\Python37\libs\python37.lib"
  kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib /MANIFEST /MANIFESTUAC:"level='asInvoker' uiAccess='false'" /manifest:embed /PDB:"C:/PythonModule/lib/Release/Release/my_python_module.pdb" /SUBSYSTEM:CONSOLE /TLBID:1 /DYNAMICBASE /NXCOMPAT /IMPLIB:"C:/PythonModule/lib/Release/Release/my_python_module.lib" /MACHINE:X64  /machine:x64 /IGNORE:4049,4217 /DLL my_python_module.dir\Release\my_python_module.obj
     Creating library C:/PythonModule/lib/Release/Release/my_python_module.lib and object C:/PythonModule/lib/Release/Release/my_python_module.exp
  my_python_module.vcxproj -> C:\PythonModule\bin\Release\Release\my_python_module.dll

顺便说一句,这些都是官方提供的二进制文件的升压,顺便说一句,从获得https://dl.bintray.com/boostorg/release/1.70.0/binaries/boost_1_70_0-msvc-14.1-64.exe

静态升压Python库不必依赖到Python,据我可以检查:

C:\local\boost_1_70_0\lib64-msvc-14.1>"C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\bin\Hostx64\x64\dumpbin.exe" /DEPENDENTS libboost_python37-vc141-mt-x64-1_70.lib
Microsoft (R) COFF/PE Dumper Version 14.16.27031.1
Copyright (C) Microsoft Corporation.  All rights reserved.

Dump of file libboost_python37-vc141-mt-x64-1_70.lib

File Type: LIBRARY

  Summary

         100 .CRT$XCU
         35A .bss
       17EF8 .chks64
         DD0 .data
         CFE .data$r
        1804 .debug$S
        23B5 .drectve
        5B20 .pdata
        9FD3 .rdata
         EF8 .rdata$r
         4DB .text$di
       32D89 .text$mn
        3F59 .text$x
         608 .text$yd
       11C10 .xdata
         4C0 .xdata$x

虽然共享的一个作用:

C:\local\boost_1_70_0\lib64-msvc-14.1>"C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\bin\Hostx64\x64\dumpbin.exe" /DEPENDENTS boost_python37-vc141-mt-x64-1_70.dll
Microsoft (R) COFF/PE Dumper Version 14.16.27031.1
Copyright (C) Microsoft Corporation.  All rights reserved.

Dump of file boost_python37-vc141-mt-x64-1_70.dll

File Type: DLL

  Image has the following dependencies:

    python37.dll <------- HERE
    MSVCP140.dll
    KERNEL32.dll
    VCRUNTIME140.dll
    api-ms-win-crt-runtime-l1-1-0.dll
    api-ms-win-crt-heap-l1-1-0.dll

  Summary

        3000 .data
        3000 .pdata
       18000 .rdata
        1000 .reloc
        1000 .rsrc
       1D000 .text

所以,我的主要问题是:

为什么,如果我使用了Boost Python库的静态版本,我仍然有Python的依赖?

我不以任何其他方式在模块中直接使用Python,和在Linux中,这样的依赖不存在。

非常感谢你的帮助。

文章来源: Why does static version of the Boost Python library has a dependency to Python in Windows?