Thread: Why I don't have to include code.c in this example?

  1. #1
    Registered User
    Join Date
    Apr 2007

    Why I don't have to include code.c in this example?

    In code.c

    int accum = 0;                                                                                                                                                               
    int sum (int x, int y)                                                                                                                                                       
        int t = x + y;                                                                                                                                                           
        accum += t;                                                                                                                                                              
        return t;                                                                                                                                                                
    In test.c
    //#include "code.c"                                                                                                                                                          
                                                                                                                                                                                 int main()                                                                                                                                                                   
      return sum(1,3);                                                                                                                                                           
    I use

    gcc -O2 -c code.c
    and get code.o

    gcc -O2 -o prog code.o test.c
    It's interesting to see I don't have to include code.c in my code, why is that?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Because you linked it. That's what the .o file is -- it's the compiled version of the .c file. (-c means to compile only, and not try to make a "real" executable.) Remember: #include'ing .c files is evil and wrong; if your source code is split over multiple files, compile each of them into an .o file and then link them just like so.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Farncombe, Surrey, England
    1. I don't think test.c would compile - it's missing at least the function that calls sum().

    2. Assuming that test.c is syntactically correct, you should also use a prototype for sum() - which could be in a "code.h" included in both code.c and code.h - that way, if you change it in one place but not the other, the compiler would be able to tell you that you messed up. If you enable warnings (-Wall in gcc), you should be told that there's a missing prototype when calling sum in test.c - it is always a good idea to get as much help from the compiler as possible when compiling the code.

    I agree with tabstop, you should NEVER EVER include a .c file in another source file - if it's to be included, it should be in a .h file.

    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  2. MFC include BS
    By VirtualAce in forum Windows Programming
    Replies: 4
    Last Post: 10-31-2005, 12:44 PM
  3. help with finding lowest number entered
    By volk in forum C++ Programming
    Replies: 12
    Last Post: 03-22-2003, 01:21 PM
  4. #includes don't seem to be working
    By Inquirer in forum C++ Programming
    Replies: 12
    Last Post: 08-05-2002, 05:38 PM
  5. MFC Assertion Failure
    By maxthecat in forum Windows Programming
    Replies: 5
    Last Post: 08-01-2002, 09:58 AM