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
Related
I am currently using a mac version 11.6.1 with Big Sur on it. I have the latest version of python (3.9) on it, however I need to use 3.6 for a project at work. I attempted to use pyenv to install this version but ran into a number of issues with pyenv. So, I next went to https://www.python.org/downloads/release/python-3615/ and downloaded this version. In my terminal I ran the following commands.
$ cd Downloads
$ cd python-3.6.15
$ ./configure
$ make
Here I ran into this error.
./Modules/getpath.c:528:49: warning: incompatible pointer types passing 'unsigned long *' to parameter of type 'uint32_t *' (aka 'unsigned int *') [-Wincompatible-pointer-types]
else if(0 == _NSGetExecutablePath(execpath, &nsexeclength) && execpath[0] == SEP) {
^~~~~~~~~~~~~
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/mach-o/dyld.h:98:54: note: passing argument to parameter 'bufsize' here
extern int _NSGetExecutablePath(char* buf, uint32_t* bufsize) __OSX_AVAILABLE_STARTING(__MAC_10_2, __IPHONE_2_0);
^
1 warning generated.
gcc -Wno-unused-result -Wsign-compare -Wunreachable-code -DNDEBUG -g -fwrapv -O3 -Wall -std=c99 -Wextra -Wno-unused-result -Wno-unused-parameter -Wno-missing-field-initializers -Wstrict-prototypes -I. -I./Include -DPy_BUILD_CORE -c ./Modules/posixmodule.c -o Modules/posixmodule.o
./Modules/posixmodule.c:8210:15: error: implicit declaration of function 'sendfile' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
ret = sendfile(in, out, offset, &sbytes, &sf, flags);
^
./Modules/posixmodule.c:10432:5: warning: code will never be executed [-Wunreachable-code]
Py_FatalError("abort() called from Python code didn't abort!");
^~~~~~~~~~~~~
1 warning and 1 error generated.
make: *** [Modules/posixmodule.o] Error 1
I am banging my head against the wall here with the errors from pyenv and now this. My attempt here was to run a virtual environment with virtualenv and point it at the 3.6 version of python, however, as you can see I can't seem to install python correctly. Any assistance here would be appreciated.
Since I was asked about the error with pyenv I'll include that here too.
BUILD FAILED (OS X 11.6.1 using python-build 2.2.0-5-g54889eb8)
Inspect or clean up the working tree at /var/folders/77/93y4tp5x557ff0h_3f76bh5c0000gn/T/python-build.20211104075648.30324
Results logged to /var/folders/77/93y4tp5x557ff0h_3f76bh5c0000gn/T/python-build.20211104075648.30324.log
Last 10 log lines:
checking for --with-cxx-main=<compiler>... no
checking for clang++... no
configure:
By default, distutils will build C++ extension modules with "clang++".
If this is not intended, then set CXX on the configure command line.
checking for the platform triplet based on compiler characteristics... darwin
configure: error: internal configure error for the platform triplet, please file a bug report
make: *** No targets specified and no makefile found. Stop.
with penv try this:
# get latest pyenv version
brew unlink pyenv
brew install pyenv --HEAD
CFLAGS=-Wno-implicit-function-declaration pyenv install 3.6.15
I built Python 3.6 from source and am trying to install lxml. Trying to install it from pip gives the following error:
gcc -pthread -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -I/usr/include/libxml2 -Isrc -Isrc/lxml/includes -I/usr/local/include/python3.6m -c src/lxml/etree.c -o build/temp.linux-armv6l-3.6/src/lxml/etree.o -w
src/lxml/etree.c:91:20: fatal error: Python.h: No such file or directory
compilation terminated.
Compile failed: command 'gcc' failed with exit status 1
cc -I/usr/include/libxml2 -I/usr/include/libxml2 -c /tmp/xmlXPathInitln68cjsn.c -o tmp/xmlXPathInitln68cjsn.o
cc tmp/xmlXPathInitln68cjsn.o -L/usr/lib -lxml2 -o a.out
error: command 'gcc' failed with exit status 1
The same error is raised if I try to install Cython to compile lxml from source. I installed libxml2-dev and libxslt1-dev, but python3.6-dev, which is apparently also required, is not found in Raspbian repository. I added the following line to /etc/apt/sources.list:
deb http://ftp.de.debian.org/debian sid main
But when trying to install it afterwards, I receive the following error:
The following packages have unmet dependencies:
libdbus-1-3 : Breaks: dbus (< 1.9.16-1~) but 1.6.8-1+deb7u1 is to be installed
E: Error, pkgProblemResolver::Resolve generated breaks, this may be caused by held packages.
In fact, this error is raised on any package that is found in Debian repository that is not in Raspbian one. What can I do about this?
Since you installed Python from source, you should have the header files already. Should also use header files for the version that you built as opposed to the version in the package manager.
Either
Add the source code directory to includes used by GCC (gcc -I<dir>) or
Install the from source package in a system directory that is automatically included like /usr/include or /usr/local/include
The second option is probably easiest and should be possible from the standard configure/make scripts.
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?
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
I try for some time to install the python-igraph-0.6 module for my EPD-python.
I built the C-core, and following the instruction on various sites I do:
/home/joseph/epd/bin/python setup.py build
or
/home/joseph/epd/bin/python setup.py install
and it generates always the same error:
error: command 'gcc' failed with exit status 1
I would have to possible explainations: Either 'gcc' is not working properly (but I already compiled the C-part with ./configure, make, make install) or something is not properly linked somehow.
I already looked at similar posts and installed python-devel packages and so on ...
but nothing changed.
The full output is:
[root#joseph python-igraph-0.6]# /home/joseph/epd/bin/python setup.py build
Using default include and library paths for compilation
If the compilation fails, please edit the LIBIGRAPH_FALLBACK_*
variables in setup.py or include_dirs and library_dirs in
setup.cfg to point to the correct directories and libraries
where the C core of igraph is installed
()
Include path: /usr/include /usr/local/include
Library path:
running build
running build_py
running build_ext
building 'igraph._igraph' extension
gcc -pthread -fno-strict-aliasing -g -O2 -DNDEBUG -O2 -fPIC -I/usr/include -I/usr /local /include -I../../build/include -I../../include -I/usr/local /include -I/usr/include -I/home/joseph/epd/include/python2.7 -c src/common.c -o build/temp.linux-x86_64-2.7/src/common.o
gcc -pthread -fno-strict-aliasing -g -O2 -DNDEBUG -O2 -fPIC -I/usr/include -I/usr/local/include -I../../build/include -I../../include -I/usr/local/include -I/usr/include -I/home/joseph/epd/include/python2.7 -c src/arpackobject.c -o build/temp.linux-x86_64-2.7/src/arpackobject.o
In file included from src/arpackobject.c:23:0:
src/arpackobject.h:27:27: fatal error: igraph_arpack.h: No such file or directory
compilation terminated.
error: command 'gcc' failed with exit status 1
The error message you are getting indicates that the compiler cannot find the headers required to bind to the C core of igraph. In particular, it is missing a file called igraph_arpack.h, but this is not the only one, there would be more had gcc continued with the compilation after this message.
The setup.py script of the Python interface of igraph relies on pkg-config to figure out where to find the headers, but in your system this fails, probably because pkg-config itself is not installed. So, I guess that the solution is as follows:
Make sure that you run make install after having compiled the C core. You claim that you did so, but I thought I'd mention it anyway for sake of completeness.
Install pkg-config on your system.
Check that pkg-config knows about the installed C core of igraph by typing pkg-config --cflags --libs igraph. If you see a bunch of compiler options printed to the standard output, you are okay.
Run python setup.py install again. This time it should invoke pkg-config successfully, get the required compiler options, and compile the Python interface successfully.
If it still doesn't work (e.g., because you cannot get pkg-config to work), you can open setup.cfg and edit the include_dirs and library_dirs variables; the former should point to the folder where the igraph include files are to be found (typically /usr/local/include/igraph or /usr/include/igraph, depending on where exactly you installed the C core), and the latter should point to the folder where libigraph.so is).
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.