GUN compiler collection源代码编译过程

2024-06-03 23:04

本文主要是介绍GUN compiler collection源代码编译过程,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

第一部分:

学习kernel需要了解编译的一些过程,为了详细理解GCC编译过程的原理,动手做了个sy,记录如下,有需要的童鞋可以参考。

1.环境:(均可,二次环境并非是WSL版本)

root@LAPTOP-X280:~# uname -r

5.15.146.1-microsoft-standard-WSL2

root@LAPTOP-X280:~# lsb_release -a

No LSB modules are available.

Distributor ID: Ubuntu

Description:    Ubuntu 22.04.3 LTS

Release:        22.04

Codename:       jammy

root@LAPTOP-X280:~# gcc -v

Command 'gcc' not found, but can be installed with:

apt install gcc

root@LAPTOP-X280:~#

2.Downloading the source 

从亚洲镜像站点下载:https://www.gnu.org/prep/ftp.html#asia

下载最新的版本:

NJU Mirror

The installation procedure itself is broken into five steps.

  1. Prerequisites 
  2. Downloading the source 
  3. Configuration 
  4. Building 
  5. Testing (optional)
  6. Final install 

来自 <Installing GCC- GNU Project>

0.解压缩文件

xz -cd gcc.13.0.tar.xz | tar xvf -

 mkdir objdir

1.Prerequisites  

apt install build-essential   //必须安装的基础编译,不如C和C++

//解决configure: error: no acceptable C compiler found in $PATH等问题

//实际上这个是安装了gcc,g++,make等工具的编译器了。

apt install libgmp-dev libmpfr-dev libmpc-dev libisl-dev //必须安装,按这里的先后顺序

3.Configuration 

root@LAPTOP-X280:/usr/gcc-13.3.0/objdir# ../configure

root@LAPTOP-X280:/usr/gcc-13.3.0/objdir# ../configure --disable-multilib

4.Building  

Building a native compiler

nproc

make -j$(nproc)

这个过程需要时间,而且top查看,开销很大,CPU基本都是耗尽。

若出现cpu等fetal error,可能就不需要使用-j的选项了,默认使用make即可

以下是WSL的资源使用:

Win11 的资源使用:

卡住在:…/gcc /explow.cc

后面出现fatal error

Ctrl+c终止进程,改为make -j6  记得6<nproc(8),资源也是很吃紧如下图:

还是卡住了:

/bin/bash ../../gcc/../move-if-change tmp-fixinc_list fixinc_list

echo timestamp > s-fixinc_list

….

最后尝试make把

先make distclean,

do ‘make distclean’ to delete all files that might be invalid.

再../configure --disable-multilib

Make  //19:10开始执行命令,完成时间:20:03终止

这时候的资源:

执行make -j8 开始执行时间:20:04,完成时间:21:10,出现fatal error

这时候的资源情况瞬间上升:

第二次改成make -j6,时间从21:21-21:46

这次OK了。

ps:(这里是第二次编译的,遇到磁盘空间不够的情况)

 磁盘空间不够的化使用命令:make -j4 bootstrap-lean

5.Testing (optional)   

The latest development sources are available via anonymous Git. Use the following command to clone a copy of the source tree:

     $ git clone git://git.sv.gnu.org/dejagnu.git

 cd dejagnu

./configure

 make

…出错解决:

apt install texinfo

make

 make install

cd objdir;

root@LAPTOP-X280:/usr/gcc-13.3.0/objdir# make -k check

6.Final install 

 make install

root@LAPTOP-X280:/usr/gcc-13.3.0/objdir# whereis gcc

gcc: /usr/lib/gcc /usr/local/bin/gcc /usr/local/lib/gcc /usr/share/gcc

root@LAPTOP-X280:/usr/gcc-13.3.0/objdir# /usr/local/bin/g

cc -v

Using built-in specs.

COLLECT_GCC=/usr/local/bin/gcc

