• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Overriding functions in static library
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Overriding functions in static library


  • Subject: Re: Overriding functions in static library
  • From: Rush Manbert <email@hidden>
  • Date: Tue, 05 Jun 2007 10:37:57 -0700

Robert Purves wrote:
My project links to a static library. Some of the library functions are intended to be overridden, at link time, by definitions in other objects.

Tests with a simple static library (i.e. one that exports functions only) prove successful. It is possible to override none, any, or all of the library's definitions. Overriding works for the reason stated in man ld:
"When a static archive library is specified as an argument to ld, it is searched exactly once, at the point it is encountered in the argument list. Only those members defining an unresolved external reference, as defined by the static archive library's table of contents, are loaded."


Attempts to override functions in my real library, though, fail with an ld error 'multiple definitions of symbol'. My real library differs from the simple test case by exporting global variables in addition to functions. The unexplained failure is reproduceable in a miniature demo, in which the library exports 1 function and 1 global:

# three files: MyLibrary.h, MyLibrary.c, main.c
$ cat MyLibrary.h
extern int gThing;
void AFunction( void );

$ cat MyLibrary.c
int gThing = 0;
void AFunction( void ) {
        gThing = 1;
}

$ cat main.c
#include <stdio.h>
#include "MyLibrary.h"
#if OVERRIDE
void AFunction( void ) {
        gThing = 2;
}
#endif
int main ( void ) {
        printf( "before: %d\n", gThing );
        AFunction();
        printf( "after:  %d\n", gThing );
        return 0;
}

# create static library
$ gcc MyLibrary.c -c -o MyLibrary.o
$ libtool MyLibrary.o -o libMyLibrary.a

# test the library
$

before: 0
after:  1

# attempt to override the library's AFunction()
$ gcc main.c -lMyLibrary -L. -DOVERRIDE && ./a.out
# what I expected: before: 0 after: 2
# what I get instead:
/usr/bin/ld: multiple definitions of symbol _AFunction
/var/tmp//cc63VbXB.o definition of _AFunction in section (__TEXT,__text)
./libMyLibrary.a(MyLibrary.o) definition of _AFunction in section (__TEXT,__text)
collect2: ld returned 1 exit status


#:-(


Hi Robert,

Your problem is that the level of granularity of library linking is the compilation unit. Because your global variable and your default AFunction are in the same source file, everything from that source file will be pulled in when any part of it is pulled in. Since the linker needs gThing, everything else comes with it.

To demonstrate this, I modified your project like this:

$ cat ./MyLibraryVariables.c
int gThing = 0;

$ cat ./MyLibrary.c
#include "MyLibrary.h" /* Gets the extern for gThing */

void AFunction( void ) {
        gThing = 1;
}

# main.c didn't change at all.

# create static lib
$ gcc MyLibrary.c -c -o MyLibrary.o
$ gcc MyLibraryVariables.c -c -o MyLibraryVariables.o
$ libtool MyLibrary.o MyLibraryVariables.o -o libMyLibrary.a

# test the library
$ gcc main.c -lMyLibrary -L. && ./a.out
before: 0
after:  1

# test the override
gcc main.c -lMyLibrary -L. -DOVERRIDE && ./a.out
before: 0
after:  2


So you need to be very careful about how you group your overridable functions. I think that each of them needs to be in its own source file. Otherwise, if you mix them in with other functions that don't get overridden, but are referenced by the program using the library, you will end up with the same multiple definition problem.


- Rush
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Follow-Ups:
    • Re: Overriding functions in static library
      • From: Robert Purves <email@hidden>
References: 
 >Overriding functions in static library (From: Robert Purves <email@hidden>)

  • Prev by Date: FYI: From C++ to Objective-C
  • Next by Date: RE: testing list
  • Previous by thread: Overriding functions in static library
  • Next by thread: Re: Overriding functions in static library
  • Index(es):
    • Date
    • Thread