From S.I.Reynolds at cs.bham.ac.uk Wed Sep 30 01:33:29 1998 From: S.I.Reynolds at cs.bham.ac.uk (Stuart I Reynolds) Date: Wed, 30 Sep 1998 00:33:29 +0100 Subject: [C++-SIG] Using SWIG for C++ to Python - ld problem Message-ID: <36116E49.686@cs.bham.ac.uk> I've just started using SWIG to port link a C++ class I have to Python. I don't mind saying, its harder than it looks. % swig -python -shadow mc.i Generating wrappers for Python % g++ -shared -c krandom.cpp #Some dull C functions % g++ -shared -c mc.cpp #Class definition % g++ -shared -c mc_wrap.c \ #Auto generated -I/bham/ums/common/pd/packages/Python/include/python1.5/ \ -I/bham/ums/solaris/pd/packages/Python/include/python1.5/ Everthing seems to work fine up until... % ld -shared *.o libgcc.a libg++.a libstc++.a -o mc.so ld: fatal: option -h and building a dynamic executable are incompatible ld: fatal: Flags processing errors ...which I seem to get whatever I do whenever I include any of my new .o files. As you can probably tell, I'm using g++ (2.8) under Solaris. There's more info. below for anyone who's suitably interested by now. Any advice welcome. Cheers, Stu ---mc.i--- %module MC %{ #include "mc.h" %} enum ACTION {coast, forward, backward}; class mcar { public: mcar(); double random_pos(); double random_vel(); double reward(); double curr_pos(); double curr_vel(); void set_curr_pos(double pos); void set_curr_vel(double vel); ACTION int_to_act(int action); ACTION choose_random_act(); void update_position_velocity(ACTION a); int reached_goal(); private: double v; double p; }; ---mc.i--- ---mc.h---- class mcar { public: ... some get and setter functions and a constructor with no args - very uninteresting indeed! ... private: double v, p; // current state = velocity and position }; ------------- PS. Does Python recognise .a files (created with ar rather than ld - and what's the difference??) From S.I.Reynolds at cs.bham.ac.uk Wed Sep 30 12:41:34 1998 From: S.I.Reynolds at cs.bham.ac.uk (Stuart I Reynolds) Date: Wed, 30 Sep 1998 11:41:34 +0100 Subject: [C++-SIG] Re: Using SWIG for C++ to Python - ld problem References: <36116E49.686@cs.bham.ac.uk> <8790j2m8tj.fsf@adsl-209-233-33-43.snfc1.pacbell.net> Message-ID: <36120ADE.4B2@cs.bham.ac.uk> Nathan Sullivan wrote: > > Stuart I Reynolds writes: > > > Everthing seems to work fine up until... > > > > % ld -shared *.o libgcc.a libg++.a libstc++.a -o mc.so > > ld: fatal: option -h and building a dynamic executable are incompatible > > ld: fatal: Flags processing errors > > > > ...which I seem to get whatever I do whenever I include any of my new .o > > files. > > > > As you can probably tell, I'm using g++ (2.8) under Solaris. > > Check your $PATH. You're running either /usr/ucb/ld or > /usr/ccs/bin/ld, neither of which support the -shared option. That's > apparently a GNU ld exclusive. If you don't have GNU ld, you'll > either need to get it or figure out the proper options to > /usr/ccs/bin/ld. > > OK, you don't have to figure out the proper options to > /usr/ccs/bin/ld, because I just did. :) The -G option tells the > linker to create a shared object. So, replace -shared with -G, and it > should work. > Aha. That works... thank a lot. (I should've read the manual - doh! Or g++ should've complained!!). Almost there. Now I'm using: % swig -python -shadow -stat -c++ mc.i % g++ -shared -c *.cpp *.cc *.c \ -I /bham/ums/common/pd/packages/Python/include/python1.5/ \ -I/bham/ums/solaris/pd/packages/Python/include/python1.5/ % ld -G -o MCcmodule.so \ /bham/pd/packages/gcc-2.8.0/lib/libg++.a \ /bham/pd/packages/gcc-2.8.0/lib/gcc-lib/sparc-sun-solaris2.6/2.8.0/libgcc.a \ *.o Which seems to work without errors. Now running Python I get: >>> import MC Traceback (innermost last): File "", line 1, in ? File "MC.py", line 2, in ? import MCc ImportError: ld.so.1: python: fatal: relocation error: file ./MCcmodule.so: symbol __builtin_new: referenced symbol not found. I take it "__builtin_new" is in one of the C++ libraries. I've include the c++ libraries libg++.a and libgcc.a because that's what the SWIG manual says. However, we don't seem to have libstc++.a on our system. What's in it / is it the problem? Ideas?? Stuart SWIG Python page http://www.swig.org/Doc1.1/Python.html#n4