Python:未定义引用 `_imp __Py_InitModule4`

15 投票
6 回答
18628 浏览
提问于 2025-04-15 22:45

我正在尝试用mingw的gcc来构建Rabbyt库的调试版本,以便与我用MSVC编译的python26_d一起运行。结果遇到了很多未定义的引用,这让我不得不创建libpython26_d.a。不过,仍然有一个未定义的引用没有解决。上网搜索了一下,找到了这个链接:

http://www.techlists.org/archives/programming/pythonlist/2003-03/msg01035.shtml

但是,使用 -rdynamic 这个选项并没有帮助。

e:\MinGW/bin\gcc.exe -mno-cygwin -mdll -O -Wall -g -IE:\code\python\python\py26\
include -IE:\code\python\python\py26\PC -c rabbyt/rabbyt._rabbyt.c -o build\temp
.win32-2.6-pydebug\Debug\rabbyt\rabbyt._rabbyt.o -O3 -fno-strict-aliasing
rabbyt/rabbyt._rabbyt.c:1351: warning: '__Pyx_SetItemInt' defined but not used
writing build\temp.win32-2.6-pydebug\Debug\rabbyt\_rabbyt_d.def

e:\MinGW/bin\gcc.exe -mno-cygwin -shared -g build\temp.win32-2.6-pydebug\Debug\r
abbyt\rabbyt._rabbyt.o build\temp.win32-2.6-pydebug\Debug\rabbyt\_rabbyt_d.def -
LE:\code\python\python\py26\libs -LE:\code\python\python\py26\PCbuild -lopengl32
 -lglu32 -lpython26_d -lmsvcr90 -o build\lib.win32-2.6-pydebug\rabbyt\_rabbyt_d.
pyd
build\temp.win32-2.6-pydebug\Debug\rabbyt\rabbyt._rabbyt.o: In function `init_ra
bbyt':

E:/code/python/rabbyt/rabbyt/rabbyt._rabbyt.c:1121: undefined reference to `_imp
__Py_InitModule4'

6 个回答

21

在这个文件 C:\Python27\Lib\distutils\cygwinccompiler.py 中,它包含了 MinGW 编译器的设置,找到 Mingw32CCompiler 这个类:

self.set_executables(compiler='gcc -O -Wall',
    compiler_so='gcc -mdll -O -Wall',
    compiler_cxx='g++ -O -Wall',
    linker_exe='gcc ',
    linker_so='%s %s %s'
        % (self.linker_dll, shared_option,
        entry_point))

然后在 compiler_so 参数中添加 -D MS_WIN64

compiler_so='gcc -mdll -O -Wall -D MS_WIN64'
23

如果有人遇到同样的错误信息,但在不同的情况下,可以试试在你的命令行中加上 -D MS_WIN64,这个方法对我有效!

4

我在使用Cygwin和MinGW的gcc时,它们之间出现了冲突。我把这两个都删掉了,然后重新安装了MinGW和Cygwin,但这次没有安装gcc,这样问题就解决了。

撰写回答