Understanding Separate Compilation

Discussion in 'C++' started by Frankie Montenegro, Oct 7, 2005.

  1. 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.
     
    Frankie Montenegro, Oct 7, 2005
    #1
    1. Advertising

  2. Frankie Montenegro

    Mark P Guest

    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
     
    Mark P, Oct 8, 2005
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. EvgueniB
    Replies:
    1
    Views:
    637
    Anthony Borla
    Dec 15, 2003
  2. Frank Fredstone
    Replies:
    1
    Views:
    447
    Jean-Francois Briere
    Jun 27, 2006
  3. Dan Stromberg
    Replies:
    2
    Views:
    401
    Mike Schilling
    Jan 3, 2008
  4. C__chp
    Replies:
    4
    Views:
    510
    Puppet_Sock
    Feb 15, 2008
  5. James Harris
    Replies:
    21
    Views:
    584
    Eric Sosman
    Nov 27, 2012
Loading...

Share This Page