Frankie Montenegro wrote:
Hi
I am trying to understand separate compilation and how to set up
simple Makefiles. This is my question:
Say I have a simple 'nonmain' sourcefile. Just a couple of functions, not
longer than 10 lines alltogether. I wondered if it's possible to combine
this source file with its header file into a single file (nonmain.h).
Is this "bad practice"? For some reason, having a
separate header for such a simple source file seems like an overkill.
Thnkas for your help.
Sure you can. When you #include a header file, the compiler
(preprocessor) just copies its contents into the including file. If you
want to do it yourself you can.
However, if you intend to have more than one source file (translation
unit) include the same header then it's important that that header not
contain any function definitions that aren't declared inline or
templates. Otherwise the linker will object to this violation of the
one definition rule (ODR).
If you're just writing a small standalone application and don't intend
to use its contents anywhere else then it's certainly convenient to have
everything in one file (most people would name this *.cpp, not *.h).
But since you call your file 'nomain' I infer that this is not a
standalone program and you should then be careful about violating the ODR.
Mark