Precompiled headers and compiling universal objects on OSX

Your problem is not the architectures. Both are failing

The issue is that you are trying to build a executable without a main function.

As the file name is commonlib.c I suspect you want to build a library if so start the project with a library template in XCode.


I just ran into the same questions and followed up with the link provided by @lucas, so I thought I would provide what I found here.

First of note, if you are porting your gcc code from Linux to MacOS, the version of gcc provided by apple does not properly detect .hpp file extension.

mac:openstudio lefticus$ g++ test.hpp
ld: warning: ignoring file test.hpp, file was built for unsupported file format which is not the architecture being linked (x86_64)
Undefined symbols for architecture x86_64:
  "_main", referenced from:
      start in crt1.10.6.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status

As mentioned in another answer, it's best to specify the -x argument to make sure gcc knows what type of file you are compiling.

g++ -x c++-header test.hpp

This creates the expected test.hpp.gch.

You can specify any architecture on the command line and the gch builds properly

g++ -x c++-header test.hpp -arch i386

or

g++ -x c++-header test.hpp -arch x86_64

If you provide more than one architecture, you get the error the poster mentioned.

mac:openstudio lefticus$ g++ -xc++-header test.hpp -arch i386 -arch x86_64
Undefined symbols for architecture i386:
  "_main", referenced from:
      start in crt1.10.6.o
ld: symbol(s) not found for architecture i386
collect2: ld returned 1 exit status
Undefined symbols for architecture x86_64:
  "_main", referenced from:
      start in crt1.10.6.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
lipo: can't open input file: /var/folders/DM/DMTpbjzHHX08IEqGgEAORE+++TI/-Tmp-//ccDeWigf.out (No such file or directory)

The key is to compile the architectures you need separately then use the -Xarch_ argument to load the appropriate one during compilation:

g++ -x c++-header -arch x86_64 x86_64/test.hpp
g++ -x c++-header -arch i386 i386/test.hpp

g++ -arch i386 -arch x86_64 test.cpp -Xarch_i386 -Ii386 -Xarch_x86_64 -Ix86_64