c语言编译及下载环境变量,windows 下使用g++ 编译器-Go语言中文社区

转自https://blog.csdn.net/xiaoliuliu2050/article/details/53420792

名词解释:GNU("Gnu's Not Unix"的缩写),一个类UNIX的操作系统,由GNU计划推动,目标在于建立一个完全相容于UNIX的自由软件环境。发展GNU系统的计划,最早由理查德·斯托曼在1983年启动,它是自由软件基金会最早致力的目标。最近一个GNU系统版本,是于2011年4月1日释出的GNU 0.401,采用GNU Hurd作为操作系统内核。其他的内核,最著名的是Linux kernel,也被应用在GNU系统中。

gcc与g++都gnu的编译器。

gcc是c语言的编译器;

g++是c++的编译器。

windows 下使用gcc 编译器:

一、环境配置

1.  下载安装MinGW

2.  设置环境变量

在path变量中,添加路径mingw的路径,如C:Program FilesMinGWbin

二、编辑源代码

Windows命令行使用GNU编译器

经过上述两步,就可以在在Windows中使用命令行窗口来模拟Linux终端了。

1.  新建 main.c 文件

通过edit main.c 命令创建并打开编辑main.c文件

133a636b29a3370d625b415d1df04095.png

2. 编辑代码

(ps:这样古朴的界面,您还记得么?)

218d8612ccf717b4e0a9e12c642f801d.png

3. 保存并退出

使用ALT键调出菜单,保存文件后退出。

b2e38301326e7b829686260f84af314d.png

三、编译过程

编译过程分为四步:预处理、编译、汇编、连接

a83f514c875e8aca8300e37258bd6aa8.png

1. 预处理

预处理主要处理源文件中的“#include”、“#define”等预处理等命令

预处理主要完成的工作有(参考:《程序员的自我修养》):

(1) 删除 #define,展开宏

(2) 处理条件编译指令。预处理程序先判断条件,再根据条件修改源代码

(3) 删除注释

(4) 添加行号,以及文件名标识。便于调试

(5) 删除“#include”,插入相应的头文件

使用gcc -E main.c -o main.i 命令,预处理后得到main.i 文件

703bd4261292a65d4a1eb3da5cd31555.png

2. 编译

生成汇编代码的过程。

使用gcc -S main.i -o main.s 命令,编译后得到main.s 文件

848c429bb51b30a7c255b60724b00dc3.png

2812624bc20f74cfb09ef71c97681934.png

3. 汇编

将汇编代码转化成 机器指令。

使用gcc -c main.c -o main.o 命令,得到目标文件main.o

18b59a6814837a8e6fbdc94eb8d9da96.png

5100e089750abca15eb4da83ce0e28b1.png

4. 连接

通过连接库文件,将目标文件转化成可执行文件。

使用命令 gcc -ld

一般情况下,我们直接使用gcc main.c -o main就可以生成可执行程序了。

当然,在Windows下使用gcc时,生成的可执行文件不再是main.out ,而是main.exe文件

Installing c++/g++ on Windows Disclaimer: This page is being maintained mainly for my students. Use these instructions at your own risk. There is no warranty in any form or shape whatsoever!. There is no guarantee that these instructions are up-to-date. With that understood you may continue with the rest of this page if you choose to accept these terms. This page was last updated on September 13, 2005, but still good as of April 30, 2009. Follow these steps to install g++ (the GNU C++ compiler) for Windows. There is no room for creativity here; you must follow the directions exactly. Pick the drive and a folder in which you want to install g++. I'll assume that it is C:, but you can choose a different one. If you choose a different drive or a different folder, you'll need to adapt the directions below accordingly. Download full.exe, an about 14 megabyte executable, to C:\full.exe by right-clicking on the link. Use Save Link As... or Save Target As... Be sure the browser saves the file as C:\full.exe. Run the downloaded executable. This will install g++ (and a lot of other things that you don't really need) on your hard drive. Go to the C: drive using Windows Explorer and double-click on full.exe. Or, open a DOS window (Start > Programs > Command Prompt), connect to the C: drive using the cd command, and type full. Locate where the bin folder was created for the g++ installation. On my Windows XP machine, it was created in the following path: C:\cygnus\cygwin-b20\H-i586-cygwin32\bin You now should add it to the PATH environment variable. You do that by following: Start -> Control Panel -> System -> Advanced -> Environment Variables At this point you can see the PATH variable either in the User Variables or in the System Variables. Add the g++ path into the PATH variable. You add it to the end of the existing value separated by a semicolon (';'). Make sure that you do not lose the original value. You are just appending more to the end separated by a semicolon. Restart your computer. A Cygnus Solutions entry will appear in your Programs menu, and an icon may appear on your desktop. Don't use them! You will use it using the g++ command on a DOS prompt as explained below. You should now be able to run g++ from a DOS (Command Prompt) window. For example, to compile a file called C:\mine\hello.cpp, connect to the C:\mine folder and enter g++ -g hello.cpp -o hello -lm You'll then be able to run the compiled program by entering hello in the DOS window. If you've installed Emacs as described here, you will also be able to run g++ from Emacs. If, when you do this, Emacs tries to compile with the command make -k, you made a mistake during the Emacs installation. If you want to learn how to run g++ on emacs, see here. If you'd like to learn more about where this free compiler came from, we downloaded it from an older site of http://sourceware.org/cygwin/. If you wish to clean up a little, you may delete the file: full.exe at this point. Your g++ compiler is installed under C:\cygnus.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值