Thread: Static libraries containing routines with identical names

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    14

    Static libraries containing routines with identical names

    Hi,

    I have a problem concerning the names of routines contained in static libraries.

    In fact, I have hundreds of libraries written in Fortran, that I need to interface in a unique C program (and I have no way to regenerate these libraries...). The main problem is that all these libraries contain routines with identical names. I really do not know how to make it correctly...

    Thanks in advance for your help,

    Best,
    CCox

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Which compiler are you using?

    I suppose you could link each library with thin wrapper functions (with unique names), then strip all the symbols which would otherwise clash.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    14
    Hi,

    Thanks for your reply. I'm using gcc 4.X, but I'm not sure to understand what you propose...

    Thanks,
    CCox

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Let's say one of your libraries contains a function called foo().

    Then create a simple wrapper function which just calls foo, say
    Code:
    // wrap_foo.c
    int wrap_foo ( int a, double b ) {
      return foo ( a, b );
    }
    Start with say
    ar x libfoo.a
    To extract all the individual object files from the library.

    Then you would do something like
    gcc -c wrap_foo.c
    echo wrap_foo > keepsyms.txt
    ld -r -o wrapped_foo.o --retain-symbols-file keepsyms.txt wrap_foo.o foo.o

    Which
    - compiles the wrapper
    - links the wrapper to the existing function, and removes the unwanted symbols.

    You can then add the new objects back into a library you can link against.

    If the library functions have the same names, I'm guessing they have the same interfaces as well? This could give you some scope with automating this task.

    http://sourceware.org/binutils/docs-...s.html#Options
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    May 2008
    Posts
    14
    Many thanks, I will have a try, and also find a way to automate the process.

    Cheers,
    CCox

  6. #6
    Registered User
    Join Date
    May 2008
    Posts
    14
    Hi,

    In fact, the fortran libs I want to interface with C contain something like hundreds of .o, containing procedures with identical names for all the libraries.

    Even if I understand the principle with one file, I am not sure what to do with all my files.
    Do you think there is a way to assemble all the fortran .o of one library in a single .o, and to perform then the operation you propose?

    Thanks a lot,
    Best,
    CCox

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    So long as the symbols are unique while you're doing it, yes.

    A single wrap.c file for all the Fortran .o files in a single library would work.

    The only downside would be it would not be a library as such, so you'd get all the code linked even if you only wanted part of it. But if you're linking with all of it, there isn't a lot of difference.

    If you do
    file fortran.o

    file is a command which tells you what type of file it is. It should print out something like
    ELF object, not stripped
    or something like it.

    An alternative might be to write an ELF manipulation program (the file format is documented in various places, and IIRC a few libraries to make it easy to do) which simply renames symbols "in situ". But I've no idea how involved that would be, but the end result would be very quick
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    May 2008
    Posts
    14
    OK, thank you, I will think about all this...

    Cheers,
    CCox

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. static keyword and headers. help me understand.
    By cmay in forum C Programming
    Replies: 4
    Last Post: 04-17-2009, 03:19 PM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 01:22 PM
  4. Getting other processes class names
    By Hawkin in forum Windows Programming
    Replies: 3
    Last Post: 03-20-2008, 04:02 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM