3

I'm trying to build binutils for generating MIPS code on Mac OS X.

I found this site (http://www.theairportwiki.com/index.php/Building_a_cross_compile_of_GCC_for_MIPS_on_OS_X) from How to build GCC 4.8.x on Mac OS X host for MIPS target, and followed the instructions.

I install the gcc-4.8 from brew, and installed source code of binutils and gcc. This is the compilation option setup.

$ export CC=/usr/local/bin/gcc-4.8
$ export CXX=/usr/local/bin/g++-4.8
$ export CPP=/usr/local/bin/cpp-4.8
$ export LD=/usr/local/bin/gcc-4.8
$ export PREFIX=/opt/cross/gcc-mips
$ export CFLAGS=-Wno-error=deprecated-declarations

Then I configure, and make the bintuils.

The issue is that after building static libraries I have an error message that archive is not built for x86_64, and then I have a bunch of undefined symbol error.

ignoring file ./../intl/libintl.a, file was built for archive which is not the architecture being linked (x86_64): ./../intl/libintl.a

Undefined symbols for architecture x86_64:
  "__bfd_abort", referenced from:
      _fix_new_internal in write.o
      _size_seg in write.o

Googling to find that I need to setup AR (archive) variable also from https://github.com/tpoechtrager/osxcross/issues/11. I added export AR=/usr/local/bin/gcc-ar-4.8, but I have another error message because gcc-ar-4.8 doesn't work.

/usr/local/bin/gcc-ar-4.8 
/usr/local/bin/gcc-ar-4.8: Cannot find plugin 'liblto_plugin.so'

Googling again to find that gcc-ar doesn't work with Mac OS X (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=56893).

gcc-ar is for use with newer GNU binutils only as that is the only ar which supports plugins.  
Apple's ar does not support plugins (though it could be made to; it will be a different plugin interface than the GNU BFD 
plugin interface which GCC supports right now).

I just created a dumb 'liblto_plugin.so' file in /usr/local/Cellar/gcc48/4.8.4/libexec/gcc/x86_64-apple-darwin14.3.0/4.8.4 directory to suppress the error message, but in this case it looks like that /usr/ar is invoked when I use /usr/bin/gcc-ar-4.8 to get the same architecture and undefined symbols error.

How to solve these issues? How to build cross compiler tools (gcc and binutils) on Mac OS X?

Community
  • 1
  • 1
prosseek
  • 182,215
  • 215
  • 566
  • 871
  • 1
    Will any of [these](https://www.macports.org/ports.php?by=category&substr=cross) do? It's much easier to get Macports to build and maintain stuff for you... – Droppy Jun 19 '15 at 20:56
  • @Droppy: I try not to use Macports if possible, because of some issues that I experienced when I used it with brew. http://superuser.com/questions/181337/is-it-safe-to-install-both-homebrew-and-macports-on-the-same-machine – prosseek Jun 19 '15 at 20:59
  • Try not to set `CFLAGS` via env, it's often the cause of problems (i.e. some internal `CFLAGS` stuff won't be set then). You could also get rid of `CPP` and `LD`, there's no need for it. You should also consider replacing `PREFIX` with `./configure --prefix`. – Thomas Jun 19 '15 at 21:05
  • @Thomas: I need the compiler flag to suppress error message ('sbrk' is deprecated), I tried `export CFLAGS+=-Wno-error=deprecated-declarations` to get the same error. – prosseek Jun 19 '15 at 21:12
  • Do not set `CFLAGS` at all, even setting `CFLAGS` may cause troubles (seriously!). Are you sure it's an error and not just a warning? – Thomas Jun 19 '15 at 21:15
  • @Thomas: I also tried without CFLAGS setup, I got the error ('sbrk' is deprecated). – prosseek Jun 19 '15 at 21:17
  • Have you tried `--disable-werror`? – Thomas Jun 19 '15 at 21:18
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/81040/discussion-between-thomas-and-prosseek). – Thomas Jun 19 '15 at 21:22

1 Answers1

3

The static library generator for Mac OS X is not ar, but libtool -static. There is another SO post about this - Static library link issue with Mac OS X: symbol(s) not found for architecture x86_64.

The binutils has multiple libraries that are linked statically. So I replaced all the ar rc command with libtool -static -o to get the static libraries that do not cause errors.

In doing so, I had to make two modifications also.

  1. Some library generate libtool script to conflict the Mac OS X's libtool, I had to rename the script.
  2. Some object files do not contain symbols, I had to remove the objects.

Then I could get the binaries without any issue.

Community
  • 1
  • 1
prosseek
  • 182,215
  • 215
  • 566
  • 871