然后您有了第二个源文件: // file happyletter.cpp #include <cstdio> #include "happy.h" void HappyLetter::speak() { printf("Hello from HappyLetter.cpp!\n"); }最后,出现一个Makefile(请参见此处以获取灵感),例如: # file Makefile CXX= g++ CXXFLAGS= -std=c++11 -Wall -Wextra -g RM= rm -f .PHONY: all clean all: happy-prog clean: $(RM) *.o *~ happy-prog happy-prog: happy.o happyletter.o happy.o: happy.cpp happy.h happyletter.o: happyletter.cpp happy.h请注意对happy.h标头的明确依赖我评论过,考虑使用 remake -x或make --trace进行调试您的Makefile.注意,GNU make有很多内置规则,请运行make -p来获取它们.详细了解 C ++ 11 ,尤其是教程,很好的使用C ++编程本书,对标准有所了解(例如 n3337 草案).另请阅读有关 make 的信息,尤其是 GNU make .研究使用C ++编码的某些现有免费软件的源代码(请参阅 sourceforge 或 github 等...找到一个). I have written the following makefile: CC=g++all: happyhappy: happy.o HappyLetter.o $(CC) -o happy happy.o HappyLetter.ohappy.o: happy.cpp $(CC) -c happy.cppHappyLetter.o: HappyLetter.cpp $(CC) -c HappyLetter.cppclean: rm -rf *.o happyand am working with the files HappyLetter.cpp and happy.cpp (which includes the former) to create an executable named happy.I can build the code successfully using make. However, when I modify HappyLetter.cpp and type 'make' again, the change is not reflected. It only works when I type 'make clean' and then 'make'. The update of the object file that I expect to take place is echoed to the command line: $ makeg++ -c HappyLetter.cppg++ -o happy happy.o HappyLetter.oHowever, the update to HappyLetter.cpp is not being reflected in happy.The problem does not work in the other direction. That is, if I modify happy.cpp, the change is reflected immediately after I type 'make'.I have replicated this problem with three make binaries on my Mac OS X, and also on an Ubuntu machine. So I must be doing something wrong in the coding. Here is the text of the files, which are in the same directory as the makefile:happy.cpp#include "HappyLetter.cpp"int main(){ printf("Hello from happy.cpp!\n"); HappyLetter *myObj = new HappyLetter(); myObj->speak(); return 0;}HappyLetter.cpp#include <cstdio>class HappyLetter { public: void speak() { printf("Hello from HappyLetter.cpp!\n"); }};I believe the problem is something simple, but I have no more ideas about what to check. One assumption I have is that the ordering of the rules and dependencies does not matter. 解决方案First, you should (conventionally) not #include "HappyLetter.cpp" in your happy.cpp (even if that is doable but poor taste). You should have a separate header file (with the conventional include guard)#ifndef HAPPY_INCLUDED//// file happy.h#define HAPPY_INCLUDED class HappyLetter { public: void speak(); };#endif /*HAPPY_INCLUDED*/Then you should have a first source file:// file happy.cpp #include <cstdio> #include "happy.h"int main() { printf("Hello from happy.cpp!\n"); HappyLetter *myObj = new HappyLetter(); myObj->speak(); delete myObj; return 0;}Then you have your second source file: // file happyletter.cpp #include <cstdio> #include "happy.h" void HappyLetter::speak() { printf("Hello from HappyLetter.cpp!\n"); }At last, a Makefile (see here for inspiration), like: # file Makefile CXX= g++ CXXFLAGS= -std=c++11 -Wall -Wextra -g RM= rm -f .PHONY: all clean all: happy-prog clean: $(RM) *.o *~ happy-prog happy-prog: happy.o happyletter.o happy.o: happy.cpp happy.h happyletter.o: happyletter.cpp happy.hNotice the explicit dependency on happy.h headerAs I commented, consider using remake-x or make --trace to debug your Makefile. Notice that GNU make has a lot of built-in rules, run make -p to get them.Read more about C++11, notably a tutorial, a good programming in C++ book, have a glance into the standard (e.g. n3337 draft). Read also about make, notably GNU make.Study the source code of some existing free software coded in C++ (see sourceforge or github etc... to find one). 这篇关于使不更新可执行文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 07-18 15:27