编译 C++与 Cygwin
如何在 Cygwin 中编译我的 C++ 程序。我已经安装了gcc。我应该使用什么命令?另外,当控制台应用程序位于 .cpp 扩展名中时,如何运行它。我正在尝试使用一些小程序来学习 C++,但在 Visual C++ 中,我不想为每个小 .cpp 文件创建一个单独的项目。
How do I compile my C++ programs in Cygwin. I have gcc installed. What command should I use? Also, how do I run my console application when it is in a .cpp extension. I am trying to learn C++ with some little programs, but in Visual C++, I don't want to have to create a seperate project for each little .cpp file.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您需要使用如下命令:
这是一种简单的形式,可以将单文件 C++ 项目转换为可执行文件。如果您有多个 C++ 文件,您可以这样做:
但最终,为了方便起见,您将需要引入 makefile,这样您只需编译已更改的部分。然后你最终会得到一个
Makefile
,如下所示:然后然后,你将开始考虑如何编写你的makefile以使其更加灵活(例如不需要单独的每个 C++ 文件的规则),但这可以留给另一个问题。
对于每个 C++ 文件都有一个单独的项目,这是完全没有必要的。如果您将它们全部放在一个目录中,并且存在 C++ 文件到可执行文件的简单映射,则可以使用以下 makefile:
然后运行
make
命令,它将(智能地)创建您的所有可执行文件。$@
是目标,$^
是先决条件列表。而且,如果您有更复杂的规则,只需将它们固定在底部即可。具体规则将优先于模式规则选择:
You need to use a command like:
That's a simple form that will turn a one-file C++ project into an executable. If you have multiple C++ files, you can do:
but eventually, you'll want to introduce makefiles for convenience so that you only have to compile the bits that have changed. Then you'll end up with a
Makefile
like:And then, you'll start figuring how to write your makefiles to make them more flexible (such as not needing a separate rule for each C++ file), but that can be left for another question.
Regarding having a separate project for each C++ file, that's not necessary at all. If you've got them all in one directory and there's a simple mapping of C++ files to executable files, you can use the following makefile:
Then run the
make
command and it will (intelligently) create all your executables.$@
is the target and$^
is the list of pre-requisites.And, if you have more complicated rules, just tack them down at the bottom. Specific rules will be chosen in preference to the pattern rules:
你将需要 g++。然后尝试以
g++ file.cpp -o file.exe
作为开始。稍后您可以通过了解 Makefile 来避免大量输入。You will need g++. Then try
g++ file.cpp -o file.exe
as a start. Later you can avoid much typing by learning about Makefiles.如果您想使用 cygwin,您应该使用正常的
gcc
语法,但这在 Visual C++ 中并不能很好地发挥作用。如果您更喜欢使用 GCC 而不是 Visual C++ 编译器,我建议您查看 Eclipse CDT。
if you want to use cygwin you should use the normal
gcc
syntaxbut that doesn't really play well with Visual C++. I advise you to take a look into Eclipse CDT if you prefer using GCC over the visual C++ compiler.
我如何编译 cpp 程序:
如果您想单独编译文件:
现在使用组合的目标文件创建一个可执行文件,如下所示:
这样您就可以编译头文件,并且可以包含在应用程序中。
What I do to compile a cpp program:
If you want to compile files separately:
Now create an executable with the combined object files as:
This way you can compile your header files and you can include in your application programs.