5

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");
}
Eldila
  • 15,426
  • 23
  • 58
  • 62

1 Answers1

-1

These sources and makefile link without complaint using standard Apple tools. Do the tools you use support a “-arch” switch, so you can explicitly specify “-arch x86_64” or “-arch i386”?

You can use the “file” command to see which architecture(s) are in object files and executable files. For library files, you may have to extract the modules and examine them.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
  • I am using 64-bit version of gcc found in macport (gcc46). I am not using the standard apple compiler since it doesn't come with gfortran. I am currently installing gcc46+univerisal. I believe that thin-archive doesn't like the 64-bit arch. – Eldila Aug 17 '12 at 18:42
  • I suggest you use the “file” command to examine each object file and that you extract all the object files from the library and examine them. This should give some indications about which steps are misbehaving. – Eric Postpischil Aug 18 '12 at 01:00