简体   繁体   English

初学者GNU Makefile错误

[英]beginner GNU Makefile Error

This is my first, attempt at a, Makefile after necessity from a previous post . 这是我从上一篇帖子的必要性之后第一次尝试Makefile。

Anyway here's the code 无论如何这里是代码

SortLab.exe : SelectionSort.o Main.o  
        g++ -o $@ $^  
SelectionSort.o : SelectionSort.cpp SelectionSort.h  
Main.o : Main.cpp  

#-------------------------------------------------------------

run: SortLab.exe  
        ./SortLab.exe  

clean:  
    rm -f *.o  
    rm -f *.exe  

build: clean SortLab.exe  

%.o: %.cpp  
    g++ -c $<  

I intend to have SelectionSort.cpp & SelectionSort.h form an object file, and Main.cpp to form its own object file. 我打算让SelectionSort.cppSelectionSort.h形成一个目标文件,并将Main.cpp形成自己的目标文件。 Then finally create an executable. 最后创建一个可执行文件。 Main.cpp depends on SelectionSort.cpp , where do I go wrong? Main.cpp取决于SelectionSort.cpp ,我在哪里出错?

Also where can I find what the different GNU commands mean, -o -c and such 另外在哪里可以找到不同的GNU命令的含义, -o -c

  1. You shouldn't need to define the %.o: %.cpp rule yourself, Make knows how to compile C++. 您不需要自己定义%.o: %.cpp规则,Make知道如何编译C ++。
  2. Indent with tabs, not spaces; 缩进标签,而不是空格; Make is sensitive to the difference. Make对这种差异很敏感。
  3. Every object file should depend on the headers included in the source files it depends on. 每个目标文件都应取决于其依赖的源文件中包含的头。 You probably need Main.o : Main.cpp SelectionSort.h . 您可能需要Main.o : Main.cpp SelectionSort.h
  4. build shouldn't depend on clean , it defeats one of Make's main features (selectively recompilation when files have changed). build不应该依赖clean ,它会破坏Make的主要功能之一(当文件发生变化时有选择地重新编译)。
  5. If you make build the first target, you can run Make without a target to get a full compile. 如果将build作为第一个目标,则可以在没有目标的情况下运行Make以获得完整的编译。 It's customary to call the main target all . 通常将主要目标称为all

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

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