[CMake] UseSWIG, Python, SWIG_LINK_LIBRARIES and linking errors

Lori Pritchett-Sheats lpritch at lanl.gov
Wed Sep 14 14:53:44 EDT 2011


I'm trying to build a very simple example from the SWIG documentation
http://www.swig.org/Doc2.0/SWIGDocumentation.html#Introduction_build_system

There is a code snippet that demonstrates how to use SWIG in a CMake 
build system, but I can not get this simple example to build on a Linux 
platform where the Python libraries are not built with the '-fPIC'. I've 
modified the example slightly by building a C example file instead of a 
C++ file and here is my version


FIND_PACKAGE(SWIG REQUIRED)
INCLUDE(${SWIG_USE_FILE})

FIND_PACKAGE(PythonLibs)
INCLUDE_DIRECTORIES(${PYTHON_INCLUDE_PATH})

INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR})

SET(CMAKE_SWIG_FLAGS "")

SET_SOURCE_FILES_PROPERTIES(example.i PROPERTIES SWIG_FLAGS "-includeall")
SWIG_ADD_MODULE(example python example.i example.c)
SWIG_LINK_LIBRARIES(example ${PYTHON_LIBRARIES})

On my Linux machine with static Python libs, the build fails trying to 
create _example.so, complaining about the static Python library not 
built with -fPIC.

Linking C shared module _example.so
/usr/bin/ld: /usr/local/lib/libpython2.6.a(abstract.o): relocation 
R_X86_64_32 against `a local symbol' can not be used when making a 
shared object; recompile with -fPIC
/usr/local/lib/libpython2.6.a: could not read symbols: Bad value
collect2: ld returned 1 exit status

This example works on systems where the Python library is shared (Mac 
OSX) and if I remove the SWIG_LINK_LIBRARIES line it will compile on 
the  Linux system, but then fails to build on the Mac.  Since the 
construction of these Python modules is hidden by the SWIG macros I'm 
very confused about what is going on.

Has anyone run into this problem with static Python libraries and did 
you find a portable solution? Testing if the Python libraries are shared 
or static and then coming up with an -fPIC test seems to be the only 
solution I see to this problem.




-- 
Lori A. Pritchett-Sheats, PhD.
CCS-2, Computational Physics and Methods
Office: 505-665-6675
Fax: 505-665-4972

Los Alamos National Laboratory
P.O. Box 1663
MS D413
Los Alamos, NM 87544



More information about the CMake mailing list