Python: undefined reference to `_imp __Py_InitModule4'
I'm trying to do a debug build of the Rabbyt library using mingw's gcc to run with my MSVC built python26_d.. I got a lot of undefined references which caused me to create libpython26_d.a, however one of the undefined references remains. Googling gives me:
http://www.techlists.org/archives/programming/pythonlist/2003-03/msg01035.shtml
But -rdynamic doesn't help.
e:\MinGW/bin\gcc.exe -mno-cygwin -mdll -O -开发者_如何学PythonWall -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'
If anyone comes across this same error message, but in a different situation: try to add -D MS_WIN64
to your command line, it worked for me!
In the file C:\Python27\Lib\distutils\cygwinccompiler.py
, which contains the MinGW compiler settings, find the Mingw32CCompiler
class:
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))
and add -D MS_WIN64
to the compiler_so
argument:
compiler_so='gcc -mdll -O -Wall -D MS_WIN64'
My Cygwin and MinGW gcc installs were conflicting with each other. I deleted them both and the installed MinGW and cygwin without gcc and that solved the problem.
I have seen this if you try to mix object code compiled for debugging (_DEBUG
macro is defined) with non-debug objects (_DEBUG
is not defined).
There is a line in the python core code that #defines Py_InitModule4
to another name (Py_InitModule4TraceRefs
) to trigger a "link-time error" (!sic) if you mix objects.
Make sure you link against a python debug library when you compile for debugging and vice-versa.
I just solved this problem by adding the compiler flag -DPy_TRACE_DEFS
. In my case I was trying to build debug versions of SIP/PyQt.
The error can occur when the debug version of Python is compiled with the Py_TRACE_DEFS
option switched on. This causes the resulting python2x_d library to contain the function Py_InitModule4TraceRefs
instead of Py_InitModule4
that the compiler is looking for (amongst other changes). Switching the option on for the project you are building ensures the code produced is compatible, and that the correct version of Py_InitModule4
is found.
More information on Py_TRACE_DEFS
can be found in Misc/SpecialBuilds.txt
in the Python source folder or here.
For those of you who don't have Visual Studio but would like to compile OpenCV from source with MinGW on Windows and run into this problem, here is my cmake command:
cmake -G "MinGW Makefiles" -D CMAKE_BUILD_TYPE=RELEASE -D INSTALL_PYTHON_EXAMPLES=ON -D INSTALL_C_EXAMPLES=OFF -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-3.1.0/modules -D PYTHON_EXECUTABLE=C:/Users/your_user_name/Envs/cv/Scripts/python.exe -D BUILD_EXAMPLES=ON -D CMAKE_CXX_FLAGS="-DMS_WIN64 -w" ..
(For those unfamiliar: In "cmd.exe", create a folder called build
in the opencv-3.1.0
folder which contains a file called CMakeLists.txt
, cd
into build
, and issue the above command there. You can edit most of the parameters to your desire, I used virtual environment for Python here, but you don't have to.)
There is one more thing you'd need to do, which is to add
#include <cmath>
#define _hypot hypot
before #include <Python.h>
in opencv-3.1.0\modules\python\src2\cv2.cpp
, which resolves the "undefined hypot" problem you might encounter, as suggested by relevant SO posts.
You would most likely compile without pain with e.g. make -j4
after these two tweaks.
精彩评论