Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I compile universal code using Macports' GCC?

Since Apple do not support GCC 4.6 or GCC 4.7, I just switched to MacPorts' build of GCC 4.6/4.7. However, I had a problem to build my code in "Universal" architecture.

Traditionally, I'd do g++ -arch i386 -arch x86_64 ... to compile my binary for at least i386/x86_64 architecture. I did some search, and realised that -arch option is only supported by apple's compilers. So of course it didn't work with the Macports'. The best I could do is use -m32/-m64 options to specify an architecture I want. However, this will only produce a binary eitherr in i386 or x86_64 format. What I really want is get a binary file in both architecture (Universal) just as been done with the original GCC compilers.

Is this not supported or a known problem? I have tried the whole night looking for answers on Google, but I haven't get anything useful. So I just start doubt if I am the only one had this problem and I have missed something really important to get it work? Really appreciate if anyone can give me some suggestions.

BTW. I have installed gcc46 +universal variants, but it didn't produce anything really universal except compiling separate binaries for each architecture.

Xing.

like image 434
xing_yu Avatar asked Dec 14 '11 00:12

xing_yu


2 Answers

Oh, I just had this problem and spent a pretty extensive amount of time on it. It turns out that the apple version of GCC 4.2.1 is really just a wrapper that honors a few flags (notably the -arch flag) and then uses lipo to stick the object files back together into a fat object. I modified this driver a bit so that it would wrap macports GCC while still honoring the -arch flag and produce multi arch object files (for 32 bit and 64 bit intel anyway).

I just posted a fairly extensive write up on how to do this:

http://thecoderslife.blogspot.com/2015/07/building-with-gcc-46-and-xcode-4.html

like image 56
John Bowers Avatar answered Nov 13 '22 00:11

John Bowers


You can combine binaries with different architectures in to a single universal using lipo.

like image 32
Mattias Wadman Avatar answered Nov 12 '22 22:11

Mattias Wadman