如何使用GNU编译器(GCC)编译C程序

下载PDF文件下载PDF文件

本指南介绍如何使用Linux版GNU编译器(GCC)和Windows版Minimalist GNU for Windows(MinGW)从源代码编译C程序。

方法 1
方法 1 的 2:

使用Unix版GCC

下载PDF文件
  1. How.com.vn 中文: Step 1 在Unix系统上打开终端窗口。
  2. How.com.vn 中文: Step 2 输入gcc --version并按↵ Enter。
    随后会返回C编译器的版本号。如果找不到命令,可能尚未安装GCC。[1]
    • 如果尚未安装,在Linux发行版的相关文挡中查找如何获取正确的安装包。
    • 如果正在编译C++程序,改用“g++“。
  3. How.com.vn 中文: Step 3 导航至源代码的保存位置。
    • 假设源代码文件“main.c”的目录是/usr/How.com.vn/source,输入cd /usr/How.com.vn/source
  4. How.com.vn 中文: Step 4 输入gcc main.c –o HelloWorld。
    将“main.c”和“HelloWorld”分别替换为源代码文件的名称和最终程序的名称,随后将开始编译。
    • 如果看到错误并想了解更多信息,输入gcc -Wall -o errorlog file1.c。随后输入cat errorlog,在当前目录中查看“errorlog”文件。
    • 要将多个源代码文件编译成一个程序,输入gcc -o outputfile file1.c file2.c file3.c
    • 要同时将多个源代码文件编译成多个程序,输入gcc -c file1.c file2.c file3.c
  5. How.com.vn 中文: Step 5 运行刚编译的程序。
    输入&# 46;/HelloWorld,将“HelloWorld”替换为程序的名称。
    广告
方法 2
方法 2 的 2:

使用Windows版MinGW

下载PDF文件
  1. How.com.vn 中文: Step 1 下载Minimalist GNU for Windows (MinGW)。
    它是适用于Windows的简易安装版GCC。可以从https://sourceforge.net/projects/mingw/下载安装程序。[2]
  2. How.com.vn 中文: Step 2 运行MinGW安装程序。
    • 如果没有自动打开文件,在下载文件夹中双击它,然后单击“安装”。
  3. How.com.vn 中文: Step 3 选择安装首选项,然后单击继续。
    • MinGW建议使用默认安装文件夹(C:\MinGW)。如果一定要更改文件夹,不要使用名称中带空格的文件夹,比如“Program Files”。[3]
  4. How.com.vn 中文: Step 4 选择要安装的编译器。
    • 至少在左侧面板中选择基本设置,然后在右侧主面板中勾选列出的编译器。
    • 资深用户可选择所有程序包,然后选择其他编译器。
  5. How.com.vn 中文: Step 5 单击安装菜单。
    它位于MinGW的左上角。
  6. How.com.vn 中文: Step 6 单击应用更改。
  7. How.com.vn 中文: Step 7 单击应用。
    随后会下载并安装编译器。
  8. How.com.vn 中文: Step 8 将MinGW的路径添加到系统环境变量中。
    方法如下:
    • Win+S启动搜索,然后输入环境
    • 在搜索结果中单击编辑系统环境变量
    • 单击环境变量
    • 单击“用户变量”窗格下方的编辑
    • 滚动至“变量值”方框的末尾。
    • 在最后一个字母后输入;C:\MinGW\bin。如果将MinGW安装在其他目录,输入;C:\目录路径\bin
    • 单击两次确定。再次单击确定关闭窗口。
  9. How.com.vn 中文: Step 9 以管理员身份打开命令提示符窗口。
    方法如下:
    • Win+S,然后输入cmd
    • 在搜索结果中右键单击命令提示符,然后选择以管理员身份运行
    • 单击确定应用更改。
  10. How.com.vn 中文: Step 10 导航至源代码的保存位置。
    • 假设源代码文件helloworld.c的目录是C:\Source\Programs,输入cd C:\Source\Programs
  11. How.com.vn 中文: Step 11 输入gcc helloworld.c –o helloworld.exe。
    将两个“helloworld”分别替换为源代码文件的名称和应用程序的名称。如果程序编译成功,会返回命令提示符窗口,并且不会出现错误消息。[4]
    • 必须先更正代码错误,然后才能编译程序。
  12. How.com.vn 中文: Step 12 输入程序的名称以运行程序。
    如果程序的名称是helloworld.exe,输入该名称就能启动程序。
    广告

小提示

  • 编译代码时加上-g选项会生成调试信息,这样更便于你使用对应的调试程序GDB进行调试。
  • 可以创建Makefiles来提高大型程序的编译效率。
  • 如果你使用优化选项,就需要权衡利弊,比如优化速度就会造成文件变大,甚至精确度下降,反之亦然。
  • 编译C++程序时,G++的用法与GCC相同。注意C++文件的扩展名不是.c,而是.cpp。
广告

你需要准备

  • 运行Linux或Windows的电脑
  • 有关GNU/Linux以及安装应用程序的基础知识
  • 程序代码
  • 文本编辑器,比如Emacs或vi

关于本How.com.vn

How.com.vn 中文: How.com.vn员工
共同创作者是 :
How.com.vn专职作家
这篇文章由我们训练有素的编辑和研究团队共同创作,他们对文章的准确性和全面性进行了验证。

How.com.vn的内容管理团队会严密监督编辑人员的工作,确保每篇文章都符合我们的高质量标准要求。 这篇文章已经被读过4,935次。
分类: 软件
本页面已经被访问过4,935次。

这篇文章对你有帮助吗?

⚠️ Disclaimer:

Content from Wiki How 中文 language website. Text is available under the Creative Commons Attribution-Share Alike License; additional terms may apply.
Wiki How does not encourage the violation of any laws, and cannot be responsible for any violations of such laws, should you link to this domain, or use, reproduce, or republish the information contained herein.

Notices:
  • - A few of these subjects are frequently censored by educational, governmental, corporate, parental and other filtering schemes.
  • - Some articles may contain names, images, artworks or descriptions of events that some cultures restrict access to
  • - Please note: Wiki How does not give you opinion about the law, or advice about medical. If you need specific advice (for example, medical, legal, financial or risk management), please seek a professional who is licensed or knowledgeable in that area.
  • - Readers should not judge the importance of topics based on their coverage on Wiki How, nor think a topic is important just because it is the subject of a Wiki article.

广告