简体   繁体   English

项目的Makefile

[英]Makefile for a project

I have a Makefile for a project using regex. 我有一个使用正则表达式的项目的Makefile。 Thus I need to use g++-4.9 and c++11. 因此,我需要使用g ++-4.9和c ++ 11。

BIN   = bin
OBJ   = src/main.o src/time2.o  src/except.o src/except2.o src/struct.o src/index.o
FLAGS = -lncurses
CC    = g++-4.9  -std=c++11 -Wall -pedantic -Wno-long-long -O0 -ggdb

all: compile

run: compile
    ./$(BIN)

clean:
    rm -f $(BIN) $(OBJ)

compile: $(OBJ)
    $(CC) -o $(BIN) $(OBJ) $(FLAGS)

but when I try to make compile: 但是当我尝试进行编译时:

 g++ -c -o src/main.o src/main.cpp In file included from /usr/include/c++/4.8/regex:35:0, from src/time2.h:16, from src/main.cpp:3: /usr/include/c++/4.8/bits/c++0x_warning.h:32:2: error: #error This file requires compiler and library support for the ISO C++ 2011 standard. This support is currently experimental, and must be enabled with the -std=c++11 or -std=gnu++11 compiler options. #error This file requires compiler and library support for the \\ ^ In file included from src/main.cpp:5:0: src/struct.h:99:18: error: 'regex' has not been declared bool isnamegood(regex x,const string& name); ^ 

so I don't understand what is wrong, may you help me please? 所以我不明白怎么了,请您能帮我吗?

The problem is that your compile target already depends on the object files! 问题是您的compile目标已经取决于目标文件! But they aren't build yet, so make tries to find a rule to build them. 但是它们尚未构建,因此请尝试找到构建它们的规则。 Because you have not defined a custom rule, it uses the implicit rule : 由于尚未定义自定义规则,因此它使用隐式规则

Compiling C++ programs no is made automatically from n.cc, n.cpp, or nC with a recipe of the form '$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c'. 从n.cc,n.cpp或nC会自动以配方形式'$(CXX)$(CPPFLAGS)$(CXXFLAGS)-c'编译C ++程序。 We encourage you to use the suffix '.cc' for C++ source files instead of '.C'. 我们建议您对C ++源文件使用后缀“ .cc”而不是“ .C”。

This rule will of course not use the flags you set in CC . 该规则当然不会使用您在CC设置的标志。 This explains why the gcc command line printed by make does not match your CC . 这解释了为什么make打印的gcc命令行与您的CC不匹配。 You can test this by running make with --no-builtin-rules , as this should disable all the implicit rules. 您可以通过使用--no-builtin-rules运行make来进行测试,因为这会禁用所有隐式规则。

A good idea in such cases is to run make with -d or --debug , I think this should display rule evaluations. 在这种情况下,一个好主意是使用-d--debug运行make,我认为这应该显示规则评估。

Instead of using the c macros, use the c++ macros 而不是使用c宏,而是使用c++

CXX= g++-4.9
CXXFLAGS=  -std=c++11 -Wall -pedantic -Wno-long-long -O0 -ggdb

Then the default rules should pick them up and you need to type less in your makefile. 然后,默认规则应将其选中,并且您需要在makefile中键入更少的内容。

Also see the complete list of variables (macros) used by Make 另请参阅Make使用的变量 (宏)的完整列表

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM