g++ on MacOSX doesn't work with -arch ppc64 - macos

I am trying to build a Universal binary on MacOSX with g++. However, it doesn't really work. I have tried with this simple dummy code:
#include <iostream>
using namespace std;
int main() {
cout << "Hello" << endl;
}
This works fine:
% g++ test.cpp -arch i386 -arch ppc -arch x86_64 -o test
% file test
test: Mach-O universal binary with 3 architectures
test (for architecture i386): Mach-O executable i386
test (for architecture ppc7400): Mach-O executable ppc
test (for architecture x86_64): Mach-O 64-bit executable x86_64
However, this does not:
% g++ test.cpp -arch i386 -arch ppc -arch x86_64 -arch ppc64 -o test
In file included from test.cpp:1:
/usr/include/c++/4.2.1/iostream:44:28: error: bits/c++config.h: No such file or directory
In file included from /usr/include/c++/4.2.1/ios:43,
from /usr/include/c++/4.2.1/ostream:45,
from /usr/include/c++/4.2.1/iostream:45,
from test.cpp:1:
/usr/include/c++/4.2.1/iosfwd:45:29: error: bits/c++locale.h: No such file or directory
/usr/include/c++/4.2.1/iosfwd:46:25: error: bits/c++io.h: No such file or directory
In file included from /usr/include/c++/4.2.1/bits/ios_base.h:45,
from /usr/include/c++/4.2.1/ios:48,
from /usr/include/c++/4.2.1/ostream:45,
from /usr/include/c++/4.2.1/iostream:45,
from test.cpp:1:
/usr/include/c++/4.2.1/ext/atomicity.h:39:23: error: bits/gthr.h: No such file or directory
/usr/include/c++/4.2.1/ext/atomicity.h:40:30: error: bits/atomic_word.h: No such file or directory
...
Any idea why that is?
I am on MacOSX 10.6. I have installed Xcode 3.2.2 with all SDKs it comes with. GCC 4.2 is the default. GCC 4.0 produces some different errors, though behaves similar.

ppc64 support was dropped in Snow Leopard. You can still use ppc64 if you build and link against the Mac OS X 10.5 SDK.
Try the following command at the command line:
g++ test.cpp -arch i386 -arch ppc -arch x86_64 -arch ppc64 -mmacosx-version-min=10.5 -isysroot/Developer/SDKs/MacOSX10.5.sdk -DMACOSX_DEPLOYMENT_TARGET=10.5 -o test
Or for the 10.4 SDK use:
g++-4.0 test.cpp -arch i386 -arch ppc -arch x86_64 -arch ppc64 -mmacosx-version-min=10.4 -isysroot/Developer/SDKs/MacOSX10.4u.sdk -DMACOSX_DEPLOYMENT_TARGET=10.4 -o test
Note, if you want to use the 10.4 SDK, you will have to use gcc 4.0 (or g++4.0 ). Apple's GCC 4.2 doesn't support the 10.4 SDK.

Related

Library not loaded: /usr/lib/libncurses.5.4.dylib error on OS X 10.7

I'm trying to update the ncurses-5.4 to ncurses-5.9 on Mac OS X 10.7.
I've built ncurses-5.9
./configure --prefix=/usr/local/ncurses-5.9 --with-shared
To do the update:
sudo mv /usr/lib/libncurses.5.4.dylib /usr/lib/libncurses.5.4.dylib_BACKUP
sudo ln -s =/usr/local/ncurses-5.9/lib/libncurses.5.dylib /usr/lib/libncurses.5.4.dylib
But after I do it, I get this sort of errors:
Dyld Error Message:
Library not loaded: /usr/lib/libncurses.5.4.dylib
Referenced from: /Applications/iTerm.app/Contents/MacOS/iTerm
Reason: Incompatible library version: iTerm requires version 5.4.0 or later,
but libncurses.5.4.dylib provides version 5.0.0
How to update it to ncurses-5.9? Thank you.
Take a look at Apple's configuration. The config.status file happens to tell you what configure-options were used:
with options \"--prefix=/usr --disable-dependency-tracking --disable-mixed-case --with-shared --without-normal --without-debug --enable-termcap --enable-widec --with-abi-version=5.4 --without-cxx-binding --without-cxx --mandir=/usr/share/man 'CFLAGS=-arch i386 -arch x86_64 -arch ppc -g -Os -pipe -isysroot /' 'CXXFLAGS=-arch i386 -arch x86_64 -arch ppc -g -Os -pipe ' 'LDFLAGS=-arch i386 -arch x86_64 -arch ppc '\"
You should read the INSTALL description for each option, to understand what's done, but the pertinent one for your question is this:
--with-abi-version=5.4

