• 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: Robert Purves <email@hidden>
  • Date: Thu, 7 Jun 2007 09:39:53 +1200


On 6/06/2007, at 5:37 AM, Rush Manbert wrote:

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.

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
#:-(

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:
< snip >
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.

Thank you for the clear explanation.
Changing the library from static to dynamic seems to allow overriding in exactly the way that I intend, at least in the miniature demo.


# create dynamic library
$ gcc MyLibrary.c -c -o MyLibrary.o
$ libtool -dynamic MyLibrary.o -o libMyLibrary.dylib

# override the library's AFunction()
$ gcc main.c -lMyLibrary -L. -D OVERRIDE && ./a.out
/usr/bin/ld: warning multiple definitions of symbol _AFunction
/var/tmp//ccNQ4qaU.o definition of _AFunction in section (__TEXT,__text)
./libMyLibrary.dylib(MyLibrary.o) definition of _AFunction
before: 0
after:  2
#:-)


Robert P.



_______________________________________________
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


References: 
 >Overriding functions in static library (From: Robert Purves <email@hidden>)
 >Re: Overriding functions in static library (From: Rush Manbert <email@hidden>)

  • Prev by Date: Link Error: indirect symbol table entry
  • Next by Date: Re: Link Error: indirect symbol table entry
  • Previous by thread: Re: Overriding functions in static library
  • Next by thread: testing list
  • Index(es):
    • Date
    • Thread