Thread: Including source file twice, but with include guard

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    204

    Including source file twice, but with include guard

    I have four source files. The main source file includes two other source files. The two other source files both include the fourth source file. In the fourth source file I have an include guard. Will the code from the fourth source file exist in two locations in the compiled code? Is this something that is compiler dependent? An example of this is shown in the code below.

    Code:
    // filename: main.c
    
    #include "source1.c"
    #include "source2.c"
    
    int main()
    {
    	return 0;
    }
    Code:
    // filename: source1.c
    
    #include "source3.c"

    Code:
    // filename: source2.c
    
    #include "source3.c"

    Code:
    // filename: source3.c
    
    #ifndef __SOURCE3__
    #define __SOURCE3__
    
    int myFunction()
    {
    	return 1;
    }
    
    #endif

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by thetinman View Post
    Will the code from the fourth source file exist in two locations in the compiled code? Is this something that is compiler dependent?
    Short answers: Yes. No.

    Longer answer:

    In your example, the definition of myFunction() will be in both translation units. That breaches the one-definition rule. Practically, when linking, the linker will usually complain about that function being defined multiple times (the fact the definitions will be equivalent doesn't change that - basic linker technology is simply not that smart).

    Generally speaking, #including source files (.c) is usually not done, precisely because it causes breaches of the one-definition rule. Include guards do not change that.

    There is an exception for inline functions, but inline functions are conventionally defined in header files, not in source files.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    IIRC, the double underline prefix is NOT allowed to be used by normal C programmers; not sure, But, I think only Compiler Writers that is allowed to use it.

    Code:
    #ifndef __SOURCE3__
    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by stahta01 View Post
    IIRC, the double underline prefix is NOT allowed to be used by normal C programmers; not sure, But, I think only Compiler Writers that is allowed to use it.
    Yes, indeed. The C standard reserves such identifiers (and others with leading underscores) for use by the implementation.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Do not #include source files. The conventional way to do what you are trying to do is to make all source files independent (.c) and then compile them all together. Only the include files typically have include guards for convenience.

    Code:
    gcc -o myprogram main.c source1.c source2.c source3.c
    main.c:
    Code:
    #include "source1.h"
    #include "source2.h"
    #include "source3.h"
    
    int main()
    {
        myFunction();
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Should I consider including my .DLL source code in my program?
    By HelpfulPerson in forum C Programming
    Replies: 2
    Last Post: 12-17-2013, 07:37 PM
  2. Does #include indirectly include the source file too?
    By Lord Asriel in forum C Programming
    Replies: 10
    Last Post: 11-30-2011, 08:20 AM
  3. Replies: 11
    Last Post: 09-25-2011, 12:22 AM
  4. Replies: 3
    Last Post: 02-10-2009, 02:51 PM
  5. inclusion guard help
    By romerboy55 in forum C++ Programming
    Replies: 3
    Last Post: 09-02-2006, 12:39 AM