Clang: Output a 32 AND 64 bit combination .dylib

I've seen dylibs that are both 32 and 64 bit when performed the file command on. How do I go about building one in Clang?
The -m32 flag creates 32 bit dylib, -m64 makes 64 bit, but using both of them doesn't work.
Edit: For instance, here's the output of file on the type of dylib I'm trying to build
file /Library/Frameworks/SDL.framework/Versions/A/SDL
/Library/Frameworks/SDL.framework/Versions/A/SDL: Mach-O universal binary with 2 architectures
/Library/Frameworks/SDL.framework/Versions/A/SDL (for architecture x86_64): Mach-O 64-bit dynamically linked shared library x86_64
/Library/Frameworks/SDL.framework/Versions/A/SDL (for architecture i386): Mach-O dynamically linked shared library i386
You can either use the -arch argument multiple times for each architecture you want, e.g.
cc -arch i386 -arch x86_64 ...
or create multiple binaries, then glue them together with lipo:
lipo -create -arch i386 my32bitbinary -arch x86_64 my64bitbinary -output myfatbinary

Do I need to give the option (-arch i386) for both compilation/link?

As is asked and answered in this post, I needed to give -arch i386 option for SWIG/C# integration.
Do I need to give the option for both compilation/link?
g++ -c -arch i386 example.cxx example_wrap.cxx
g++ -arch i386 -bundle -undefined suppress -flat_namespace example.o example_wrap.o -o libexample.dylib
Have you tried it? A simple test with a C program on OS X 10.6 with a 64-bit capable machine suggests that, in general, you do need to specify -arch for both.
$ gcc -arch i386 -o x.o x.c
$ gcc x.o -o x.dylib
ld: warning: in x.o, file was built for i386 which is not the architecture being linked (x86_64)
Intuitively, the linker does need to know which set of architecture-specific libraries to link with.

How to compile LibJpeg in universal binary in Mac OSX 10.4

I want to compile jpeg-8b in universal binary (ppc,i386). It should be supported in 10.4 and later OSs. I could do it in 10.5 and 10.6, but the binary is not compatible with 10.4. Thus I tried to compile it in 10.4, but it fails.
This is my code
cd jpeg-8b
sudo ./configure CC="gcc -arch i386 -arch ppc" CXX="g++ -arch i386 -arch ppc" CPP="gcc -E" CXXCPP="g++ -E" -enable-static=yes -enable-shared=no
It fails with the error configure: error: C compiler cannot create executables
How can I compile jpeg-8b in MacOS 10.4?
Can you try compiling it on 10.5/10.6 with 10.4 SDK installed
and using:
export MACOSX_DEPLOYMENT_TARGET="10.4"
export OSX_SDK="/Developer/SDKs/MacOSX10.4.sdk"
export OSX_CFLAGS="-isysroot $OSX_SDK -arch ppc -arch i386"
export OSX_LDFLAGS="-Wl,-syslibroot,$OSX_SDK -arch ppc -arch i386"
export CFLAGS="$CFLAGS $OSX_CFLAGS"
export CXXFLAGS="$CXXFLAGS $OSX_CFLAGS"
export LDFLAGS="$LDFLAGS $OSX_LDFLAGS"
export ARCHFLAGS="-arch ppc -arch i386"
and try adding --disable-dependency-tracking to ./configure
It worked for me after adding export CFLAGS="-arch x86_64 -arch arm64" before calling config. See GitHub gist

how do you get gcc (or llvm) to generate ARM code from the OSX command-line?

On OSX 10.6.4 with iPhone SDK and Xcode 3.2.1 installed:
$ gcc foo.c -arch arm
gcc-4.2: error trying to exec '/usr/bin/arm-apple-darwin10-gcc-4.2.1':execvp: No such file or directory
$ /Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/clang -O3 -arch armv7 bar.c -c

Resources