使用没有foo =的makefile参数

时间:2016-10-27 02:05:43

标签: c++ makefile

我有一个用于编译单个文件的makefile。当我需要传递参数时,我使用target = targetFile。

脚本接受参数,查找与参数具有相同值的文件(在同一目录中)并编译它。

我用它来编译来自uhunt和uva的问题,这些问题使用单个c ++文件。所以我没有'需要多个makefile用于多个源文件。多个源文件的单个makefile是我制作makefile的原因。

这是我到目前为止的代码

words_list

我用来编译的命令是, OBJS = $(target).o CC = g++ CFLAGS = -Wall -g -std=c++11 INCLUDE = -I./$(target) #default command to run all : Main-$(target) clean run #compile and build Main-$(target) : $(OBJS) $(CC) $(CFLAGS) $^ -o $@ %.o : %.cpp $(CC) -c $(CFLAGS) $< #remove object and any other garbage files. clean: rm -rf -d $(target).o *~ *% *# .#* #remove the compiled file clean-all: $(clean) rm Main-$(target) #run the compiled file run: ./Main-$(target)

另外,我没有包含文件扩展名,我的所有源文件扩展名都是cpp

我最终想要的是: make target=sourceFile

请注意,使用命令clean和clean-all,我使用

make sourceFile

make target=sourceFile clean

我更喜欢我可以使用:

make target=sourceFile clean-all

make sourceFile clean

2 个答案:

答案 0 :(得分:0)

您可以使用包含传递给make的所有目标的公共Makefile变量MAKECMDGOALS

请尝试此变体

CC = g++
CFLAGS = -Wall -g
MAKECMDGOALS := $(filter-out clean, $(MAKECMDGOALS))

.PHONY: $(MAKECMDGOALS)
$(MAKECMDGOALS):
        $(CC) $(CFLAGS) $@.c -o Main-$@

clean:
        rm -f *.o

这里的行

$(MAKECMDGOALS):
        $(CC) $(CFLAGS) $@.c -o Main-$@

将为MAKECMDGOALS中的每个单词生成单独的构建目标。

注意,我们需要这个Makefile知道&#39; clean&#39;是删除内容的目标,但不是尝试构建Main-clean。这就是我们使用过滤功能从clean删除MAKECMDGOALS的原因。

因此,如果我们运行make a b clean,构建系统将自动生成构建Main-aMain-b的目标,然后使用已编写的clean目标

答案 1 :(得分:0)

免责声明 - 这是Make的非标准使用,因此会打开各种角落案例,所以我不推荐它。这更适合调用make的shell脚本。话虽如此......这是一个有趣的问题。

你不能make xxx clean,而不是试图建立xxx(除非你使用递归制作做一些非常讨厌的cludge,但我不会去那里)。您可以执行类似make clean-xxx的操作,如下所示:

%:Main-%

Main-%:%.cpp
    $(CC) $(CFLAGS) $< -o Main-$@

clean-%:
    rm Main-$*

请注意,%-clean的词干较短,如果make目标以%开头,则clean-优先于#include <iostream> #include <fstream> #include <string> #include <cctype> using namespace std; class Test { private: string fileIn; string fileOut; char ch; ifstream inFile; //input file stream fstream outFile; //output file stream public: Test(); //constructor void getData(); void testOpen(); void validate(); void outOpen(); void transform(); void close(); }; /************************Method Defintions*****************************/ Test::Test() { } void Test::getData() { cout << "Enter the input file name including file type and hit enter when finished." << endl << endl; getline(cin, fileIn); cout << "Enter an output file name including file type and hit enter when finished." << endl << endl; getline(cin, fileOut); } void Test::testOpen() { inFile.open(fileIn); validate(); } void Test::validate() { if (!inFile) { cout << "The file could not be opened."; exit(1234); } } void Test::outOpen() { outFile.open(fileOut, ios::out); } void Test::transform() { //set all letters to lower case inFile.get(ch); while (!inFile.eof()) { outFile.put(tolower(ch)); inFile.get(ch); } //rewind to beginning inFile.seekg(0L, ios::beg); //make all letters after .space upper case inFile.get(ch); while (!inFile.eof()) { if (inFile, fileIn, '. ') { inFile.get(ch); outFile.put(toupper(ch)); } } } void Test::close() { inFile.close(); outFile.close(); } /**************************************Driver*********************************/ int main() { //variables Test object; //object of class Testing //get data for opening files object.getData(); //open in file and validate it opened object.testOpen(); //open output file object.outOpen(); //transform text from one file to another object.transform(); //close files object.close(); return 0; }