Jorgel
Jorgel

Reputation: 930

How to compile and link python3 wrapper functions using gcc

I am trying to compile some C wrapper functions to Python 3.4 using gcc and makefile, but I am having no success in finding the correct compile and link flags. I am using Ubuntu 14

Right now this is what I was trying in the makefile:

CC      = gcc
CFLAGS  = -Wall -std=c99 `pkg-config --cflags python3` 
LDFLAGS = `pkg-config --libs python3` 

final: functions.o wrapper.o
$(CC) -o functions.o $(CFLAGS) $(LDFLAGS)

functions.o: functions.c functions.h
$(CC) $(CFLAGS) -c functions.c

wrapper.o: wrapper.c
$(CC) $(CFLAGS) -g -c wrapper.c

Using this get me this error:

/usr/bin/ld: /usr/local/lib/libpython3.4m.a(dynload_shlib.o): undefined reference to symbol 'dlsym@@GLIBC_2.2.5'
//lib/x86_64-linux-gnu/libdl.so.2: error adding symbols: DSO missing from command line

Also, I have little experience in makefiles, so I don't if I have done something wrong along the way

Upvotes: 2

Views: 2873

Answers (1)

Michael Petch
Michael Petch

Reputation: 47603

You should probably find a good tutorial on Makefiles, but this one should get you started:

CC      = gcc
CFLAGS  = -Wall -std=c99 `pkg-config --cflags python-3.4`
CFLAGS  += -fPIC
LDFLAGS = `pkg-config --libs python-3.4`

all: myfunctions.so

myfunctions.so: wrapper.o functions.o
    $(CC) -shared $(LDFLAGS) $^ -o $@

If you are creating C wrappers for Python you need to create a shared object. To do that at a minimum you have to use -fPIC when compiling and use -shared when linking. The sample Makefile above uses the built in rules to compile .c files into .o files. The shared object in this example will be created as myfunctions.so, but you can change myfunctions.so: to whatever you'd prefer to call it. This Makefile can be called with make all to generate the shared object.

Upvotes: 1

Related Questions