简体   繁体   English

使用-std = c ++ 11的Makefile

[英]Makefile with -std=c++11

I am writing a makefile for my program (that is tested and works) The problem I can tell is that some how my '-std=c++11' is not running in the function. 我正在为我的程序编写一个makefile(该文件已经过测试并且可以正常工作),我可以说的问题是我的'-std = c ++ 11'在函数中没有运行。 The code pops out a unrecognized error that I dont get when I simply run 'g++ -o exe -std=c++11 *.cpp'. 当我简单地运行'g ++ -o exe -std = c ++ 11 * .cpp'时,代码弹出一个无法识别的错误。 Here is my make file. 这是我的make文件。

HEADERS = card.h sortedLinkedList.h deck.h
OBJECTS = card.o sortedLinkedList.o deck.o main.o
exe: $(OBJECTS)
   g++ -std=c++11 $^ -o $@
%.o: %.cpp $(HEADERS)
   g++ -c -std=c++11 $< -o $@
clean:
   rm -i *.o exe

The output of my makefile is 我的makefile的输出是

me@root:~/Documents/CS216/Lab8$ make
g++    -c -o card.o card.cpp
card.cpp: In member function ‘void Card::print()’:
card.cpp:55:28: error: ‘to_string’ was not declared in this scope
       cardN=to_string(point);
                            ^
<builtin>: recipe for target 'card.o' failed
make: *** [card.o] Error 1

note: to_string() is not defined until c++11 注意:to_string()直到c ++ 11才定义

daniel@Reimann:~/Documents/CS216/Lab8$ ls 
card.cpp  deck.cpp  Lab8      lab8source.zip        SortedLinkedList.h
card.h    deck.h    lab8.cpp  makefile              SortedLinkedList.o
card.o    deck.o    lab8.o    SortedLinkedList.cpp

Output of xxd makefile xxd makefile的输出

00000000: 4845 4144 4552 5320 3d20 6361 7264 2e68  HEADERS = card.h
00000010: 2073 6f72 7465 644c 696e 6b65 644c 6973   sortedLinkedLis
00000020: 742e 6820 6465 636b 2e68 0a4f 424a 4543  t.h deck.h.OBJEC
00000030: 5453 203d 2063 6172 642e 6f20 536f 7274  TS = card.o Sort
00000040: 6564 4c69 6e6b 6564 4c69 7374 2e6f 2064  edLinkedList.o d
00000050: 6563 6b2e 6f20 6c61 6238 2e6f 0a4c 6162  eck.o lab8.o.Lab
00000060: 383a 2024 284f 424a 4543 5453 290a 0967  8: $(OBJECTS)..g
00000070: 2b2b 202d 7374 643d 632b 2b31 3120 245e  ++ -std=c++11 $^
00000080: 202d 6f20 2440 0a25 2e6f 3a20 252e 6370   -o $@.%.o: %.cp
00000090: 7020 2428 4845 4144 4552 5329 200a 0967  p $(HEADERS) ..g
000000a0: 2b2b 202d 6320 2d73 7464 3d63 2b2b 3131  ++ -c -std=c++11
000000b0: 2024 3c20 2d6f 2024 400a 636c 6561 6e3a   $< -o $@.clean:
000000c0: 0a09 726d 202d 6920 2a2e 6f20 4c61 6238  ..rm -i *.o Lab8
000000d0: 0a                            

I believe the problem is in this line: 我相信问题出在这一行:

HEADERS = card.h sortedLinkedList.h deck.h
#----------------^

You have specified a dependency on sortedLinkedList.h , but that file doesn't exist. 您已指定对sortedLinkedList.h的依赖关系,但该文件不存在。 You only have SortedLinkedList.h with a capital S . 您只有带有大写SortedLinkedList.h S SortedLinkedList.h

The rule 规则

%.o: %.cpp $(HEADERS)
    g++ -c -std=c++11 $< -o $@

is considered for making card.o but rejected because one of the prerequisites (namely sortedLinkedList.h ) doesn't exist. 被认为是制作card.o但由于不存在先决条件之一(即sortedLinkedList.h )而被拒绝。

So make falls back to its built-in rule for making .o files from .cpp files, which is $(CXX) $(CPPFLAGS) $(CXXFLAGS) -c . 因此, make会使用其内置规则从.cpp文件中创建.o文件,即$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c

CXX is set to g++ by default, but CPPFLAGS and CXXFLAGS are empty. 默认情况下, CXX设置为g++ ,但是CPPFLAGSCXXFLAGS为空。 This explains the extra spaces between g++ and -c in the output: 这解释了输出中g++-c之间的多余空格:

g++    -c -o card.o card.cpp

You can confirm this by running make -r , which disables all built-in rules. 您可以通过运行make -r来确认这一点,该命令将禁用所有内置规则。 It should now fail to find a rule for making card.o . 现在应该找不到创建card.o的规则。

To solve this, you should fix the capitalization of SortedLinkedList.h . 要解决此问题,您应该修复SortedLinkedList.h的大小写。 You should also consider using the built-in rules and just set CXXFLAGS = -std=c++11 . 您还应该考虑使用内置规则,只需设置CXXFLAGS = -std=c++11

Generally you want to use the CXXFLAGS implicit variable to specify this switch. 通常,您想使用CXXFLAGS隐式变量来指定此开关。 I am not sure why the current pattern rule is failing, but using CXXFLAGS will allow getting rid of it entirely 我不确定为什么当前的模式规则会失败,但是使用CXXFLAGS可以完全摆脱它

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

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