COLLECT_LTO_WRAPPER=/usr/local/libexec/gcc/x86_64-pc-linux-gnu/13.3.0/lto-wrapper

Target: x86_64-pc-linux-gnu

Configured with: ../configure --disable-multilib

Thread model: posix

Supported LTO compression algorithms: zlib

gcc version 13.3.0 (GCC)

root@LAPTOP-X280:/usr/gcc-13.3.0/objdir#

第二部分:

使用gcc编译c程序

1.先设置环境变量

查看:

因为在root环境下编译的源代码,所以切换root下查看:

这里就可以直接使用gcc -v了,所以不需要设置环境变量。

关于环境变量的设置,查看/etc/profile或需要用到:

export HOME等命令, 如export PATH= 'echo $PATH':/root/linux

2.编译单个文件

ps,了解以下:

但是并不是说gcc编译c语言,g++编译c++。而是:gcc调用了C compiler,而g++调用了C++ compiler。主要区别在于:

  •     对于 .c和.cpp文件,gcc分别当做c和cpp文件编译(c和cpp的语法强度是不一样的)
  •     对于 .c和.cpp文件,g++则统一当做cpp文件编译
  •     使用g++编译文件时,g++会自动链接标准库STL,而gcc不会自动链接STL
  •     gcc在编译C文件时,可使用的预定义宏是比较少的
  •     gcc在编译cpp文件时/g++在编译c文件和cpp文件时(这时候gcc和g++调用的都是cpp文件的编译器),会加入一些额外的宏。
  •     在用gcc编译c++文件时,为了能够使用STL,需要加参数 –lstdc++ ,但这并不代表 gcc –lstdc++ 和 g++等价,它们的区别不仅仅是这个。
  • gcc是GCC中的GUNC Compiler(C 编译器)
  • g++是GCC中的GUN C++ Compiler(C++编译器)

linux@LAPTOP-X280:~/cfile$ cat hello.c

#include <stdio.h>

int main(void)

{

        printf("hello,\nworld!\n");

        return 0;

}

缺省一步编译

linux@LAPTOP-X280:~/cfile$ gcc hello.c   //直接执行编译,会生产a.out的文件

linux@LAPTOP-X280:~/cfile$ ls

a.out  hello.c  hi.c

linux@LAPTOP-X280:~/cfile$ ./a.out    //执行a.out 二进制可执行文件

hello,

world!

linux@LAPTOP-X280:~/cfile$ file a.out

a.out: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 3.2.0, not stripped

指定一步编译:

inux@LAPTOP-X280:~/cfile$ gcc hello.c -o myhello

linux@LAPTOP-X280:~/cfile$ file myhello

myhello: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 3.2.0, not stripped

linux@LAPTOP-X280:~/cfile$ ./myhello

hello,

world!

分步编译:

预处理:gcc -E hello.c -o hello.i  //-E(大写)只进行预处理

编  译: gcc -S hello.i -o hello.s   //-S(大写)只进行预处理和编译

汇  编: gcc -c hello.s -o hello.o  //-c 只进行预处理、编译和汇编

链  接: gcc    hello.o -o file   //指定生成的输出文件名为file

linux@LAPTOP-X280:~/cfile$ gcc -E hello.c -o h.i

linux@LAPTOP-X280:~/cfile$ cat h.i |wc -l

745

linux@LAPTOP-X280:~/cfile$ cat h.i

linux@LAPTOP-X280:~/cfile$ gcc -S h.i -o h.s

linux@LAPTOP-X280:~/cfile$ cat h.o |wc -l

27

linux@LAPTOP-X280:~/cfile$ cat h.o

linux@LAPTOP-X280:~/cfile$ file h

h: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 3.2.0, not stripped

linux@LAPTOP-X280:~/cfile$ ./h

hello,

world!

使用readelf -a h.o查看.o文件

3.编译多个文件

4.自动编译文件

