I am trying to develop c extensions for python, which requires me to use
#include "Python.h"
in the c source code. However, it appears that Python.h in Ubuntu systems can be obtained by downloading python-dev, but this is not an option for macOS.
I tried compiling using /Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/Headers/ (which contains a Python.h file) but When I use the command
gcc test.c -I /Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/Headers/
I get the following error:
Undefined symbols for architecture arm64:
"_PyObject_Size", referenced from:
_main in test-165478.o
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Which makes it seem that the Python.h file on my mac is incomplete somehow. Is the solution to this problem downloading the source of python-dev and then using the Python.h header file that is in that package? If so, where could I find the python-dev source code?
Related
I want to install Quantlib from source to enable intraday support for Python. When installing QuantLib from source on Mac OSX 11.1 with M1 chip I am facing a problem during the 'make' in the tests.
Undefined symbols for architecture arm64:
"_PrimitivePolynomials", referenced from:
LowDiscrepancyTest::testPolynomialsModuloTwo() in lowdiscrepancysequences.o
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[1]: *** [quantlib-test-suite] Error 1
make: *** [all-recursive] Error 1
This looks like the linker is not finding the QuantLib libraries you have just compiled, and is instead trying to link to a version compiled for a different architecture. Try running make install or sudo make install prior to compiling the tests -- if that works, then you have something going on with your library paths.
I'm running macOS 12.4 here, and I'm trying to compile a sample C program which uses Python's library:
#include <Python.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
wchar_t *program = Py_DecodeLocale(argv[0], NULL);
if (program == NULL) {
fprintf(stderr, "Fatal error: cannot decode argv[0]\n");
exit(1);
}
Py_SetProgramName(program);
Py_Initialize();
PyRun_SimpleString("print(\"Hello, world!\")\n");
if (Py_FinalizeEx() < 0) {
exit(EXIT_FAILURE);
}
PyMem_RawFree(program);
return EXIT_SUCCESS;
}
Simply running gcc ./main.c -o ./main.o yields the following error:
./main.c:1:10: fatal error: 'Python.h' file not found
#include <Python.h>
^~~~~~~~~~
1 error generated.
I tried adding "-framework Python" argument so that the final command would look like:
gcc ./main.c -o ./main.o -framework Python, but that didn't change anything.
Alternatively, I decided to manually specify the location of the header files:
gcc ./main.c -o ./main.o -I/Library/Frameworks/Python.framework/Headers
But now it's complaining about missing symbols (meaning that it can't find the lib).
Undefined symbols for architecture x86_64:
"_PyMem_RawFree", referenced from:
_main in main-e12ebf.o
"_PyRun_SimpleStringFlags", referenced from:
_main in main-e12ebf.o
"_Py_DecodeLocale", referenced from:
_main in main-e12ebf.o
"_Py_FinalizeEx", referenced from:
_main in main-e12ebf.o
"_Py_Initialize", referenced from:
_main in main-e12ebf.o
"_Py_SetProgramName", referenced from:
_main in main-e12ebf.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
If I take my previous command and decide to add -framework Python now, it'll just say the framework couldn't be found:
ld: framework not found Python
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Same deal with -lpython.
I do have /Library/Frameworks/Python.framework present on my file system, but for some reason I can't get it to work. I've got Python 3.10.5 and 2.7, both of which were installed using Python's own official DMG installers (and just in case, inside /Library/Frameworks/Python.framework, the Versions/Current link points to Versions/3.10, so the symlink isn't broken). Beforehand, I tried installing Python 3.9 with homebrew using brew install python command, but after the installation, the problem persisted (all of the commands above had the exact same errors).
Additionally, it's not like macOS itself cannot see my Python installations. Both python and python3 commands work in the terminal, and running each one with --version argument will print the correct version. pip and pip3 work fine, too.
Xcode and Xcode Command Line Tools are both installed. As stated before, I'm running macOS 12.4 (Release)
I still couldn't figure this one out, but I did manage to come up with a workaround. I'm still hoping to know what the actual core problem is and how to fix it, though...
In any case, here's what I did:
Run this command (add Python lib):
ln -sf /Library/Frameworks/Python.framework/Versions/Current/Python /usr/local/lib/libpython.dylib
Add this line to ~/.bash_profile (add Python headers):
export CPATH="/Library/Frameworks/Python.framework/Headers:{$CPATH}"
Compile with this command:
gcc ./main.c -o ./main -lpython
pip install pyaudio failed on MacOS with error src/_portaudiomodule.c:27:19: fatal error: stdio.h: No such file or directory #include <stdio.h>
After doing some research I ran the following commands:
1/ export CPATH=/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/
2/ pip install --global-option='build_ext' --global-option='-I/usr/local/include' --global-option='-L/usr/local/lib' pyaudio
But got errors /usr/local/include/portaudio.h:114:1: warning: function declaration isn’t a prototype [-Wstrict-prototypes] and error: command 'gcc' failed with exit status 1 instead.
It seems upgrading to MacOS Catalina messed up my Xcode Developer Tools C headers but am not able to resolve it.
i was able to solve this by copying portaudio.h from /usr/local/include/ to anaconda3/include.
I work on a u 18.04 and a third party tool (pymesh) that I use needs to include Python.h
I installed python-dev, python3-dev, libpython-dev and libpython3-dev.
Python.h is found in the folders: /usr/include/Python/, /usr/include/Python3.6m/ and /usr/include/Python3.6/.
Still when I try to compile a minimal C-program:
#include<Python.h>
int main(){}
I get the error:
$ gcc test.c
test.c:1:9: fatal error: Python.h: No such file or directory
#include<Python.h>
^~~~~~~~~~
compilation terminated.
I can fix this by making symbolic links to every header in one of those directories in, e.g., /usr/local/include/ or by specifying the path in the #include statement, but is that the correct way of doing it?
You should use the -I option of gcc:
gcc -I /usr/local/include test.c
I have a virtual env for python, and trying to install some packages in the virtual-env:
sudo /Users/edamame/Library/python_virenv/bin/pip install matplotlib
Then I got the following error:
:
:
clang: warning: -framework Tcl: 'linker' input unused
clang: warning: -framework Tk: 'linker' input unused
In file included from src/_tkagg.cpp:28:
/usr/local/include/tk.h:71:13: fatal error: 'X11/Xlib.h' file not found
# include <X11/Xlib.h>
^
1 error generated.
error: command 'cc' failed with exit status 1
I am on Mac El-Captain, and I have:
edamame$ locate Xlib.h
WARNING: The locate database (/var/db/locate.database) does not exist.
To create the database, run the following command:
sudo launchctl load -w /System/Library/LaunchDaemons/com.apple.locate.plist
Please be aware that the database can take some time to generate; once
the database has been created, this message will no longer appear.
Any idea how I can fix this problem? Thanks!
You should either install the development files for X11 (from memory: libx11-dev), or if they are already installed, add the X11 directory to your include search path.
You can use locate Xlib.h to figure out if the files are already there. Use the -I compiler option to add directories to your search path.