gcc:cannot find -lpython2.7 when including python headers during linking - python

I am trying to port python codes on windows to c codes and the convert it to an executable on windows 7. In linux I can do this easily, but in windows, this is totally new for me plus I am not good with compilers.
I download minGW and installed the compiler collection which includes gcc.
Below shows how I am trying to include python headers when compiling the .c file
C:\Users\repzero>gcc -Os -I C:\Python27\include -o C:\Users\repzero\hello.o C:\Users\repzero\hello.c -lpython2.7 -lpthread -lm -lutil
I get the errors
c:/mingw/bin/../lib/gcc/mingw32/5.3.0/../../../../mingw32/bin/ld.exe: cannot find -lpython2.7
c:/mingw/bin/../lib/gcc/mingw32/5.3.0/../../../../mingw32/bin/ld.exe: cannot find -lutil
collect2.exe: error: ld returned 1 exit status
Additional information
Here is my command line which I used to port python codes to c does (This generates hello.c) this works fine
cython --embed -o hello.c hello.py
How can I alleviate those errors?

Related

Cython: Compiling and Cannot Find Library Mac OSX 10.12

I am just getting started with Cython and am trying to compile a "Hello World" script. I am trying to use gcc -Os /User/Documents/Python/Test\ Python/helloCopy.c -I/Library/Frameworks/Python.framework/Versions/3.5/include/python3.5m -l, but I don't know what to add after the -l. Other forum pages say to "include -lpython2.7 (or whatever version of Python you're using) on the linker command-line" but that produces ld: library not found for -lpython3.5
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Should I be directing the -l to a particular folder?
I do not know what resource you're using, but this does not say anything about a -l flag. It suggests
cython -a helloCopy.pyx
This creates a yourmod.c file, and the -a switch produces an annotated html file of the source code. Pass the -h flag for a complete list of supported flags.
gcc -shared -pthread -fPIC -fwrapv -O2 -Wall -fno-strict-aliasing -I/usr/include/python2.7 -o helloCopy.so helloCopy.c
(Linux)
On macOS I would try to compile with
gcc -I/usr/bin/python -o helloCopy.so helloCopy.c
to use the standard version of Python.

Fatal Python error when using SWIG under OSX

I have troubles using a Python interface generated with SWIG (I have OSX 10.11.12). After compiling and linking everything together as such:
swig -python erk_integrator.i
gcc -c -fPIC -O3 model.c auxiliary_functions.c timing_functions.c
gcc -c -fPIC -O3 erk_integrator.c erk_integrator_wrap.c -I. -I/usr/local/include/python2.7
gcc -lpython -dynamiclib model.o erk_integrator.o erk_integrator_wrap.o auxiliary_functions.o timing_functions.o -o _erk_integrator.so
I try a test script, but Python throws a fatal error:
/usr/local/bin/python test_erk.py
Fatal Python error: PyThreadState_Get: no current thread
Abort trap: 6
But when I run
/System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7 test_erk.py
everything works as it should. However, I need to use /usr/local/bin/python (from Homebrew) instead of the system Python.
I think something goes wrong in the linking step. Many thanks for helping!
This blog post helped me solve it: blog.tim-smith.us/2015/09/python-extension-modules-os-x
It turns out on OSX you need
-undefined dynamic_lookup
instead of
-lpython

cx_Freeze building error

Im trying to install cx_Freeze for Python 3.3. However, when compiling the source I get this error
gcc -pthread build/temp.linux-i686-3.3/source/bases/Console.o -L/usr/local/lib/python3.3/config-3.3m -lpython3.3 -o build/lib.linux-i686-3.3/cx_Freeze/bases/Console -Xlinker -export-dynamic -lpthread -ldl -lutil -lm -s
/usr/bin/ld: cannot find -lpython3.3
collect2: error: ld returned 1 exit status
Since I'm a newbie I couldn't really figure out what's missing so I've tried installing libpython3.3-dev and python3.3-dev, but this doesn't solve the problem. I don't know if this is wheather a Python or gcc problem. What am I missing?
create link and it will be ok!
sudo ln /usr/local/lib/libpython3.3m.a /usr/local/lib/libpython3.3.a

Python.h header missing

I'm trying to figure out how to compile Python modules in C (http://docs.python.org/extending/extending.html), but the Python.h header file appears to be missing.
I've installed all the python development headers (I have Python-dev, python2.7-dev, python2.6-dev, python-all-dev) but gcc is still reutrning the error:
fatal error: Python.h: No such file or directory
compilation terminated.
Any idea where I'm going wrong here? Also is there an argument I need to add to gcc for Python.h (and what is it?).
You need to use python-config to determine the compile time and link time flags.
When compiling:
gcc -c `python-config --cflags` somefile.c
When linking:
gcc -o libfoo.so -shared `python-config --ldflags`
Though you really ought to think about using distutils as described in Building C and C++ Extensions with distutils

g++ linking and swig

I have a cpp file with functions that I'm using in python with SWIG. I use the following commands to compile the source and create the file to use with python.
swig -c++ -python mini.i
g++ -O2 -c mini.cpp -I/usr/include/python2.4 -I/usr/lib/python2.4
g++ -O2 -c mini_wrap.cxx -I/usr/include/python2.4 -I/usr/lib/python2.4
g++ -shared mini.o mini_wrap.o -o _mini.so
I'm trying now to use GSL in my source cpp source file. If I was just compiling the GSL file I would do
g++ -lgsl -lgslcblas -lm -o mini.o mini.cpp
I've tried adding the -lgsl -lgslcblas -lm to the lines for the swig compile but I get
g++: -lgsl: linker input file unused because linking not done
g++: -lgslcblas: linker input file unused because linking not done
g++: -lm: linker input file unused because linking not done
How can I link the gsl libraries? Thanks
Swig does no linking, as the warning message states. Put the -lgsl etc. on the link command, which is your last g++ command.

Categories

Resources