Makefile将目录中的所有cpp文件编译成单独的可执行文件 [英] Makefile that compiles all cpp files in a directory into separate executable

查看:683
本文介绍了Makefile将目录中的所有cpp文件编译成单独的可执行文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在在学习C ++。我想要一个makefile,它将编译当前目录中的所有cpp文件以分离可执行文件。 例如:

I am now studying C++. I want a makefile which will compile all of the cpp files in the current directory to separate executables. For example:

在目录中有3个c ++文件,例如 examp1.cpp, examp2.cpp examp3.cpp 。我想要一个makefile,它将编译并链接它们,并给予 examp1.exe,examp2.exe examp3.exe

In a directory there are 3 c++ files, such as examp1.cpp, examp2.cpp and examp3.cpp. I want a makefile which will compile and link them and give examp1.exe, examp2.exe and examp3.exe

我创建了一个bash脚本来编译所有的和创建exes但我认为;这不是这样做的确切方法。

I have created a bash script to compile all of them and create exes but I think; that's not the exact way to do this.

我有一个.c的Makefile,但这似乎在这里不工作。它只创建对象文件,而不是实际链接它。它如下:

I have a a Makefile for ".c", but that does not seem to work here. It is only creating object files and not actually linking it. It is as follows:

SRCS=$(wildcard *.c)
OBJS=(SRCS:.c=.o)
all: $(OBJS)

上述代码编译所有新的修改.c文件到当前目录中具有相同名称的.o文件。

The above code compiles all the new and modified ".c" files to ".o" files with same name in the current directory.

我用来创建可执行文件的bash脚本如下: p>

The bash script I am using to create executables is as follows:

for i in ./*.cpp
do
   g++ -Wno-deprecated $i -o `basename $i .cpp`".exe"
done

这意味着我想要的。

推荐答案

我使用一个简单的make all一个最小的Makefile做你想要的:

A minimal Makefile that does what you want would be:

#Tell make to make one .out file for each .cpp file found in the current directory
all: $(patsubst %.cpp, %.out, $(wildcard *.cpp))

#Rule how to create arbitary .out files. 
#First state what is needed for them e.g. additional headers, .cpp files in an include folder...
#Then the command to create the .out file, probably you want to add further options to the g++ call.
%.out: %.cpp Makefile
    g++ $< -o $@ -std=c++0x

您必须用编译器替换g ++你使用并可能调整一些平台特定的设置,但Makefile本身应该工作。

You'll have to replace g++ by the compiler you're using and possibly adjust some platform specific setting, but the Makefile itself should work.

这篇关于Makefile将目录中的所有cpp文件编译成单独的可执行文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