How do you compile just a .h file in a makefile?

倾然丶 夕夏残阳落幕 提交于 2019-12-23 10:16:57

问题


I have a makefile that creates object files for two classes (and main) and one of those classes is just defined in a .h file. In my makefile I have a line that says

FileName.o: FileName.h
  g++ -c FileName.h

but when I try to compile it says it can't find FileName.o

Do I have to create FileName.cpp in order to get this to compile?


回答1:


You are using your class from FileName.h somewhere, aren't you? So at least one of your .cpp files should contain #include "FileName.h", and .h's code will be compiled with this .cpp and you needn't compile .h's code separately.




回答2:


You don't normally attempt to compile a header (.h) file by itself. Including it into an otherwise empty .cpp file will let you compile it and produce a .o file, but it probably won't do much (if any) real good unless you've put things in the header that don't really belong in a header.



来源:https://stackoverflow.com/questions/10112851/how-do-you-compile-just-a-h-file-in-a-makefile

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!