Compilar con -fPIC y Swig error

I'm trying to add a Swig interface on top of some already existing c++ code and I'm coming up with some errors with the -fPIC bandera.

If I compile the already existing code without -fPIC and then create my Swig module using:

swig -python -c++ pyinterface.i
gcc -fPIC $(CFLAGS)-I $(PYTHON_INCLUDE_DIR) -c pyinterface_wrap.cxx
g++ -shared -o pyinterface_wrap.o -I $(PYTHON_INCLUDE_DIR) -L $(PYTHON_LIB_DIR) $(LPATH) $(LFLAGS) $(IPATH) $(LIBS)

Entonces obtengo un

warning: creating a DT_TEXTREL in object

However the module does seem to load and work in Python.

If I compile the already existing code with -fPIC and then do the same to create my module, when I try and import it into python:

import pyinterface

Entonces me sale el error

ImportError: ./ undefined symbol: _Z7InitErfPA20_d

I can't tell why this would be. However, in the compliation of the base c++ files that I am trying to wrap, they do link to some standard libraries such as -llapack.

Could it be that I need to recompile these base libraries, like lapack, with -fPIC. That seems insane.


Actually sorry, I didn't realise, but with the -fPIC flag on I'm getting an error during compile:

g++: Internal error: Killed (program cc1plus)

Please submit a full bug report. See para obtener instrucciones.

I'll update this question if I figure out whats going on, but it seems like it might be unrelated to anything that the question actually raises.

preguntado el 04 de julio de 12 a las 09:07

What architecture and OS are you using? My current best guess is that your _pyinterface module isn't actually linked against your C++ library, or there's a name mangling problem, i.e. you call C code from C++ but are missing extern "C" with the declarations. -

32 bit, linux. The base c++ code does call some C code (from lapack for instance) but that all works usually. You could be right about the linking, but then wouldn't both these methods fail? -

Can you make a minimal (e.g. 10 lines, single trivial inlined function) example that illustrates this? I'd have a play with it on my system and figure out what's going on, but I can't do more than guess from this right now. -

You correctly list g++ as the linker driver, but gcc as the compiler driver. But you should always compile C++ with g++ driver too. That might be your problem. -

You say it's insane that the base libraries need to be compiled with -fPIC, but sadly it's true. Welcome to the love/hate world of Python :) -

1 Respuestas

I might be wrong regarding this reply.

Regarding the warning: creating a DT_TEXTREL in object

Even I was getting the same warning.

In my Makefile; I had done something like this

SRC= ../../some_folder/file.c 1.c 2.c

OBJS= $(SRC:.c=.o)

and while making the shared object, i was using this $(OBJS); whereas the "file.o" would be available at that location.

Make sure the object files that you are trying to access are available at that location.

My warning message vanished when i made the following changes

OBJS=file.o 1.o 2.o

Respondido el 08 de enero de 14 a las 13:01

Thanks for your input, but sorry this was too long ago for me to remember what this is all about. So I have no idea if your problem is the same as that I was experiencing. - George Wilson

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.