• 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
Overriding functions in static library
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Overriding functions in static library


  • Subject: Overriding functions in static library
  • From: Robert Purves <email@hidden>
  • Date: Tue, 5 Jun 2007 22:38:26 +1200

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
$ gcc main.c -lMyLibrary -L. && ./a.out
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


#:-(

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


  • Follow-Ups:
    • Re: Overriding functions in static library
      • From: Rush Manbert <email@hidden>
  • Prev by Date: Re: CFBundleLoadExecutable failing to load...
  • Next by Date: testing list
  • Previous by thread: Re: CFBundleLoadExecutable failing to load...
  • Next by thread: Re: Overriding functions in static library
  • Index(es):
    • Date
    • Thread