c++ - 为什么这个 Makefile 不能正确编译?

标签 c++ c++11 compiler-construction

我有以下 Makefile:

CC=g++
CFLAGS=-c -pedantic -std=c++11 -lglfw -lGL -lGLU -lX11 -lXxf86vm -lXrandr -lpthread -lXi

all: Set GLDrawableGraph Edge Graph Play main

Set: Set.cpp Set.h
        $(CC) $(CFLAGS) -o Set Set.cpp

GLDrawableGraph:  GLDrawableGraph.cpp GLDrawableGraph.h Edge.h Graph.h 
         $(CC) $(CFLAGS) -o GLDrawableGraph GLDrawableGraph.cpp

Edge:  Edge.cpp Edge.h
         $(CC) $(CFLAGS) -o Edge Edge.cpp

Graph:  Graph.cpp GLDrawableGraph.h Edge.h Graph.h Play.h
         $(CC) $(CFLAGS) -o Graph Graph.cpp

Play:  Play.cpp GLDrawableGraph.h Edge.h Graph.h  Play.h
         $(CC) $(CFLAGS) -o Play Play.cpp

main:  main.cpp GLDrawableGraph.o Edge.o Graph.o Play.o GLDrawableGraph.h Edge.h Graph.h Play.h Set.o Set.h
         $(CC) $(CFLAGS) -o main main.cpp GLDrawableGraph.cpp Edge.cpp Graph.cpp Play.cpp Set.cpp

我得到以下输出:

GLDrawableGraph.cpp:95:10: error: ‘p’ does not name a type
     auto p=nodes.find(nodenumber);
          ^
GLDrawableGraph.cpp:96:8: error: ‘p’ was not declared in this scope
     if(p==nodes.end()) throw std::string("Tried to color nonexistant vertex");
        ^
GLDrawableGraph.cpp:97:5: error: ‘p’ was not declared in this scope
     p->second.color=col;
     ^
GLDrawableGraph.cpp: In member function ‘void DrawableGraph::DrawGraph(GLFWwindow*) const’:
GLDrawableGraph.cpp:140:15: error: ISO C++ forbids declaration of ‘e’ with no type [-fpermissive]
     for(auto &e : edges) {
               ^
GLDrawableGraph.cpp:140:19: error: range-based ‘for’ loops are not allowed in C++98 mode
     for(auto &e : edges) {
                   ^
GLDrawableGraph.cpp:141:18: error: request for member ‘first’ in ‘e’, which is of non-class type ‘int’
         int i1=e.first.first;
                  ^
GLDrawableGraph.cpp:142:18: error: request for member ‘first’ in ‘e’, which is of non-class type ‘int’
         int i2=e.first.second;
                  ^
GLDrawableGraph.cpp:151:28: error: request for member ‘second’ in ‘e’, which is of non-class type ‘int’
         RGB rgb=RGBColor(e.second.color);
                            ^
GLDrawableGraph.cpp:158:15: error: ISO C++ forbids declaration of ‘p’ with no type [-fpermissive]
     for(auto &p : nodes) {
               ^
GLDrawableGraph.cpp:158:19: error: range-based ‘for’ loops are not allowed in C++98 mode
     for(auto &p : nodes) {
                   ^
GLDrawableGraph.cpp:159:26: error: request for member ‘second’ in ‘p’, which is of non-class type ‘int’
         float xx=float(p.second.x-R.l)/(R.r-R.l)*1.8-0.9;
                          ^
GLDrawableGraph.cpp:160:26: error: request for member ‘second’ in ‘p’, which is of non-class type ‘int’
         float yy=float(p.second.y-R.b)/(R.t-R.b)*1.8-0.9;

我认为这些错误来自于 C++11 与常规 C++ 的差异,但我已经给出了 -std=c++11 选项,但它仍然不起作用。

最佳答案

您的 makefile 依赖项不正确。发生的事情是它使用内置规则编译您的项目,因此您的标志不适用并且编译器不识别 C++11 auto 关键字。尝试使用 make -r 进行编译以禁用内置规则,看看会发生什么。

你可能想要这样的东西:

CXX := g++
CPPLAGS := -Wall -Wextra
CXXFLAGS := -std=c++11
LDLIBS := -lglfw -lGL -lGLU -lX11 -lXxf86vm -lXrandr -lpthread -lXi

all : main

main : main.o GLDrawableGraph.o Edge.o Graph.o Play.o Set.o 
    ${CXX} -o $@ $^ ${LDLIBS}

%.o : %.cpp # also auto-generates dependencies
    ${CXX} -c -o $@ ${CPPLAGS} ${CXXFLAGS} -MD -MP -MF ${@:.o=.d} $<

-include $(wildcard *.d) # include auto-generated dependencies

关于c++ - 为什么这个 Makefile 不能正确编译?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22333993/

相关文章:

c++ - 通过未覆盖的基类从派生类调用函数

C++11代替 `GetExitCodeThread`监控线程状态?

c - GBA C 编程编译器 gcc

c++ - 工具提示中的 Qt WIdget

c++ - 如何映射 Linux 系统线程 id 和 std::thread::id?

scala - 理解函数字面量类型

algorithm - 是否有满足这些要求的垃圾回收算法?

c++ - 使用 openGL (c++) 将椭圆分解为三角形

C++ 11:引用无效?

c++ - 有没有办法强制实例只在堆栈上?