编译 C++与 Cygwin

发布于 2024-08-12 00:46:24 字数 143 浏览 2 评论 0原文

如何在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

赴月观长安 2024-08-19 00:46:24

您需要使用如下命令:

g++ -o prog prog.cpp

这是一种简单的形式,可以将单文件 C++ 项目转换为可执行文件。如果您有多个 C++ 文件,您可以这样做:

g++ -o prog prog.cpp part2.cpp part3.cpp

但最终,为了方便起见,您将需要引入 makefile,这样您只需编译已更改的部分。然后你最终会得到一个Makefile,如下所示:

prog: prog.o part2.o part3.o
    g++ -o prog prog.o part2.o part3.o

prog.o: prog.cpp
    g++ -c -o prog.o prog.cpp

part2.o: part2.cpp
    g++ -c -o part2.o part2.cpp

part3.o: part3.cpp
    g++ -c -o part3.o part3.cpp

然后然后,你将开始考虑如何编写你的makefile以使其更加灵活(例如不需要单独的每个 C++ 文件的规则),但这可以留给另一个问题。

对于每个 C++ 文件都有一个单独的项目,这是完全没有必要的。如果您将它们全部放在一个目录中,并且存在 C++ 文件到可执行文件的简单映射,则可以使用以下 makefile:

SRCS=$(wildcard *.cpp)
EXES=$(SRCS:.cpp=.exe)

all: $(EXES)

%.exe: %.cpp
    g++ -o $@ $^

然后运行 ​​make 命令,它将(智能地)创建您的所有可执行文件。 $@ 是目标,$^ 是先决条件列表。

而且,如果您有更复杂的规则,只需将它们固定在底部即可。具体规则将优先于模式规则选择:

SRCS=$(wildcard *.cpp)
EXES=$(SRCS:.cpp=.exe)

all: $(EXES)

%.exe: %.cpp
    g++ -o $@ $^

xx.exe: xx.cpp xx2.cpp xx3.cpp
    g++ -o $@ $^
    echo Made with special rule.

You need to use a command like:

g++ -o prog prog.cpp

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:

g++ -o prog prog.cpp part2.cpp part3.cpp

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:

prog: prog.o part2.o part3.o
    g++ -o prog prog.o part2.o part3.o

prog.o: prog.cpp
    g++ -c -o prog.o prog.cpp

part2.o: part2.cpp
    g++ -c -o part2.o part2.cpp

part3.o: part3.cpp
    g++ -c -o part3.o part3.cpp

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:

SRCS=$(wildcard *.cpp)
EXES=$(SRCS:.cpp=.exe)

all: $(EXES)

%.exe: %.cpp
    g++ -o $@ $^

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:

SRCS=$(wildcard *.cpp)
EXES=$(SRCS:.cpp=.exe)

all: $(EXES)

%.exe: %.cpp
    g++ -o $@ $^

xx.exe: xx.cpp xx2.cpp xx3.cpp
    g++ -o $@ $^
    echo Made with special rule.
温柔少女心 2024-08-19 00:46:24

你将需要 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.

胡渣熟男 2024-08-19 00:46:24

如果您想使用 cygwin,您应该使用正常的 gcc 语法,

g++ -o foobar foobar.cpp

但这在 Visual C++ 中并不能很好地发挥作用。如果您更喜欢使用 GCC 而不是 Visual C++ 编译器,我建议您查看 Eclipse CDT

if you want to use cygwin you should use the normal gcc syntax

g++ -o foobar foobar.cpp

but 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.

厌倦 2024-08-19 00:46:24

我如何编译 cpp 程序:

g++ -Wall Test.cpp -o Test
-Wall enables warning and error messages to be shown 
-o Test creates an Test.exe after compilation

如果您想单独编译文件:

g++ -Wall -c File1.cpp
g++ -Wall -c File2.cpp

现在使用组合的目标文件创建一个可执行文件,如下所示:

g++ -Wall File1.o File2.o  -o File.exe

这样您就可以编译头文件,并且可以包含在应用程序中。

What I do to compile a cpp program:

g++ -Wall Test.cpp -o Test
-Wall enables warning and error messages to be shown 
-o Test creates an Test.exe after compilation

If you want to compile files separately:

g++ -Wall -c File1.cpp
g++ -Wall -c File2.cpp

Now create an executable with the combined object files as:

g++ -Wall File1.o File2.o  -o File.exe

This way you can compile your header files and you can include in your application programs.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文