12

I am implementing a C++ program that uses python/C++ Extensions. As of now I am explicitly linking my program to python static library I compiled. I am wondering is there any way to link my program with system installed python(i mean the default python installation that comes with linux)

neuron
  • 619
  • 2
  • 7
  • 13

1 Answers1

20

Yes. There is a command line utility called python-config:

Usage: /usr/bin/python-config [--prefix|--exec-prefix|--includes|--libs|--cflags|--ldflags|--help]

For linkage purposes, you have to invoke it with --ldflags parameter. It will print a list of flags you have to pass to the linker (or g++) in order to link with system installed python libraries:

$ python-config --ldflags
-L/usr/lib/python2.6/config -lpthread -ldl -lutil -lm -lpython2.6

It also can give you flags to compilation with --cflags parameter:

$ python-config --cflags
-I/usr/include/python2.6 -I/usr/include/python2.6 -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes

Say you have a test program in test.cpp file, then you can do something like this in order to compile and link:

g++ $(python-config --cflags) -o test $(python-config --ldflags) ./test.cpp

That will link your program with shared libraries. If you want to go static, you can pass -static option to the linker. But that will link with all static stuff, including a runtime. If you want to go just with static python, you have to find those libraries yourself. One of the option is to parse python-config --ldflags output and look for libraries with .a extensions. But I'd rather stick to all dynamic or all static.

Hope it helps. Good luck!

  • 1
    Hi Vlad, Thank you so much for the response.. but unfortunately I am not seeing any python-config tool in /usr/bin/ I am on Fedora13. Do you think it could depend on platform?? – neuron Oct 08 '10 at 21:03
  • 1
    Do you have a python development RPM installed? This is a part of SDK I think. I have it on both Ubuntu 10.4 and Fedora 12. You just probably don't have required package installed. –  Oct 08 '10 at 21:06
  • 1
    hey.. Thanks!! found it after installing python-devel from package manager using 'sudo yum install python-devel' – neuron Oct 08 '10 at 21:09