On 20 December 2015 at 03:15, Thomas Nyberg <[email protected]> wrote:
> 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 $@ >lt;
> --------
>
> 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.

Hi Thomas,

Whether or not what you're currently doing is acceptable really
depends. Who needs to install this? Do you know which OS or compiler
they will use etc? Are you hoping that this will be installed by pip?

The setup.py is supposed to allow the end user a bit of freedom to use
different OS/compiler etc. and free up the module author from needing
to know exactly where the Python header files and link libraries will
be on each platform. OTOH if you are just writing for exactly one
platform then what you have may be more convenient and flexible.

I would approach this step-by-step to convert that to using setup.py.
So the first part is to tell extension about all of the cpp files and
get it to use g++ to compile it. The next step is to tell about the
additional include locations. The next step is the additional
libraries that need linking. There are arguments to Extension and
setup for each of these things. The last part is to get the exact
right compiler flags.

--
Oscar
_______________________________________________
Distutils-SIG maillist  -  [email protected]
https://mail.python.org/mailman/listinfo/distutils-sig

Reply via email to