【发布时间】:2020-12-24 05:58:30
【问题描述】:
我正在学习 Cython,并尝试运行此处找到的一个简单示例:making C library callable。
我使用 VS 2019 创建了 mylib.lib 和 setup.py 来构建 Cython 扩展(详细信息和代码见下文),但是链接器失败并出现错误:
错误 LNK2001:无法解析的外部符号 hello
然而,当我运行在其他帖子中发现的 nm mylib.lib 时,我可以看到符号 _hello 存在:
D:\Codes\git_folders\my_repository\Cython_test\lib>nm mylib.lib
...
Debug/examples.obj:
...
00000000 T _hello
...
出了什么问题?
代码:
pyexamples.pyx
cdef extern from "examples.h":
void hello(const char *name)
def py_hello(name: bytes) -> None:
hello(name)
setup.py
from distutils.core import setup
from distutils.extension import Extension
from Cython.Build import cythonize
examples_extension = Extension(
name="pyexamples",
sources=["pyexamples.pyx"],
libraries=["mylib"],
library_dirs=["lib"],
include_dirs=["lib"]
)
setup(
name="pyexamples",
ext_modules=cythonize([examples_extension])
)
examples.c
#include <stdio.h>
#include "examples.h"
void hello(const char *name) {
printf("hello %s\n", name);
}
examples.h
#ifndef EXAMPLES_H
#define EXAMPLES_H
void hello(const char *name);
#endif
但是,如果我运行此命令,
python setup.py build_ext --inplace
我收到了这个错误。
running build_ext
building 'pyexamples' extension
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.27.29110\bin\HostX86\x64\cl.exe /c /nologo /Ox /W3 /GL /DNDEBUG /MD -Ilib -IC:\Users\swsyo\anaconda3\include -IC:\Users\swsyo\anaconda3\include "-IC:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.27.29110\ATLMFC\include" "-IC:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.27.29110\include" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\ucrt" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\shared" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\um" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\winrt" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\cppwinrt" /Tcpyexamples.c /Fobuild\temp.win-amd64-3.7\Release\pyexamples.obj
pyexamples.c
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.27.29110\bin\HostX86\x64\link.exe /nologo /INCREMENTAL:NO /LTCG /DLL /MANIFEST:EMBED,ID=2 /MANIFESTUAC:NO /LIBPATH:lib /LIBPATH:C:\Users\swsyo\anaconda3\libs /LIBPATH:C:\Users\swsyo\anaconda3\PCbuild\amd64 "/LIBPATH:C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.27.29110\ATLMFC\lib\x64" "/LIBPATH:C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.27.29110\lib\x64" "/LIBPATH:C:\Program Files (x86)\Windows Kits\10\lib\10.0.18362.0\ucrt\x64" "/LIBPATH:C:\Program Files (x86)\Windows Kits\10\lib\10.0.18362.0\um\x64" mylib.lib /EXPORT:PyInit_pyexamples build\temp.win-amd64-3.7\Release\pyexamples.obj /OUT:D:\git_folders\my_repository\Cython_test\pyexamples.cp37-win_amd64.pyd /IMPLIB:build\temp.win-amd64-3.7\Release\pyexamples.cp37-win_amd64.lib
Creating library build\temp.win-amd64-3.7\Release\pyexamples.cp37-win_amd64.lib and object build\temp.win-amd64-3.7\Release\pyexamples.cp37-win_amd64.exp
pyexamples.obj : error LNK2001: unresolved external symbol hello
D:\git_folders\my_repository\Cython_test\pyexamples.cp37-win_amd64.pyd : fatal error LNK1120: 1 unresolved externals
error: command 'C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Community\\VC\\Tools\\MSVC\\14.27.29110\\bin\\HostX86\\x64\\link.exe' failed with exit status 1120
更多详情:
基于下面的 cmets,我运行了 dumpbin mylib.lib 命令来查看库中是否有 hello 函数。这是正确的方法吗?不过,我在此摘要中没有看到名称 hello。
D:\Codes\git_folders\my_repository\Cython_test\lib>dumpbin mylib.lib
Microsoft (R) COFF/PE Dumper Version 14.27.29111.0
Copyright (C) Microsoft Corporation. All rights reserved.
Dump of file mylib.lib
File Type: LIBRARY
Summary
E8 .chks64
C94 .debug$S
C8 .debug$T
10A .drectve
5 .msvcjmc
A .rdata
8 .rtc$IMZ
8 .rtc$TMZ
1B4 .text$mn
我还运行了在其他帖子中找到的nm mylib.lib。您可以在摘要末尾看到_hello。
D:\Codes\git_folders\my_repository\Cython_test\lib>nm mylib.lib
Debug\mylib.obj:
00000000 N .chks64
00000000 N .debug$S
00000000 N .debug$S
00000000 N .debug$S
00000000 N .debug$T
00000000 i .drectve
00000000 d .msvcjmc
00000000 r .rtc$IMZ
00000000 r .rtc$TMZ
00000000 t .text$mn
00000000 t .text$mn
U @__CheckForDebuggerJustMyCode@4
010471b7 a @comp.id
80000391 a @feat.00
00000000 d __87B4E6C0_mylib@c
00000000 T __JustMyCode_Default
U __RTC_CheckEsp
U __RTC_InitBase
00000000 r __RTC_InitBase.rtc$IMZ
U __RTC_Shutdown
00000000 r __RTC_Shutdown.rtc$TMZ
00000000 T _fnmylib
Debug/examples.obj:
00000000 N .chks64
00000000 N .debug$S
00000000 N .debug$S
00000000 N .debug$S
00000000 N .debug$S
00000000 N .debug$S
00000000 N .debug$S
00000000 N .debug$T
00000000 i .drectve
00000000 d .msvcjmc
00000000 r .rdata
00000000 r .rtc$IMZ
00000000 r .rtc$TMZ
00000000 t .text$mn
00000000 t .text$mn
00000000 t .text$mn
00000000 t .text$mn
00000000 t .text$mn
00000000 R ??_C@_09DEHHIH@hello?5?$CFs?6@
00000008 C ?_OptionsStorage@?1??__local_stdio_printf_options@@9@9
U @__CheckForDebuggerJustMyCode@4
010471b7 a @comp.id
80000391 a @feat.00
00000000 T ___local_stdio_printf_options
00000001 d __101834BA_corecrt_wstdio@h
00000003 d __2F33A99E_examples@c
00000002 d __AD6A91B7_stdio@h
00000000 d __F66CEB67_corecrt_stdio_config@h
U __imp____acrt_iob_func
U __imp____stdio_common_vfprintf
00000000 T __JustMyCode_Default
U __RTC_CheckEsp
U __RTC_InitBase
00000000 r __RTC_InitBase.rtc$IMZ
U __RTC_Shutdown
00000000 r __RTC_Shutdown.rtc$TMZ
00000000 T __vfprintf_l
00000000 T _hello
00000000 T _printf
在我为 x64 系统重建项目后(我必须在 Build > Configuration Manager 中将“Active solution platform”更改为 x64),这是dumpbin /symbols mylib.lib 的结果。我可以在摘要中看到hello 函数。
【问题讨论】:
-
目前看起来还不错。问题可能是图书馆。你究竟是如何建造它的?它是一个dll吗?您应该检查符号 hello 是否确实存在于库中。
-
@ead 感谢您的评论。我使用 VS2019 制作了一个静态库并按照指令 (docs.microsoft.com/en-us/cpp/build/…) 进行操作。我还制作了 dll 文件以防万一(不同的名称)。但是,无论您使用 dll 还是 lib,错误都是相同的。我将库文件放在文件夹名称“lib”中。 “检查库中真正存在的符号 hello”到底是什么意思?
-
参见例如stackoverflow.com/q/305287/5769463可能是编译成c++的
-
@ead,我在上面编辑了我的问题,添加了检查库的结果。如何知道源文件被编译为 c++ ?当我们使用 *.c 和 *.h 在 VS 中编译为 c++ 时是否可以?
-
@ead,非常感谢。我成功地为 x64 构建了库。不知道VS默认系统架构是x86,不是x64。最后按预期编译 cython 代码。
标签: python c windows visual-studio cython