简体   繁体   English

如何在x86上为Sun4v交叉编译GCC?

[英]How to cross compile GCC on x86 for Sun4v?

I am writing a programming assignment using C++. 我正在使用C ++编写一个编程作业。 The instructor of this course requires all code to be compiled and run on the UNIX server. 本课程的讲师要求在UNIX服务器上编译和运行所有代码。 The server is a SunOS machine. 该服务器是SunOS机器。 I wrote all my code on my personal laptop with GCC 5.2, which support most C++11 features. 我用GCC 5.2在我的个人笔记本电脑上编写了所有代码,它支持大多数C ++ 11功能。 However, when I upload my code to the server and tried to compile it, I surprisingly found that the g++ version on the server is 4.2.1, which was released in mid-2007. 但是,当我将代码上传到服务器并尝试编译时,我惊奇地发现服务器上的g ++版本是4.2.1,它是在2007年中期发布的。 Many of the C++11 features are not supported. 许多C ++ 11功能都不受支持。 Even the -std argument is not accepted. 即使是-std参数也不被接受。

I tried to download the source code of the latest GCC and compile it on the server. 我试着下载最新GCC的源代码并在服务器上编译它。 Unfortunately there is a disk quota limiting to 500M per account. 不幸的是,每个帐户的磁盘配额限制为500M。 I am just wondering if it is possible to cross compile GCC on my x86 machine and upload the binary on to the server so that I can compile my C++ code. 我只是想知道是否可以在我的x86机器上交叉编译GCC并将二进制文件上传到服务器,以便我可以编译我的C ++代码。

By the way, I have contacted the IT department about updating the software but they responded that they do not have such plans in the near future. 顺便说一句,我已经联系了IT部门关于更新软件,但他们回答说他们在不久的将来没有这样的计划。

I did do research on the Internet about cross compilation and found a couple tutorials. 我确实在互联网上研究过交叉编译,并找到了几个教程。 But they are not easy to follow. 但他们并不容易理解。 In addition to binaries, there are also a lot dependencies like headers and libraries. 除了二进制文件之外,还有很多依赖项,如头文件和库。 So before I give up and modify my code to fit the old compiler, can anyone give me some suggestions? 所以在我放弃并修改我的代码以适应旧编译器之前,有人能给我一些建议吗?

Thank you. 谢谢。

uname -a returns the following result uname -a返回以下结果

SunOS 5.10 Generic_147147-26 sun4v sparc SUNW,T5240

Of course it's possible, and it's the way you usually do the things when writing operating systems . 当然这是可能的,这是你在编写操作系统时通常做的事情。

First of all, you need to take binutils in the toolbox , too. 首先, 您还需要在工具箱中使用binutils Once you have all the Holy Sources, let's prepare! 一旦你拥有了所有神圣的来源,让我们做好准备吧!

export PREFIX="$HOME/opt" # All the stuff will get installed here!
export TARGET=sparc-sun-solaris  # I'm not *100%* sure if this is correct, check it yourself
export PATH="$PREFIX/bin:$PATH"  # If you forget this/close the terminal, you're doomed!

Now, let's get with the little monster... Shall binutils be built! 现在,让我们来看看这个小怪物......应该建造binutils!

cd $HOME/src # Or where you have the sources
mkdir binutils-build
cd binutils-build
../binutils-src/configure --target=$TARGET --prefix="$PREFIX" --disable-nls
make
make install

--disable-nls disables the support for native natural languages (aka: the compiler prints errors in your own language!), and just uses English for messages. --disable-nls禁用对本机自然语言的支持(也就是说:编译器以您自己的语言打印错误!),并且只使用英语作为消息。 That's not a must, but it certainly speeds up the process of building binutils. 这不是必须的,但它肯定会加快构建binutils的过程。

Now, compiling GCC itself is a very fragile process, and it can fail anywhere, anyhow, so be prepared! 现在,编译GCC本身是一个非常脆弱的过程,它无论如何都会失败,所以要做好准备! This process is long (it can take up to an hour on some machines), but trust me, LLVM+Clang is worse ;). 这个过程很 (在某些机器上可能需要长达一个小时),但请相信我,LLVM + Clang更糟糕;)。

cd $HOME/src
cd gcc-src
./contrib/download_prerequisites # Get GMP, MPFR, and MPC
cd ..
mkdir gcc-build
cd gcc-build
../gcc-src/configure --target=$TARGET --prefix="$PREFIX" --disable-nls --enable-languages=c,c++
make all-gcc
make all-target-libgcc
make install-gcc
make install-target-libgcc

If you don't get into issues while compiling (trust me, you will unless you're too lucky for this world), you'll have a toolchain that runs on your machine, but compiles for SunOS/SPARC! 如果你不进入的问题,同时编制(相信我,你除非你对这个世界太幸运了),你就会有你的机器上运行的工具链,但编译用于Sun / SPARC! BTW, --enable-languages=c,c++ means that GCC will have support for compiling C and C++ code. BTW, - --enable-languages=c,c++意味着GCC将支持编译C和C ++代码。 Nothing less, nothing more. 没有更少,仅此而已。 Try it out with... 尝试用...

sparc-sun-solaris-g++ --version

Now, if you want to get a compiler for the server, that runs on the server, you will have to some mess with a double canadian cross . 现在,如果你想获得服务器上运行的服务器的编译器,你将不得不加倍于加拿大的双十字架 Basically, what you have to do is... 基本上,你要做的是......

export PREFIX="$HOME/some-holy-directory" # This path *must* be the same for both your machine and the target server!
export HOST=$TARGET

And then repeat the compilation process again, remembering to adding the option --host=$HOST to both configure scripts! 然后再次重复编译过程,记住在两个configure脚本中添加选项--host=$HOST Once done, you must move that some-holy-directory at exactly the same location into the server. 完成后,您必须将位于完全相同位置的some-holy-directory移动到服务器中。 If it didn't fit into the 500MB, well, ask your teacher if you can at least compile assignments in your own machine, then upload them to the server. 如果它不适合500MB,那么,问问你的老师你是否至少可以在你自己的机器上编译作业,然后将它们上传到服务器。 Otherwise, you're left out with C++98. 否则,您将被排除在C ++ 98之外。

BTW : Please note that cross-compiling GCC itself is an even more fragile process. 顺便说一句 :请注意,交叉编译GCC本身是一个更加脆弱的过程。 All this post is just theoretical, because I won't do all this steps just for the sake of doing it. 所有这些帖子都只是理论上的,因为我不会仅仅为了这个目的而做所有这些步骤。 Please comment if you have any major issues, or if someone spots an error in the steps ;). 如果您有任何重大问题,或者有人在步骤中发现错误,请发表评论;)。

Edit : Apparently, you'll have to build Glibc and all that funky stuff too... 编辑 :显然,你必须建立Glibc和所有那些时髦的东西......

I hope this has led some light on you! 我希望这对你有所启发!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM