Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gcc on OS X: Undefined symbols for architecture x86_64

Tags:

c

macos

gcc

I am writing an application that has multiple subdirectories. I have created a Makefile structure such that the subdirectories compile the file and do "ar rvs" and "ranlib libxxx.a" to create archives into the parent directory for linking.

However the "ld" command is running into the following problem.

ld: warning: ignoring file ./libxxx.a, file was built for archive which is not
the architecture being linked (x86_64):
./libxxx.a Undefined symbols for architecture x86_64:

I am using gcc on Mac OS X 10.10.1

I read many posts on this. I tried "gcc -arch i386", then I encounter the same error for i386

Undefined symbols for architecture i386:

I installed gcc-4.9.2 and tried using it instead of the default gcc, with no luck. I tried using x86_64-apple-darwin14.0.0-g++-4.9.2 and that did not help either.

like image 291
newprogrammer Avatar asked Oct 31 '22 12:10

newprogrammer


1 Answers

The errors you are seeing mean that you have a mixture of i386 and x86_64 code in the build, and you need to be consistent. Unless there's a compelling reason to do otherwise (I'd be curious to know what it is), you should be compiling everything for 64-bit only. Using gcc on Mac, that is normally the default, but you can force it by adding a -m64 flag to the compilations. One way of doing that is to set CC="gcc -m64" on the make command line; there are other better ways too, but the details depend on your makefile contents.

To resolve: first, remove all the libraries and object code you've built in your project area (maybe make clean will do it — if you wrote a clean target). Then, fettle the value of CC or its flags (CFLAGS ultimately, but how CFLAGS is built depends on the makefile) so that 64-bit compilation is enforced. Then make sure you're using that in all compilations. If you don't see the -m64, you've got a problem.

If you must use 32-bit, substitute -m32 for -m64.

The discussion above assumes you are using gcc to run the ld command. If you are not, then you're on your own until you use gcc to run the ld command. In my view, you've got better things to do with your life than work out how to run the ld command correctly; I certainly have.

like image 179
Jonathan Leffler Avatar answered Nov 08 '22 06:11

Jonathan Leffler