I am having trouble understanding an answer I saw in another post. It said that it is good practice to define a struct in a separate .h file so it can be used in other files. I think that is great and it solves my current dilemma, however I have a question about compilation and makefiles. I am only familiar with having header files that are associated with .cpp files at the moment.
Can someone explain how that implementation would look when I have a .h and no .cpp? Do I need an implementation file as well? Also, how do I link the header in a makefile? Currently I only know how to compile a .cpp & header into a .o file and link them.
Thanks, and sorry for taking us back to c++ kindergarten. This is a new revelation and seems like a good one.
The key concept is that you define the struct/class in a
.h
header, so that you can use it in multiple.cpp
files. Whenever you needstruct foo
defined infoo.h
, you#include "foo.h"
. You don't need to directly compile the header file, it will be pulled in by whichever source file uses it. Therefore you don't need a make target for.h
in normal circumstances.If the definition in the header is never used, it won't be pulled in and that's it.