[Distutils] Trouble using setuptools to build separate c++ extension

Thomas Nyberg tomnyberg at gmail.com
Sat Dec 19 22:15:04 EST 2015


Hello I'm having trouble understanding the right way to build a c++ 
module using setuptools. I've been reading the docs, but I'm confused 
where I should be putting my build options. Everything builds fine on 
its own. I have my sources in src/ and my headers in include/.

My first problem is that I'm having trouble figuring out where to put my 
build flags. Here is the Makefile I'm currently using:

--------
srcs=$(wildcard *.cpp)
srcs+=$(wildcard src/*.cpp)
objs=$(patsubst %.cpp,%.o,$(srcs))

cc=g++
ccflags=-std=c++11 -g -O3 -fPIC
includes=-I. -I./include/ -I/usr/include/python2.7/ -I/usr/include/boost
libflags=-L. -L/usr/lib/x86_64-linux-gnu
ldflags= -shared -Wl,--export-dynamic

patent_computer_cpp.so: $(objs)
         $(cc) $(libflags) $(ldflags) $(objs) -o patent_computer_cpp.so 
-lboost_python -lpython2.7

%.o:%.cpp
         $(cc) $(ccflags) $(includes) -c -o $@ $<
--------

Unfortunately I can't post the sources, but they compile fine to produce 
the `patent_computer_cpp.so` file which can be imported as a module. 
Maybe I should also point out that I'm using boost-python (I don't think 
this is the issue though).

I just can't figure out how to get setuptools.Extension to use these 
build flags. I've seen recommendations online saying that I should set 
CFLAGS as an environment variable and set OPT='' as an environment 
variable as well, but this just feels wrong given the simplicity of my 
setup. (Besides the shared object doesn't seem to compile correctly in 
this case.) I've tried using the extra_compile_args option in setup.py, 
but that fails.

Is there a way to avoid setting environment variables like this or is 
this the accepted way to build this kind of software? Am I missing some 
obvious docs somewhere? Thanks for any help.

Cheers,
Thomas


More information about the Distutils-SIG mailing list