Building Shared Libraries: Python Extension Using C++ and SWIG

I am working through the tutorial examples from http://www.swig.org/ and have run into some problems. I took the following command instructions from the tutorial on swig.org:
http://www.swig.org/tutorial.html
I have written example.c and example.i as described in the above tutorial. My first attempt at compiling the libraries was this:
swig -python example.i
gcc -c example.c example_wrap.c \
-I /System/Library/Frameworks/Python.framework/Versions/Current/Headers
ld -shared example.o example_wrap.o -o _example.so
this last command fails with the following error:
ld: unknown option: -shared
Any help?

Hi, newbie73
I'm also running into trouble with manually linking and creating .so objects using swig.
The good news is, for python there's a shortcut, using the distutils module. Using the example.c and example.i codes from the tutorial, make a python script called 'setup.py':
<pre>
from distutils.core import setup, Extension
setup(name='example',
version='1.0',
ext_modules = [
Extension('example', ['example.c','example.i'] )
</pre>
edit: The discussion markup is messing up this code snippet: line 5 should read:
Extension('example', LEFT BRACKET 'example.c' , 'example.i' ] )
Then run:
python setup.py build_ext
Python will do the compiling and linking for you, and create an example.so file inside a new 'build' directory. To use the .so file, move or rename this file to _example.so in your $PYTHONPATH.
(By the way, I found this information in the excellent textbook Beginning Python by M. Hetland)
I hope that helps!
Caleb
Message was edited by: Caleb Mattoon

Similar Messages

  • How to use g++ to build shared libraries for labview in linux?

    does anyone knows if it's possible to compile a shared library in linux with g++ that could be recognized by labview?
    thks
    JP

    Hi JP,
    Yes you can use g++ to build shared libraries that labview can use. You'll want to give your C++ library a C interface. This is really common practice. You can do this by simply declaring functions as 'extern "C"'. Here is a link with more information:
    http://www.parashift.com/c++-faq-lite/mixing-c-and-cpp.html#faq-30.8
    I've personally worked on projects that do exactly what you want to do.

  • Building shared libraries on linux with SunStudio

    Hi,
    I installed the sunstudio-compilers on my system (SuSE 10.0/x86-64) and try to build a shared library.
    For my (c++-)library I use autoconf/automake, so I hoped it to be trouble-free, but it is not. I configured my library with:
    ./configure CC=cc CXX=CC CFLAGS=-fast CXXFLAGS=-fast -D_GNU_SOURCE(I needed this -D_GNU_SOURCE to have pthread_rwlock_*).
    The compiler run fine, but the linker does not produce any output. I guess the command, which should have created the library is:
    /bin/sh ../libtool --tag=CXX --mode=link CC  -fast -D_GNU_SOURCE   -o libcxxtools.la -rpath /usr/local/lib -lpthread -version-info 3:1:0 base64stream.lo cgi.lo dlloader.lo hdstream.lo hirestime.lo httpreply.lo httprequest.lo iniclass.lo log.lo md5.lo md5stream.lo multifstream.lo net.lo pollclass.lo query_params.lo tcpstream.lo tee.lo thread.lo udp.lo udpstream.lo xmltag.lo iconvstream.lo  ../libltdl/libltdlc.la  -lnslIt creates a src/libcxxtools.la and symbolic links src/.libs/libcxxtools.so and src/.libs/libcxxtools.so.3, but no shared library src/.libs/libcxxtools.so.3.0.1, where the links point to. Is there any chance to get automake running with sunstudio?

    libtool problem is that it has embedded knowledge about compiler options, and it is platform-dependant.
    libtool knows that it should use -G to link shared library with SunStudio on Solaris.
    It has no clue about existance of SunStudio on Linux.
    Anyway, if you are compiling with SunStudio you should always link with SunStudio (cc, CC or f90).
    Thus proper configure line should include LD=CC for C++, LD=cc for C programs.
    If you use default linker (ld) it will fail to resolve dependancies specific to SunStudio compiler.
    Thats what happens in your case.
    However even if you specify LD=CC it will not do shared libraries for the cause mentioned about - when configuring libtool tries to figure out how to build shared library and deciding not to build it at all.
    You can get out with LD=cc (as our cc driver supports gnu-style -shared option).
    Though you will have to specify all the SunStudio C++-specific link dependancies manually.
    Another option is to modify libtool script. Which version do you use?
    regards,
    __Fedor.

  • Implementation of Shared Libraries on Palm Using J2ME

    Hi,
    I am currently working on the Palm Shared Libraries using C...Kind of drag....so I wonder is there any way to implement this on the Palm using J2ME...
    Thanks and Regards,
    Joseph

    From memory, I dont think that J2ME provides a an implementations at the moment that can access the Palm shared libraries, it's too specific for the generic implementation of MIDP, although I've heard of a PDA profile that SUN is working on, may or may not be true , but it's worth checking out : )
    Vance

  • Using sunperf in shared libraries

    Hi,
    I would like to support sunperf in one software project which uses cblas. The code using cblas is a dynamically loadable library (python module). My problem is that using -xlic_lib=sunperf does not work when building shared libraries. More concretely:
    file: floupi.c
    enum CBLAS_ORDER { CblasRowMajor = 101, CblasColMajor = 102 };
    enum CBLAS_TRANSPOSE { CblasNoTrans = 111, CblasTrans =
         112, CblasConjTrans = 113 };
    void cblas_sgemm(const enum CBLAS_ORDER Order,
              const enum CBLAS_TRANSPOSE TransA,
              const enum CBLAS_TRANSPOSE TransB, const int M,
              const int N, const int K, const float alpha,
              const float A, const int lda, const float B,
              const int ldb, const float beta, float *C, const int ldc);
    int main(void)
    int lda = 3;
    float A[] = { 1, 2, 3,
         4, 5, 6
    int ldb = 2;
    float B[] = { 1, 2,
         3, 4,
         5, 6
    int ldc = 2;
    float C[] = { 0.00, 0.00,
         0.00, 0.00
    /* Compute C = A B */
    cblas_sgemm(CblasRowMajor,
              CblasNoTrans, CblasNoTrans, 2, 2, 3,
              1.0, A, lda, B, ldb, 0.0, C, ldc);
    return 0;
    This can be compiled easily using cc floupi.c -xlic_lib=sunperf; the executable works as expected. Now, let's say that instead of main, I use another symbol, to compile the code as a shared library:
    cc -G floupi.c -xlic_lib=sunperf
    Then the built shared library has the cblas_sgemm undefined (according to nm), and ldd shows that libsunperf is not linked. This of course means that for my python module, I have an error with undefined symbols when I try to import it. Why is sunperf not linked at all for shared libraries ? The problem appears both on solaris studio express on x86 and on linux x86 (sunstudio 12 in both cases).
    I also noticed that if instead, I use
    cc -G floupi.c -lsunperf
    Then it seems that cblas_sgemm is not undefined anymore (according to nm), but this looks like linking the cblas_sgemm statically, which is not what I want.
    Thanks,
    David

    I finally found the verbose option for suncc (-#), and this confirms what the above suggested, that is when -G is used, sunperf is not linked:
    ### Note: LD_LIBRARY_PATH = /home/david/local/intel/cc/9.1.042/lib:/home/david/opt/intel/fc/10.0.023//lib:/home/david/opt/intel/cc/10.0.023//lib:/home/david/local/lib:
    ### Note: LD_RUN_PATH = <null>
    /usr/bin/ld -m elf_i386 -dynamic-linker /lib/ld-linux.so.2 --enable-new-dtags /home/david/opt/sun/sunstudio12/prod/lib/crti.o /home/david/opt/sun/sunstudio12/prod/lib/values-xa.o -shared floupi.o -Y "/home/david/opt/sun/sunstudio12/prod/lib:/lib:/usr/lib" -Qy -lc /home/david/opt/sun/sunstudio12/prod/lib/libc_supp.a /home/david/opt/sun/sunstudio12/prod/lib/crtn.o                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       

  • Building a best practice web application using ColdFusion and Jave EE

    I've been tasked with rewriting a software using ColdFusion.  I cannot seem to find a lot of information on best practice development in ColdFusion.  I am an experience Java developer who has never used ColdFusion before.  I want to build this application using a synergy of ColdFusion and Java EE technologies.  Can someone recommend me a book that outlines how to developer in ColdFusion?  Ideally this book assumes the reader is an experienced developer with no exposure to ColdFusion.  Ideally the methods outlined in the book are still "best practice" methods.

    jaisheela wrote:
    Hello Friends,
    I am also in the same situation.
    I am a building a new web application using JSF and AJAX.
    Requirement is I need to use IBM version of DOJO and JSF but I need to develop the whole application using Eclipse 3.3,2 and Tomcat 5.5.
    With IBM version of DOJO and JSF, will Eclipse and Tomcat help to speed up the development or do you suggest me to go for Rational Application Developer and WebSphere Application Server.
    If I need to go with RAD and WAS, then I am new to RAD and WAS, is it easy to use RAD and WAS for this kind of application and implement web applicaiton fast.
    Any feedback will be great help.Those don't sound like requirements of the system to me. They sound more like someone wants to improve their CV/resume
    From what I've read recently, if it's just fast you want, look at Ruby on Rails

  • Building Shared Object for LabView

    All;
    I'm trying to build a C++-based shared object to be called from LabView and use SunCC instead of gcc. The compile stage looks like:danny@traveler:~/tubes/dielectric/rod_software/lookup> make -f Makefile.unix suncc=1
    sunCC -I/usr/local/lv71/cintools -I/home/danny/src/NR_C301/code -c -KPIC -m32 -Di686 lookup.cc
    "/home/danny/src/NR_C301/code/interp_1d.h", line 183: Warning: n hides Base_interp::n.
    "/home/danny/src/NR_C301/code/mins_ndim.h", line 103: Warning: n hides Linemethod<extern "C" double(const NRvector<double>&)>::n.
    "lookup.cc", line 176:     Where: While instantiating "Powell<extern "C" double(const NRvector<double>&)>::minimize(const NRvector<double>&)".
    "lookup.cc", line 176:     Where: Instantiated from non-template code.
    2 Warning(s) detected.and the load stage fails like this:sunCC -G -m32 -o lookup.so lookup.o /usr/local/lv71/AppLibs/liblvrt.so.7.1 /usr/lib/libGL.so /usr/lib/libOSMesa.so.6
    /opt/sun/sunstudio12/prod/lib/crtn.o:(.text+0x0): multiple definition of `_etext'
    make: *** [lookup.so] Error 1Is my problem obvious? Is there a simple example on how to build SOs? I found the switch definitions in the Sun documentation, but no good examples yet.
    ...Dan

    If your library links to other shared libraries, use -L options to point to directories other than system directories, and the -l option for the library name after "lib".
    When building shared libraries, you also need to list all system libraries explicity, unfortunately, to ensure your library has the right dependencies.
    You should also add -zdefs to force the linker to complain about unresolved symbols. The default when building shared libraries is not to warn about them.
    You want to be sure you have included all the needed libraries in the link.
    Your link command should look like this :
    sunCC -G -m32 -o lookup.so lookup.o -zdefs \
      -L /usr/local/lv71/AppLibs -llvrt.so.7.1 -lGL.so -lOSMesa.so.6 -lCstd -lCrun -lm -lcYou don't need a -L option for /usr/lib or the directories in the Sun Studio installation. The CC driver knows where to find system libraries.
    That said, I don't think the command line issues are the cause of the multiple definition error.
    Binaries created by Sun C++ are not compatible with binaries created by other C++ compilers like g++. Is liblvrt.so.7.1 a g++ library?

  • Troubles with shared libraries

    I have some problems with shared libraries. This is the partial result of a ./configure
    checking if libtool supports shared libraries... no
    checking whether to build shared libraries... no
    checking whether to build static libraries... yes
    checking for ld used by g++... /usr/bin/ld
    checking if the linker (/usr/bin/ld) is GNU ld... yes
    checking whether the g++ linker (/usr/bin/ld) supports shared libraries... no
    checking for g++ option to produce PIC... -fPIC -DPIC
    checking if g++ PIC flag -fPIC -DPIC works... yes
    checking if g++ static flag -static works... yes
    checking if g++ supports -c -o file.o... yes
    checking if g++ supports -c -o file.o... (cached) yes
    checking whether the g++ linker (/usr/bin/ld) supports shared libraries... no
    checking dynamic linker characteristics... no
    Why libtool and the g++ linker don't support shared libraries?
    Searching in the forum i saw other people having a positive answer to the checks. Did i make something wrong?
    Thanks.

    "This is the partial result of a ./configure" - of *what* app? Look at the configure script that you're running.

  • Finding other users Shared libraries

    Hello,
    Where I work there are people who have Macs and have shared their music libraries in iTunes. I used to see the Shared category when I launched my iTunes. I have moved to a new building in our complex, and I no longer see the shared libraries that I used to listen to. I read somewhere about having to be on the same network subnet (submask) as the people sharing their music? Unfortunately, I'm now one of the few Macs in the new building, and there's no one here sharing a music library. Am I totally hosed or is there a way to navigate to where the music is? We are all on the same NT workgroup.
    Regards,
    Zoar

    I don't have a specific answer toy our question, but to address you comment about being the only Mac user... You don't have to be on the same platform to share music. Macs and Windows can share iTunes libraries, at least that was certainly true for iTunes PRE-version 8. I assume it is true now.
    Patrick

  • Level building/shared members

    Can you generate shared members when level building your dimensions?

    I'm afraid I have to disagree with the previous 2 posters. You can build shared members when you use a level build method. I do it all the time. Assume your level zero member is in a primary and secondary rollup and that there are 3 parents in the firs rollup and 2 in the second, the format of the load rule would be:
    Lev0,Lev1,Lev2,Lev3,lev1,Lev2

  • Linux JVM: How to load shared libraries with mutual/circular dependencies

    Hi,
    I am using the API
    System.loadLibrary(String name)
    to dynamically link shared libaries to Linux JVM.
    The shared libraries has complex circular symbol dependencies among them.
    I am not able to load shared libraries due to java.lang.UnsatisfiedLinkError.
    Any ideas on how a number of shared libraries with interdependent realtionships
    could be successfully linked in to the Linux JVM?
    Thanks,
    Soumen.
    Background
    ===========
    Successful execution of native code within Java virtual machine in a
    host platform, has two necessary prerequisites:
    Prerequisite 1: The native code is written to the JNI specification
    Prerequisite 2: The native code is dynamically linked with Java virtual
    machine.
    The second step is achieved via simple Java interface, System.loadLibrary:
    http://java.sun.com/j2se/1.4.2/docs/api/java/lang/System.html#loadLibrary(java.lang.String)
    However, implementation of this interface is highly platform dependent
    in regards to exact nature of dynamic linking process.
    Tests
    =====
    I made three shared libraries libfeatureA.so, libfeatureB.so and libfeatureC.so.
    Feature A and Feature B are mutually dependent on each other (i.e cicular
    dependencies between libfeatureA.so, libfeatureB.so) whereas libfeatureC.so
    has no external dependencies.
    Case 1
    I could link libfeatureC.so with java.
    Case 2
    Java failed to resolve circular dependency between libfeatureA.so, libfeatureB.so
    resulting in linking failure. There may be repackaging workarounds which I have
    not yet explored.
    Java source code
    ================
    class TestLoadLibrary
    static
    System.loadLibrary("featureB");
    System.loadLibrary("featureA");
    System.loadLibrary("featureB");
    public static void main(String[] args)
    System.out.println("In TestLoadLibrary.main()");
    Exception in thread "main" java.lang.UnsatisfiedLinkError: /homes/soumen/work/event/featureB/libfeatureB.so.1.0: /homes/soumen/work/ev B.so.1.0: undefined symbol: featureA_func2
    at java.lang.ClassLoader$NativeLibrary.load(Native Method)
    at java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1737)
    at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1662)
    at java.lang.Runtime.loadLibrary0(Runtime.java:817)
    at java.lang.System.loadLibrary(System.java:986)
    at TestLoadLibrary.<clinit>(TestLoadLibrary.java:5)

    The use of "dlopen() application programming" to achieve dynamic link closure works if there are
    no circular dependencies. For example, I have the following dependency:
    libfeatureD.so ==> libfeatureC.so
    Even if I call Java APIs in correct dependency order, i.e.,
    System.loadLibrary("featureC");
    System.loadLibrary("featureD");
    there would be java.lang.UnsatisfiedLinkError for call System.loadLibrary("featureD").
    Now, I have a JNI method implementation, which makes native API call in same dependency order:
    dlopen("libfeatureC.so", RTLD_LAZY|RTLD_GLOBAL);
    dlopen("libfeatureD.so", RTLD_LAZY|RTLD_GLOBAL);
    and now System.loadLibrary calls succeed. Note that I had to set environment
    variable LD_LIBRARY_PATH so that dlopen call would find libraries to load.
    In other words, from a JNI programming point of view, "linking chasm" has been crossed.
    I am looking at circular dependency issue, i.e.,
    libfeatureA.so <==> libfeatureB.so
    There may be library repackaging workaround.
    If anybody knows any workaround, please post.
    -Soumen Sarkar
    JNI code
    ========
    #include <jni.h>
    #include "TestLoadLibrary.h"
    #include <stdio.h>
    #include <string.h>
    #include <dlfcn.h>
    JNIEXPORT void JNICALL Java_TestLoadLibrary_libLoadNative
    (JNIEnv * jenv, jclass class, jobjectArray libNames)
    printf("native: In TestLoadLibrary.libLoadNative()\n");
    char linuxLibName[1024];
    jsize idx = 0;
    jsize nLibs = (*jenv)->GetArrayLength(jenv, libNames);
    for(idx = 0; idx < nLibs; ++idx)
    /* obtain the current object from the object array */
    jobject myObject = (*jenv)->GetObjectArrayElement(jenv, libNames, idx);
    /* Convert the object just obtained into a String */
    const char libName = (jenv)->GetStringUTFChars(jenv,myObject,0);
    strcpy(linuxLibName, "lib");
    strcat(linuxLibName, libName);
    strcat(linuxLibName, ".so");
    printf("\nnative: Trying to open lib with name %s\n", linuxLibName);
    void* handle = dlopen(linuxLibName, RTLD_LAZY|RTLD_GLOBAL);
    if(handle == 0)
    fputs ("\nnative: Could not load lib\n", stderr);
    fputs (dlerror(), stderr);
    else
    printf("\nnative: Loaded lib %s\n", linuxLibName);
    /* Free up memory to prevent memory leaks */
    (*jenv)->ReleaseStringUTFChars(jenv, myObject, libName);
    return;

  • Panda3d 1.6.2-3 binary programs can't find shared libraries

    This was a bit of an issue for me, and I wanted to see if this was an issue for anyone else.
    Initially after installing panda3d 1.6.2-3 I had issues running the binary programs it provided, in particular punzip. punzip would complain about not finding the required shared libraries (such as libpanda.so), and adding symbolic links to the libraries in /opt/panda3d/lib tended to solve these issues. Adding /opt/panda3d/lib to /etc/ld.so.conf and running ldconfig solve this issue completely.
    I checked the PKGBUILD and noticed that a profile file (panda3d.profile) is installed into /etc/profile.d as panda3d.sh which exports /opt/panda3d/bin and /opt/panda3d/lib to the PATH and LD_LIBRARY_PATH environmental variables, respectively. When I run `echo $LD_LIBRARY_PATH ` I do get ':/opt/panda3d/lib' as the output so the path is getting exported correctly. However, I've confirmed that removing the /opt/panda3d/lib entry in /etc/ld.so.conf causes many (if not all) of the binary programs panda3d installs to loose track of their shared libraries even when /opt/panda3d/lib is clearly in the LD_LIBRARY_PATH variable.
    Am I the only one experiencing these issues? I'd be interested to know why my system isn't working as intended Obviously if this is a common issue adding a line to the PKGBUILD to add the entry to /etc/ld.so.conf might be a good solution.
    - Zaridu

    report this issue on bugtracker. seems to me that is a bug.

  • Buling a new Web Application using JSF and Ajax.

    Hello Group,
    I am a building a new web application using JSF and AJAX. Planning to use Myfaces Tomahawk, Dojo for Ajax, Hibernate, Spring,Eclipse IDE and Jetty Server.Can some one please suggest me will this be a right one for
    building complex UI and will it support for using the jsf features and would like to know any other free open
    source framework, ide, tools which support the best way for an agile project..?. There is restriction like i have to use java1.4

    jaisheela wrote:
    Hello Friends,
    I am also in the same situation.
    I am a building a new web application using JSF and AJAX.
    Requirement is I need to use IBM version of DOJO and JSF but I need to develop the whole application using Eclipse 3.3,2 and Tomcat 5.5.
    With IBM version of DOJO and JSF, will Eclipse and Tomcat help to speed up the development or do you suggest me to go for Rational Application Developer and WebSphere Application Server.
    If I need to go with RAD and WAS, then I am new to RAD and WAS, is it easy to use RAD and WAS for this kind of application and implement web applicaiton fast.
    Any feedback will be great help.Those don't sound like requirements of the system to me. They sound more like someone wants to improve their CV/resume
    From what I've read recently, if it's just fast you want, look at Ruby on Rails

  • How can I build a personal web site using...

    Hi,friends on Oracle world.
    I am a new starter in oracle forms and AS.Nowdays,I am interested to build a personal web site using forms and other tech. such as flash. Can I do that ?Would you plz give me some suggestions?

    All the information about running Forms on the web is at:
    http://otn.oracle.com/formsupgrade
    I would also suggest that you would look at Oracle9iAS Portal for the design of your website.
    look at http://otn.oracle.com/products

  • How do I import music using homeshare with iTunes 11...I can see the shared libraries but I can't drag and drop and there is no import button.  Thanks!

    How do I import music using homeshare with iTunes 11...I can see the shared libraries but I can't drag and drop and there is no import button.  Thanks!

    Yes, that appears how Mavericks works right now. However, you can open the Address Panel, select all your contacts, then click the To (or CC, or Bcc) buttons to move the selection to the address field.
    If it is not already in the Toolbar, right-click on the Toolbar and select Customize Toolbar…
    Then, drag the address panel up to wherever you want it.
    You may also want to add it to the New Message window in the same way.

Maybe you are looking for