Understanding Separate Compilation

  • Thread starter Frankie Montenegro
  • Start date
F

Frankie Montenegro

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.
 
M

Mark P

Frankie said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top