C++ 使用gcc进行c ++命令行编译

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15981465/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 19:57:24  来源:igfitidea点击:

c++ command line compilation with gcc

c++gccwindows-7command-linemingw

提问by Terryl

I am trying to use a text editor instead of code::blocks to write some c++ code. Just wrote a "hello world" program.

我正在尝试使用文本编辑器而不是 code::blocks 来编写一些 C++ 代码。刚刚写了一个“hello world”程序。

My code::blocks ide uses the gcc compiler that I installed, but I want to learn how to do it at a bit of a lower level. I read a few tutorials that said all I have to do is open a command prompt and type:

我的 code::blocks ide 使用了我安装的 gcc 编译器,但我想学习如何在较低级别进行操作。我读了一些教程,上面说我所要做的就是打开命令提示符并键入:

gcc helloWorld.cpp -o helloWOrld

but i get an error saying "gcc" is not a recognized anything.

但我收到一条错误消息,说“gcc”不是公认的任何东西。

What do i do to make it work?

我该怎么做才能让它发挥作用?

采纳答案by podshumok

If you can compile with code:blocks, than probably it ships out with compiler.

如果您可以使用 code:blocks 进行编译,那么它可能会随编译器一起发布。

You need to find a path to the compiler (probably somewhere in C:\Program Files\CodeBlocks...) The file name is something like mingw-gcc.exe or mingw-g++.exe I believe also, that you can find this path in IDE settings.

您需要找到编译器的路径(可能在 C:\Program Files\CodeBlocks ... 中的某处)文件名类似于 mingw-gcc.exe 或 mingw-g++.exe 我相信,您可以找到这个IDE 设置中的路径。

When you know the path and filename, just add the path to the system PATH variable and call gccfilename.exe

当您知道路径和文件名时,只需将路径添加到系统 PATH 变量并调用 gccfilename.exe

to compile c++ programms run g++filename.exe

编译 C++ 程序运行 g++filename.exe

Also you can run simple compilation without modifying PATH: just run "c:\Full path to compiler\compiler.exe"

您也可以在不修改 PATH 的情况下运行简单编译:只需运行“c:\Full path to compiler\compiler.exe”

回答by Arunava Nag

Do g++ -Wall helloWorld.cpp -o helloWOrld... for your example

g++ -Wall helloWorld.cpp -o helloWOrld......以你的为例

回答by mday299

FWIW, gcc is NOT included in Windows by default. You have to install it via MinGW, Cygwin, or some other means.

FWIW,默认情况下 Windows 中不包含 gcc。您必须通过 MinGW、Cygwin 或其他方式安装它。

You can play around with MinGW if you like, but I prefer the gcc compiler provided with Cygwin... it's easier for my simple mind to install. Just install Cygwin and be sure to install gcc as well (it's in the Devel section).

如果你愿意,你可以使用 MinGW,但我更喜欢 Cygwin 提供的 gcc 编译器......我的头脑更容易安装。只需安装 Cygwin 并确保也安装 gcc(它在 Devel 部分)。

Good luck.

祝你好运。

回答by Ed Heal

  1. Try installing both the C and C++ compilers - See http://www.gnu.org/software/commoncpp/etc
  2. .cppis C++ so the command line will start g++ not gcc
  1. 尝试安装 C 和 C++ 编译器 - 请参阅http://www.gnu.org/software/commoncpp/
  2. .cpp是 C++ 所以命令行将启动 g++ 而不是 gcc