Ale*_*lds 6 macos gcc gnu darwin cross-compiling
问题概述
我正在尝试学习如何在gcc针对i386-apple-darwin(32位Intel)的64位Intel OS X主机上构建交叉编译器,特别是GNU 4.7.2 版本.但是,我一直陷入同样的错误:
...
checking for i386-apple-darwin-gcc... /Users/alexpreynolds/Developer/third-party/build-gcc/./gcc/xgcc -B/Users/alexpreynolds/Developer/third-party/build-gcc/./gcc/ -B/Users/alexpreynolds/Developer/third-party/i386-apple-gcc/i386-apple-darwin/bin/ -B/Users/alexpreynolds/Developer/third-party/i386-apple-gcc/i386-apple-darwin/lib/ -isystem /Users/alexpreynolds/Developer/third-party/i386-apple-gcc/i386-apple-darwin/include -isystem /Users/alexpreynolds/Developer/third-party/i386-apple-gcc/i386-apple-darwin/sys-include
checking for suffix of object files... configure: error: in `/Users/alexpreynolds/Developer/third-party/build-gcc/i386-apple-darwin/libgcc':
configure: error: cannot compute suffix of object files: cannot compile
See `config.log' for more details.
make[1]: *** [configure-target-libgcc] Error 1
make: *** [all] Error 2
Run Code Online (Sandbox Code Playgroud)
构建过程
我首先构建一个GNU binutils目标版本,i386-apple-darwinGNU gcc构建文件夹可以访问它:
$ cd third-party
$ wget http://ftp.gnu.org/gnu/binutils/binutils-2.23.2.tar.bz2
$ tar xjvf binutils-2.23.2.tar.bz2
$ mkdir i386-apple-darwin
$ mkdir build-binutils && cd build-binutils
$ ../binutils-2.23.2/configure --target=i386-apple-darwin --prefix="/Users/alexpreynolds/Developer/third-party/i386-apple-gcc"
$ make all && make install
...
Run Code Online (Sandbox Code Playgroud)
然后我尝试构建gcc如下的目标版本:
$ cd ..
$ pwd
/Users/alexpreynolds/Developer/third-party
$ wget http://ftp.gnu.org/pub/gnu/gcc/gcc-4.7.2/gcc-4.7.2.tar.bz2
$ tar xvjf gcc-4.7.2.tar.bz2
$ cd gcc-4.7.2
$ ./contrib/download_prerequisites
... /* this grabs GMP, MPC and MPFR dependencies */
$ cd ..
$ mkdir build-gcc && cd build-gcc
$ ../gcc-4.7.2/configure --target=i386-apple-darwin --prefix="/Users/alexpreynolds/Developer/Bedops/bedops/third-party/i386-apple-gcc"
$ make
...
Run Code Online (Sandbox Code Playgroud)
编译完东西大约30分钟后,它会被上面显示的错误信息卡住.
错误详情
要跟踪错误的来源,请参阅以下代码段build-gcc/i386-apple-darwin/libgcc/config.log:
configure:3344: /Users/alexpreynolds/Developer/third-party/build-gcc/./gcc/xgcc -B/Users/alexpreynolds/Developer/third-party/build-gcc/./gcc/ -B/Users/alexpreynolds/Developer/third-party/i386-apple-gcc/i386-apple-darwin/bin/ -B/Users/alexpreynolds/Developer/third-party/i386-apple-gcc/i386-apple-darwin/lib/ -isystem /Users/alexpreynolds/Developer/third-party/i386-apple-gcc/i386-apple-darwin/include -isystem /Users/alexpreynolds/Developer/third-party/i386-apple-gcc/i386-apple-darwin/sys-include -V >&5
xgcc: error: unrecognized command line option '-V'
xgcc: fatal error: no input files
compilation terminated.
configure:3355: $? = 1
configure:3344: /Users/alexpreynolds/Developer/third-party/build-gcc/./gcc/xgcc -B/Users/alexpreynolds/Developer/third-party/build-gcc/./gcc/ -B/Users/alexpreynolds/Developer/third-party/i386-apple-gcc/i386-apple-darwin/bin/ -B/Users/alexpreynolds/Developer/third-party/i386-apple-gcc/i386-apple-darwin/lib/ -isystem /Users/alexpreynolds/Developer/third-party/i386-apple-gcc/i386-apple-darwin/include -isystem /Users/alexpreynolds/Developer/third-party/i386-apple-gcc/i386-apple-darwin/sys-include -qversion >&5
xgcc: error: unrecognized command line option '-qversion'
xgcc: fatal error: no input files
compilation terminated.
configure:3355: $? = 1
configure:3371: /Users/alexpreynolds/Developer/third-party/build-gcc/./gcc/xgcc -B/Users/alexpreynolds/Developer/third-party/build-gcc/./gcc/ -B/Users/alexpreynolds/Developer/third-party/i386-apple-gcc/i386-apple-darwin/bin/ -B/Users/alexpreynolds/Developer/third-party/i386-apple-gcc/i386-apple-darwin/lib/ -isystem /Users/alexpreynolds/Developer/third-party/i386-apple-gcc/i386-apple-darwin/include -isystem /Users/alexpreynolds/Developer/third-party/i386-apple-gcc/i386-apple-darwin/sys-include -o conftest -g -O2 conftest.c >&5
Assembler messages:
Fatal error: invalid listing option `r'
configure:3374: $? = 1
configure:3562: checking for suffix of object files
configure:3584: /Users/alexpreynolds/Developer/third-party/build-gcc/./gcc/xgcc -B/Users/alexpreynolds/Developer/third-party/build-gcc/./gcc/ -B/Users/alexpreynolds/Developer/third-party/i386-apple-gcc/i386-apple-darwin/bin/ -B/Users/alexpreynolds/Developer/third-party/i386-apple-gcc/i386-apple-darwin/lib/ -isystem /Users/alexpreynolds/Developer/third-party/i386-apple-gcc/i386-apple-darwin/include -isystem /Users/alexpreynolds/Developer/third-party/i386-apple-gcc/i386-apple-darwin/sys-include -c -g -O2 conftest.c >&5
Assembler messages:
Fatal error: invalid listing option `r'
configure:3588: $? = 1
configure: failed program was:
| /* confdefs.h */
| #define PACKAGE_NAME "GNU C Runtime Library"
| #define PACKAGE_TARNAME "libgcc"
| #define PACKAGE_VERSION "1.0"
| #define PACKAGE_STRING "GNU C Runtime Library 1.0"
| #define PACKAGE_BUGREPORT ""
| #define PACKAGE_URL "http://www.gnu.org/software/libgcc/"
| /* end confdefs.h. */
|
| int
| main ()
| {
|
| ;
| return 0;
| }
configure:3602: error: in `/Users/alexpreynolds/Developer/third-party/build-gcc/i386-apple-darwin/libgcc':
configure:3605: error: cannot compute suffix of object files: cannot compile
See `config.log' for more details.
Run Code Online (Sandbox Code Playgroud)
我正在引导的编译器
$ gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/opt/local/libexec/gcc/x86_64-apple-darwin12/4.7.2/lto-wrapper
Target: x86_64-apple-darwin12
Configured with: ../gcc-4.7.2/configure --prefix=/opt/local --build=x86_64-apple-darwin12 --enable-languages=c,c++,objc,obj-c++,lto,fortran,java --libdir=/opt/local/lib/gcc47 --includedir=/opt/local/include/gcc47 --infodir=/opt/local/share/info --mandir=/opt/local/share/man --datarootdir=/opt/local/share/gcc-4.7 --with-libiconv-prefix=/opt/local --with-local-prefix=/opt/local --with-system-zlib --disable-nls --program-suffix=-mp-4.7 --with-gxx-include-dir=/opt/local/include/gcc47/c++/ --with-gmp=/opt/local --with-mpfr=/opt/local --with-mpc=/opt/local --with-ppl=/opt/local --with-cloog=/opt/local --enable-cloog-backend=isl --disable-cloog-version-check --enable-stage1-checking --enable-lto --enable-libstdcxx-time --with-as=/opt/local/bin/as --with-ld=/opt/local/bin/ld --with-ar=/opt/local/bin/ar --with-bugurl=https://trac.macports.org/newticket --disable-ppl-version-check --with-pkgversion='MacPorts gcc47 4.7.2_2+universal'
Thread model: posix
gcc version 4.7.2 (MacPorts gcc47 4.7.2_2+universal)
Run Code Online (Sandbox Code Playgroud)
题
有我设立的构建环境错过了一步coreutils还是gcc这样,这个错误的结果吗?
在尝试解决此错误时,我还可以解决哪些问题?我对这个错误消息的谷歌搜索没有多少成果.
变化
我更改了构建脚本,如下所示:
#!/bin/bash -x
WORK_DIR=$PWD
INSTALL_DIR=$WORK_DIR/gcc-4.7.2-darwin
MIN_OSX_VERSION=10.5
if [ -d "$INSTALL_DIR" ]; then
echo "Directory $INSTALL_DIR already exists";
exit 1
fi
BINUTILS=binutils-2.23.2
TARBALL_BINUTILS=$BINUTILS.tar.bz2
URL_BINUTILS=http://ftp.gnu.org/gnu/binutils/$TARBALL_BINUTILS
SOURCE_BINUTILS_DIR=$WORK_DIR/$BINUTILS
BUILD_BINUTILS_DIR=$WORK_DIR/build-$BINUTILS
GCC=gcc-4.7.2
TARBALL_GCC=$GCC.tar.bz2
URL_GCC=http://ftp.gnu.org/pub/gnu/gcc/$GCC/$TARBALL_GCC
SOURCE_GCC_DIR=$WORK_DIR/$GCC
BUILD_GCC_DIR=$WORK_DIR/build-$GCC
curl -O $URL_BINUTILS
mkdir -p $SOURCE_BINUTILS_DIR
tar -xjvf $TARBALL_BINUTILS
rm $TARBALL_BINUTILS
mkdir -p $BUILD_BINUTILS_DIR
cd $BUILD_BINUTILS_DIR
$SOURCE_BINUTILS_DIR/configure --prefix=$INSTALL_DIR
make -j $(sysctl -n hw.availcpu) all CFLAGS='-Wno-error' && make install
find $INSTALL_DIR -name '*strip*' -delete
cd $WORK_DIR
curl -O $URL_GCC
mkdir -p $SOURCE_GCC_DIR
tar -xvjf $TARBALL_GCC
rm $TARBALL_GCC
cd $SOURCE_GCC_DIR
sed -i '' -e 's/wget/curl -O/g' $SOURCE_GCC_DIR/contrib/download_prerequisites
$SOURCE_GCC_DIR/contrib/download_prerequisites
cd $WORK_DIR
mkdir -p $BUILD_GCC_DIR
cd $BUILD_GCC_DIR
$SOURCE_GCC_DIR/configure --prefix=$INSTALL_DIR --enable-languages=c,c++
make -j $(sysctl -n hw.availcpu) CFLAGS="-mmacosx-version-min=$MIN_OSX_VERSION" && make install
find $INSTALL_DIR -name '*.a' -print0 | xargs -0 -n1 ranlib
rm -Rf $BUILD_GCC_DIR
rm -Rf $BUILD_BINUTILS_DIR
rm -Rf $SOURCE_GCC_DIR
rm -Rf $SOURCE_BINUTILS_DIR
Run Code Online (Sandbox Code Playgroud)
ranlib由于在构建库的过程中存在大量错误,因此该步骤是必要的.
要构建i386二进制文件,我必须对我执行以下操作Makefile:
ARCH = i386
...
GCCDIR = ${PARTY3}/gcc-4.7.2-darwin
GCCBINDIR = ${GCCDIR}/bin
GCCLIBDIR = ${GCCDIR}/lib
GCCINCDIR = ${GCCDIR}/include
...
LIBCPP = libgcc_eh.a
LIBCXX = libstdc++.a
...
INCLUDES = -I${GCCINCDIR} ...
...
CC = ${GCCBINDIR}/gcc
CXX = ${GCCBINDIR}/g++
...
GCCCPPLIBPATH = ${GCCLIBDIR}/gcc/x86_64-apple-darwin12.3.0/4.7.2/${ARCH}/${LIBCPP}
GCCCXXLIBPATH = ${GCCLIBDIR}/${ARCH}/${LIBCXX}
...
FLAGS = ${SFLAGS} ${NONSTATICFLAGS} ${OBJS} -L${GCCLIBDIR} ...
...
run: dependencies
mkdir -p ${BIN} && ${CXX} -o ${BIN}/${PROG}_${ARCH} ${FLAGS} -static-libgcc -mmacosx-version-min=${MIN_OSX_VERSION} -arch ${ARCH} ... ${GCCCPPLIBPATH} ${GCCCXXLIBPATH} ${SOURCE1}
Run Code Online (Sandbox Code Playgroud)
Makefile变更摘要:
我不得不添加-static-libgcc标志并省略-static-libstdc++,这没有任何效果(除了导致构建失败,直到我运行ranlib静态库,这修复了构建失败但没有影响-static-libstdc++).
我加${GCCCPPLIBPATH}和${GCCCXXLIBPATH}到run目标,它们分别指向静态C ANC C++库libgcc_eh.a和libstdc++.a分别.
这允许我编译一个工作i386(32位)二进制文件,但相同的GCC安装无法构建一个工作x86_64(64位)二进制文件.
稍微思考了一下之后,我想到迄今为止最简单的解决方案是为 x86_64 编译 GCC,然后通过-m32当您希望为 i386 构建时就通过。由于某种原因这还不足以满足您的需求吗?以这种方式构建要简单得多,并且不需要下面描述的任何疯狂操作。执行以下事件序列会生成一个工作编译器,该编译器将在请求时输出 i386 二进制文件:
WORK_DIR=$PWD\nINSTALL_DIR=$WORK_DIR/install\n\ncurl -O http://ftp.gnu.org/gnu/binutils/binutils-2.23.2.tar.bz2\ntar xjvf binutils-2.23.2.tar.bz2\nmkdir build-binutils && cd build-binutils\n../binutils-2.23.2/configure --prefix=$INSTALL_DIR\nmake -j $(sysctl -n hw.availcpu) all CFLAGS=\'-Wno-error\' && make install\n\nfind $INSTALL_DIR -name \'*strip*\' -delete\n\ncd $WORK_DIR\n\ncurl -O http://ftp.gnu.org/pub/gnu/gcc/gcc-4.7.2/gcc-4.7.2.tar.bz2\ntar xvjf gcc-4.7.2.tar.bz2\ncd gcc-4.7.2\nsed -i \'\' -e \'s/wget/curl -O/g\' ./contrib/download_prerequisites\n./contrib/download_prerequisites\ncd ..\nmkdir build-gcc && cd build-gcc\n../gcc-4.7.2/configure --prefix=$INSTALL_DIR --enable-languages=c,c++\nmake -j $(sysctl -n hw.availcpu) && make install\nRun Code Online (Sandbox Code Playgroud)\n\n完成后,您可以看到它的运行情况,如下所示:
\n\nmrowe@falcon:~/tmp$ cat > test.cpp\n#include <iostream>\n\nint main(int argc, char **argv) {\n std::cerr << "Built with GCC 4.7" << std::endl;\n return 0;\n}\n\n^D\nmrowe@falcon:~/tmp$ ./install/bin/g++ -o test test.cpp \nmrowe@falcon:~/tmp$ file test\ntest: Mach-O 64-bit executable x86_64\nmrowe@falcon:~/tmp$ ./install/bin/g++ -m32 -static-libgcc -static-libstdc++ -o test test.cpp \nmrowe@falcon:~/tmp$ file test\ntest: Mach-O executable i386\nmrowe@falcon:~/tmp$ otool -L ./test\n./test:\n /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1192.0.0)\nmrowe@falcon:~/tmp$ ./test \nBuilt with GCC 4.7\nmrowe@falcon:~/tmp$ \nRun Code Online (Sandbox Code Playgroud)\n\n如果您看到如下警告:
\n\nignoring file \xe2\x80\xa6/4.7.2/i386/libgcc.a, file was built for archive which is not the architecture being linked (i386)\nRun Code Online (Sandbox Code Playgroud)\n\nranlib您可以通过在静态库上运行来修复它们:
find $INSTALL_DIR -name \'*.a\' -print0 | xargs -0 -n1 ranlib\nRun Code Online (Sandbox Code Playgroud)\n\n以下是我试图帮助您沿着您试图遵循的道路前进的尝试。我认为这不是理想的方法,而且充满了问题。
\n\n您报告看到的错误是由于您的 GCC 构建尝试使用 binutils 中的 GNU 汇编器,而生成的程序集旨在使用 Apple 的 cctools 中的 GNU 汇编器进行汇编。两者所支持的汇编语言有所不同,因为 Apple 的汇编器多年前就与上游 GNU 汇编器分道扬镳。
\n\n我能够找到的用于构建 cctools 的所有说明都已过时,因此我进行了实验,直到找到工作为止。我做了一些接近以下内容的事情:
\n\ncurl -O http://opensource.apple.com/tarballs/cctools/cctools-829.tar.gz\ntar xvzf cctools-829.tar.gz\ncd cctools-829\nRun Code Online (Sandbox Code Playgroud)\n\n然后,您需要修补两个文件,以便在不依赖 LLVM 的情况下进行构建:
\n\ndiff -ru cctools-829/libstuff/lto.c cctools-829.modified/libstuff/lto.c\n--- cctools-829/libstuff/lto.c 1969-12-31 16:00:00.000000000 -0800\n+++ cctools-829.modified/libstuff/lto.c 2013-04-05 03:18:34.000000000 -0700\n@@ -376,4 +376,18 @@\n lto_dispose(mod);\n }\n\n+#else \n+\n+#include <stdlib.h>\n+\n+__private_extern__\n+int\n+is_llvm_bitcode(\n+ struct ofile *ofile,\n+ char *addr,\n+ size_t size)\n+{\n+ return 0;\n+}\n+\n #endif /* LTO_SUPPORT */\ndiff -ru cctools-829/misc/libtool.c cctools-829.modified/misc/libtool.c\n--- cctools-829/misc/libtool.c 1969-12-31 16:00:00.000000000 -0800\n+++ cctools-829.modified/misc/libtool.c 2013-04-05 03:20:21.000000000 -0700\n@@ -1369,8 +1369,11 @@\n }\n }\n }\n- else if(ofiles[i].arch_type == OFILE_Mach_O ||\n- ofiles[i].arch_type == OFILE_LLVM_BITCODE){\n+ else if(ofiles[i].arch_type == OFILE_Mach_O\n+#ifdef LTO_SUPPORT\n+ || ofiles[i].arch_type == OFILE_LLVM_BITCODE\n+#endif\n+ ){\n if(cmd_flags.ranlib == TRUE){\n error("for architecture: %s file: %s is not an "\n "archive (no processing done on this file)",\nRun Code Online (Sandbox Code Playgroud)\n\n然后构建:
\n\nmake install USE_DEPENDENCY_FILE=NO BUILD_DYLIBS=NO LTO= TRIE= DSTROOT=../build-cctools\nRun Code Online (Sandbox Code Playgroud)\n\n并手动安装必要的部件:
\n\ncd ..\ncp build-cctools/usr/bin/as i386-apple-gcc/bin/i386-apple-darwin-as\ncp build-cctools/usr/bin/as i386-apple-gcc/i386-apple-darwin/bin/as\ncp build-cctools/usr/bin/lipo i386-apple-gcc/bin/i386-apple-darwin-lipo\ncp build-cctools/usr/bin/lipo i386-apple-gcc/i386-apple-darwin/bin/lipo\ncp build-cctools/usr/bin/strip i386-apple-gcc/bin/i386-apple-darwin-strip\ncp build-cctools/usr/bin/strip i386-apple-gcc/i386-apple-darwin/bin/strip\nRun Code Online (Sandbox Code Playgroud)\n\n如果我们再次尝试构建 GCC,我们会发现构建过程会受到干扰,因为我们新构建的汇编程序无法针对 x86_64。由于这似乎与您想要实现的目标无关,您可以将参数添加--disable-multilib到 GCC 的配置脚本中以防止它尝试这样做。
此时我在尝试构建 libgcc 时遇到了错误。这些错误是因为找不到合适的链接器,所以我想现在您也需要下载并构建 ld64。
\n\n就在此时,我已经没有时间再继续下去了。祝你好运!
\n| 归档时间: |
|
| 查看次数: |
7904 次 |
| 最近记录: |