0

I am trying to write an C++ extension for python which involves Python C API. According to my previous experience on doing this with Visual Studio, I just need to include python's header files and link python's static libraries and everything would work fine.

However I couldn't do this on OS X Mavericks now. I installed a separate Python 3.4.2 from python.org and added an include entry in my Cmake configuration:
include_directories("/Library/Frameworks/Python.framework/Versions/3.4/Headers")

Then I have no clue how to proceed with the static libraries part. On Windows they are stored in %python_root%/libs and you can find .lib files there. But on OS X I couldn't find such directory in which .a static libraries are placed.

I tried several solutions such as attaching the result of python-config --lib to compile options and set(CMAKE_BUILD_TYPE Release) while they all ended up with the following error:

Undefined symbols for architecture x86_64:
    "_PyLong_FromLong", referenced from:
        test() in Main.cpp.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

I tried distutils as well, but it didn't seem to work properly, either :(

  • *Where* did you set the flags/libraries returned by `python-config --libs`? To which variable? Because those flags are *linker* flags and not compiler flags. – Some programmer dude Feb 21 '15 at 06:23
  • I set them to `CMAKE_CXX_FLAGS` in `cmake.txt` and the result returned was like `-ldl -framework CoreFoundation -lpython3.4m`. – Kevin M. Vansittart Feb 21 '15 at 07:04
  • Adding libraries to the compiler flags doesn't help, as those are only used for compilation, you should add them to e.g. `CMAKE_EXE_LINKER_FLAGS` instead. Then they will also be added in the correct place on the linker command line (where order of object/source files and libraries matter). – Some programmer dude Feb 21 '15 at 07:09
  • Thanks. I added `CMAKE_SHARED_LINKER_FLAGS` entry and also the `LINK_DIRECTORIES` pointing at the static library path and it now works :) – Kevin M. Vansittart Feb 21 '15 at 08:52

1 Answers1

0

You are getting a linker error, the library is has not been added to your link options.

On my system OSX 10.8.5, the static library is in the directory below: You need to add the directory to your cmake library path, and and add the python library itself. (In GCC you use the -L and -l options).

Directory: /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/config/

file: libpython2.7.a 
Gadi
  • 1,152
  • 9
  • 6
  • Thanks and it works now :) I thought that the static library directory would contain many `.a` files as it did on Windows but things are quite different here. – Kevin M. Vansittart Feb 21 '15 at 08:50