SCIENTIFIC-LINUX-USERS Archives

July 2009

SCIENTIFIC-LINUX-USERS@LISTSERV.FNAL.GOV

Options: Use Monospaced Font
Show Text Part by Default
Show All Mail Headers

Message: [<< First] [< Prev] [Next >] [Last >>]
Topic: [<< First] [< Prev] [Next >] [Last >>]
Author: [<< First] [< Prev] [Next >] [Last >>]

Print Reply
Subject:
From:
Jon Peatfield <[log in to unmask]>
Reply To:
Jon Peatfield <[log in to unmask]>
Date:
Fri, 17 Jul 2009 21:44:51 +0100
Content-Type:
TEXT/PLAIN
Parts/Attachments:
TEXT/PLAIN (82 lines)
On Fri, 17 Jul 2009, Nicola Maggi wrote:

> Hello to everyone,
> maybe this question is a bit off topic but I'm having a lot of trouble
> with some application using shared libraries.

Probably way off... :-)

>
> I have a FPGA .so file which is provided by the FPGA manufacturer.
> In addition I have .cpp/.h files which defines a class to wrap around
> some methods to the DLL entry points.
>
> If I write a stand-alone application to access this library everything
> works fine,I can create objects and access .so functions.
>
> Anyway I  have to build another object library which uses the FPGA library.
> the code to load the library seems to work fine:
> __________________________________________
> okCUsbFrontPanel *device;
> if (FALSE == okFrontPanelDLL_LoadLib(NULL)) {
> 		printf("FrontPanel DLL could not be loaded.\n");
> 		DEBUG_TEXT(DFDB_RCDDETECTOR, 15, "raps::constructor: initialized");
>    	// TODO:throw exception
>  	}else{
> 		printf("Frontpanel DLL loaded.\n");
> 		device= new okCUsbFrontPanel();
>        }
> __________________________________________
> When executing device= new okCUsbFrontPanel(); I get a segfault error
> (exit status 11).
>
> I compile my shared library in this way:
> g++ -shared -ldl -o libReadoutModuleRAPS.so okFrontPanelDLL.o
> ReadoutModuleRAPS.o
>
> where ReadoutModuleRAPS.o is the object of the source in which i used
> the code above.
>
> I really don't understand why does the stand-alone application works
> and the shared one doesn't.
> Does anyone have any idea?

About all I can suggest is adding -fPIC to the compilation of the .cc and 
the the link line - you didn't specify how that was being compiled...

When compiling code to go into shared/dynamic libraries you need to tell 
the compiler that it may be loaded at any memory location.  ie you need 
'position independent code' which is what -fPIC says to do.  See the gcc 
manpage for more details.

...
    -shared
         Produce a shared object which can then be linked with other
         objects to form an executable.  Not all systems support this
         option.  For predictable results, you must also specify the same
         set of options that were used to generate code (-fpic, -fPIC, or
         model suboptions) when you specify this option.[1]
...
    -fpic
        Generate position-independent code (PIC) suitable for use in a
        shared library, if supported for the target machine.  Such code
        accesses all constant addresses through a global offset table
        (GOT).  The dynamic loader resolves the GOT entries when the pro-
        gram starts (the dynamic loader is not part of GCC; it is part of
        the operating system). ...
    -fPIC
        If supported for the target machine, emit position-independent
        code, suitable for dynamic linking and avoiding any limit on the
        size of the global offset table. ...
...

> Thank you,
> Nicola Maggi

I hope it helps.

> ps.I attached the source code of the library- hope I din't break any
> maling list rule.

  -- Jon

ATOM RSS1 RSS2