Python: undefined reference to `_imp __Py_InitModule4'
Asked Answered
N

6

15

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 -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'
Nicolina answered 16/5, 2010 at 3:15 Comment(2)
python_d setup.py build --debug -c mingw32 is the command lineNicolina
It's not recommended to use MinGW (or any of its forks) with the official WIndows Python builds (or any other MSVC build), for the reasons explained at bugs.python.org/issue4709#msg243605.Memorabilia
N
4

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.

Nicolina answered 12/8, 2010 at 21:13 Comment(0)
A
23

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!

Ackley answered 12/3, 2012 at 18:52 Comment(2)
This was my fix too. Thanks!Surly
@Ackley Hi, I have a similar problem at stackoverflow.com/questions/18956136/… Can you help me with this? In my case, I am not using Boost to build the binaries with command line. I am simply trying to build a Hello World example. It should be very easy, but now it is driven me crazy... Thanks.Tamishatamma
T
21

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'
Thrilling answered 8/11, 2013 at 19:50 Comment(1)
No need to mess with your installed distutils module. Just add define_macros=[('MS_WIN64',None)] as an argument to the setup call in setup.py.Floris
N
4

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.

Nicolina answered 12/8, 2010 at 21:13 Comment(0)
H
4

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.

Hemangioma answered 29/11, 2010 at 15:59 Comment(0)
A
4

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.

Abisia answered 3/9, 2015 at 14:29 Comment(0)
P
1

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.

Pignut answered 11/6, 2018 at 7:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.