Linking to thin archive on Snow Leopard

Viewed 663

I am attempting to link a thin archive that combines two archive libraries to a C program.

I constructed two simple hello world functions and built an archive with the commands:

ar rcs lib1.a lib1.o
ar rcs lib2.a lib2.o

The two archives are then merged using a thin-archive:

ar rcsT all_lib.a lib1.a lib2.a

and then compiled with gcc:

gcc main.o all_lib.a -o hello

I end up with a error message saying:

ld: warning: ignoring file all_lib.a, file was built for unsupported file format which is not the architecture being linked (x86_64)

Undefined symbols for architecture x86_64: "_func1", referenced from: _main in main.o "_func2", referenced from: _main in main.o ld: symbol(s) not found for architecture x86_64

If I attempt to link main.o with lib1.a and lib2.a directly, everything works.

I am using gcc (MacPorts gcc46 4.6.3_3) 4.6.3 and GNU ar (GNU Binutils) 2.21 on Mac OSX 10.6.8.


Makefile

test1: main.o lib1.o lib2.o
    gcc main.o lib1.a lib2.a -o hello

test2: main.o combine
    gcc main.o all_lib.a -o hello

lib1.o: lib1.c
    gcc -c lib1.c
    ar rcs lib1.a lib1.o

lib2.o: lib2.c
    gcc -c lib2.c
    ar rcs lib2.a lib2.o

combine: lib1.o lib2.o
    ar rcsT all_lib.a lib1.a lib2.a

main.o: main.c
    gcc -c main.c

clean:
    rm -rf *.o  *.a hello

main.c

#include<stdio.h>
#include "lib1.h"
#include "lib2.h"

main()
{
    printf("Hello World\n");
    func1();
    func2();
}

lib1.h

#include<stdio.h>
void func1();

lib2.h

#include<stdio.h>
void func2();

lib1.c

#include "lib1.h"

void func1()
{
    printf("Hello World 1\n");
}

lib2.c

#include "lib2.h"

void func2()
{
    printf("Hello World 2\n");
}
1 Answers
Related