Linker errors linking to .a files on OS X

Basically my problem is that when I try to compile anything using ./configure && make, it fails because of linker errors. I can reproduce the behavior I'm getting as follows:

I have the two following files

main.c:

#include <stdio.h>

extern void func(void);

int
main(int argc, char **argv)
{
    printf("Before func\n");
    func();
    return 0;
}

func.c:

#include <stdio.h>

void
func(void)
{
    printf("In func\n");
}

If I do the commands

gcc -c func.c
gcc -c main.c
ar rc func.a func.o
gcc -o test main.o func.a

I get the output

ld: warning: ignoring file func.a, file was built for archive which is not the architecture being linked (x86_64): func.a
Undefined symbols for architecture x86_64:
  "_func", referenced from:
      _main in main.o
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status

Any idea what I should do?

When I run those commands with the source files you provided on OS X Yosemite 10.10.1, I don't get any diagnostic messages from ld , and when I run ./test afterwards, I get:

Before func
In func

as I would expect from that code. Please look at the contents of your archive with:

ar -tv func.a

It should show something like:

rw-r--r--     501/20           24 Dec 12 21:41 2014 __.SYMDEF SORTED
rw-r--r--     501/20          816 Dec 12 21:41 2014 func.o

If the __.SYMDEF SORTED entry isn't present, try:

ar -s func.a

which should create (or recreate) the symbol table for the archive.

1 Like

My output for ar -tv func.a is:

rw-r--r-- 502/20    684 Dec 12 20:29 2014 func.o

So I did ar -s func.a, but my output for ar -tv func.a is the same. I might mention that ar worked perfectly fine until I installed MacPorts. I deleted MacPorts but that didn't solve the problem.

Did you follow the MacPorts directions for uninstalling MacPorts, or did you just "rm" it?

I don't have any experience with MacPorts, but glancing through the on-line documentation it seems that MacPorts may change the target architecture to the port you were working on. That would explain the ld diagnostic:

ld: warning: ignoring file func.a, file was built for archive which is not the architecture being linked (x86_64): func.a
Undefined symbols for architecture x86_64:
  "_func", referenced from:
      _main in main.o

but I'm afraid I don't know how to help you correct the problem.

One possibility is that MacPorts might have installed alternative versions of gcc or ar in a directory in your PATH before the standard locations for those utilities. Does type ar gcc ld indicate any directory other than /usr/bin for any of those utilities? If it is something else, try:

/usr/bin/gcc -c func.c main.c
/usr/bin/ar -src func.a func.o
/usr/bin/gcc -o test main.o func.a
1 Like

Also, check the output from the command:
file main.o func.o
It should be something like:

func.o: Mach-O 64-bit object x86_64
main.o: Mach-O 64-bit object x86_64

If you're seeing something other than x86_64 that is the problem. (But, I'm not sure how to tell gcc to produce x86_64 architecture code instead of whatever you're getting.)

Good suggestion on checking my PATH. A weird ar got installed in /usr/local/bin. The one in /usr/bin works as expected. The problem turned out to simpler than I thought. Thanks for your help.