使用Cygwin编译C ++ [英] Compile C++ with 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.
推荐答案
您需要使用以下命令:
g++ -o prog prog.cpp
这是一个简单的形式,将一个单文件C ++项目变成可执行文件。如果你有多个C ++文件,你可以这样做:
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
但是最终,为了方便起见,我们需要引入makefile,这样你只需要编译改变。然后你最终会得到一个 Makefile
like:
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
然后,您将开始计算如何编写makefiles以使其更加灵活(例如,不需要为每个C ++文件单独创建规则),但可以留下另一个问题。
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.
关于对每个C ++文件有一个单独的项目,这是没有必要的。如果你把它们都放在一个目录下,并且有一个简单的C ++文件映射到可执行文件,你可以使用下面的makefile:
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 $@ $^
make
命令,它将(智能地)创建所有可执行文件。 $ @
是目标, $ ^
是先决条件列表。
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.
这篇关于使用Cygwin编译C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!