与 MS 上的 Apache Portable Runtime 链接 Windows

Linking with Apache Portable Runtime on MS Windows

我正在尝试构建一个链接到 APR 的 Python 扩展。这次是在 MS Windows 上...(在 Linux 上有效)。

以下是构建失败的地方、命令和为便于阅读而格式化的输出:

C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\BIN\x86_amd64\link.exe
    /nologo
    /INCREMENTAL:NO
    /LTCG
    /DLL
    /MANIFEST:EMBED,ID=2
    /MANIFESTUAC:NO
    /LIBPATH:c:\dev\protopy\lib/apr
    /LIBPATH:c:\bin\python\Libs
    /LIBPATH:c:\dev\protopy\.venv\libs
    /LIBPATH:c:\dev\protopy\.venv\PCbuild\amd64
    "/LIBPATH:C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\LIB\amd64"
    "/LIBPATH:C:\Program Files (x86)\Windows Kits\lib.0.10240.0\ucrt\x64"
    "/LIBPATH:C:\Program Files (x86)\Windows Kits.1\lib\winv6.3\um\x64"
    apr-1.lib
    /EXPORT:PyInit_wrapped build\temp.win-amd64-3.6\Release\protopy/lib/descriptors.obj
    build\temp.win-amd64-3.6\Release\protopy/lib/binparser.obj
    build\temp.win-amd64-3.6\Release\protopy/lib/defparser.obj
    build\temp.win-amd64-3.6\Release\protopy/lib/protopy.lex.obj
    build\temp.win-amd64-3.6\Release\protopy/lib/protopy.tab.obj
    build\temp.win-amd64-3.6\Release\protopy/lib/pyhelpers.obj
    build\temp.win-amd64-3.6\Release\protopy/lib/list.obj
    build\temp.win-amd64-3.6\Release\protopy/wrapper.obj
    /OUT:build\lib.win-amd64-3.6\protopy\wrapped.cp36-win_amd64.pyd
    /IMPLIB:build\temp.win-amd64-3.6\Release\protopy/lib\wrapped.cp36-win_amd64.lib

wrapper.obj : warning LNK4197: export 'PyInit_wrapped' specified multiple times; using first specification
   Creating library build\temp.win-amd64-3.6\Release\protopy/lib\wrapped.cp36-win_amd64.lib \
   and object build\temp.win-amd64-3.6\Release\protopy/lib\wrapped.cp36-win_amd64.exp
descriptors.obj : error LNK2001: unresolved external symbol \
    __imp_apr_hash_set

(它实际上错过了我用过的所有符号,而不仅仅是这个)。

我之前在APR的顶层目录下运行nmake -f Makefile.win建了apr-1.lib,就这样制作了libapr-1.lib。然后我把它重命名为apr-1.lib,放在c:\dev\protopy\lib/apr里,所以链接器其实是能找到的,但是找不到需要的符号...

问题 1

如何查看库中有哪些符号? (也许我编译错了什么?)


好的,我找到了 dumpbin.exe,但现在我看到 apr-1.lib 中的符号称为 __imp__apr_hash_set(注意第二个双下划线),但链接器查找 __imp_apr_hash_set(单下划线)。是什么赋予了?它们是用相同的编译器/链接器编译的...

问题 2

是否有任何特定于在 MS Windows 上构建 Python 扩展,使其以这种方式工作? (我这辈子只试过一次,没能成功,但原因不同)。

根据 APR 项目文件

choose either aprutil or libaprutil (for static or dynamic libraries)

表示apr-1.lib-->staticlibapr-1.lib -->dynamic

To target the static .lib versions of the library, the consuming compiliation must define the macros APR_DECLARE_STATIC and APU_DECLARE_STATIC. This prevents the apr and apr-util symbols from being tagged as __declspec(dllimport), eliminating compiliation warnings and speeding up execution.