这篇关于GUN compiler collection源代码编译过程的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1028361

相关文章

ESP32 esp-idf esp-adf环境安装及.a库创建与编译

简介 ESP32 功能丰富的 Wi-Fi & 蓝牙 MCU, 适用于多样的物联网应用。使用freertos操作系统。 ESP-IDF 官方物联网开发框架。 ESP-ADF 官方音频开发框架。 文档参照 https://espressif-docs.readthedocs-hosted.com/projects/esp-adf/zh-cn/latest/get-started/index

C++工程编译链接错误汇总VisualStudio

目录 一些小的知识点 make工具 可以使用windows下的事件查看器崩溃的地方 dumpbin工具查看dll是32位还是64位的 _MSC_VER .cc 和.cpp 【VC++目录中的包含目录】 vs 【C/C++常规中的附加包含目录】——头文件所在目录如何怎么添加,添加了以后搜索头文件就会到这些个路径下搜索了 include<> 和 include"" WinMain 和

C/C++的编译和链接过程

目录 从源文件生成可执行文件(书中第2章) 1.Preprocessing预处理——预处理器cpp 2.Compilation编译——编译器cll ps:vs中优化选项设置 3.Assembly汇编——汇编器as ps:vs中汇编输出文件设置 4.Linking链接——链接器ld 符号 模块,库 链接过程——链接器 链接过程 1.简单链接的例子 2.链接过程 3.地址和

Windwos +vs 2022 编译openssl 1.0.2 库

一 前言 先说 结论,编译64位报错,查了一圈没找到解决方案,最后换了32位的。 使用qt访问web接口,因为是https,没有openssl库会报错 QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());if (reply){if (reply->error() == QNetworkReply::NoError

青龙面板2.9之Cdle傻妞机器人编译教程

看到有的朋友对傻妞机器人感兴趣,这里写一下傻妞机器人的编译教程。 第一步,这里以linux amd64为例,去官网下载安装go语言安装包: 第二步,输入下方指令 cd /usr/local && wget https://golang.google.cn/dl/go1.16.7.linux-amd64.tar.gz -O go1.16.7.linux-amd64.tar.gz

mysql中存储过过程和游标的联合使用

1.SQL如下: DELIMITER //DROP PROCEDURE IF EXISTS PrintAllEmployeeNames5;CREATE PROCEDURE PrintAllEmployeeNames5()BEGINDECLARE error_count INT DEFAULT 0;DECLARE num INT ;DECLARE done INT DEFAULT

iOS:编译时出现no such file or directory:xxx以及use twice...filenames are used to distinguish private dec

简    注册  登录   添加关注 作者  婉卿容若 2016.04.29 11:22 写了21870字,被16人关注,获得了14个喜欢 iOS:编译时出现"no such file or directory:xxx"以及"use twice...filenames are used to distinguish private

Java的简易编译命令

生成jar包 编译生成.class 文件 编译.class文件的命令,其中的参数是输出,原文件路径 javac -sourcepath class.class MyClass.java 如果有包名的需要创建对应包的文件夹,建文件移动对应的包名下在开始包下创建meta-inf文件夹在meta-inf文件下创建manifest.mf文件 生成可运行的jar包 解压 生成了jar包后,进

编译测试后出现“发现不明确的匹配”错误

原文链接:http://blog.163.com/zhaoyanping_1125/blog/static/201329153201204218533/ 错误提示: 【“/”应用程序中的服务器错误。  分析器错误 说明: 在分析向此请求提供服务所需资源时出错。请检查下列特定分析错误详细信息并适当地修改源文件。  分析器错误信息: 发现不明确的匹配。】   这个问题发生原因一般情况是

Java compiler level does not match the version of the installed Java project facet. map解决方法

右键项目“Properties”,在弹出的“Properties”窗口左侧,单击“Project Facets”,打开“Project Facets”页面。 在页面中的“Java”下拉列表中,选择相应版本就OK了。