Does #include indirectly include the source file too?

This is a discussion on Does #include indirectly include the source file too? within the C Programming forums, part of the General Programming Boards category; I have read many tutorials on organizing code, and this simple yet important question is all that remains, for total ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    22

    Does #include indirectly include the source file too?

    I have read many tutorials on organizing code, and this simple yet important question is all that remains, for total enlightenment to be reached .

    So, when I #include "filename.h", the compiler uses the lines in the header .h file, OK, but does it ALSO use the lines in the filename.c(pp) too?
    Bet its yes, but i need to know for sure. Thanks!

  2. #2
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,650
    No, it includes only the included file.

  3. #3
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Quote Originally Posted by Lord Asriel View Post
    OK, but does it ALSO use the lines in the filename.c(pp) too?
    By 'use' if you mean.. blind copy paste.. then no..
    But it compiles those files separately and links to them.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Lord Asriel View Post
    I have read many tutorials on organizing code, and this simple yet important question is all that remains, for total enlightenment to be reached .

    So, when I #include "filename.h", the compiler uses the lines in the header .h file, OK, but does it ALSO use the lines in the filename.c(pp) too?
    Bet its yes, but i need to know for sure. Thanks!
    No... there are no meaningful links between files in C sourcecode. A header file is a header file, and it's only a header file.

    You must still compile your source code files separately and link the resulting object modules together to get a working program.

    All you're really doing with header files is making a promise : "The linker will find this later..." meaning the compiler is trusting that the function, variable and type prototypes in the header actually exist somewhere else.

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    22
    I see...so the use of headers is quite similar to when, in a one-source-file-program, I make function declarations before the main(), but add the definitions later?

    So, this also means that if i do not "add to project" the actual source files, everything will go wrong?

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Lord Asriel View Post
    So, this also means that if i do not "add to project" the actual source files, everything will go wrong?
    Yep, that's what it usually means.

    You need to add the .c sources to your project to be compiled.
    You need to add the .obj or .o files from each source file to be linked.

  7. #7
    Registered User
    Join Date
    Nov 2011
    Posts
    83
    Interesting.
    So putting include just puts a symbol in the object file?

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Swoorup View Post
    Interesting.
    So putting include just puts a symbol in the object file?
    No, that's not what it does.

    Header files are merged into source code files as text... in memory they become one continuous thing the compiler uses to compile to an object file. The action is to make a promise to the compiler: "This function (variable, struct, etc) can be found in a different file" so it will compile your code as if the function (etc.) actually exists... just like function prototypes at the top of a page.

    These prototyped functions obviously can't be compiled into the current oject, they're in different files, so the linker connects them together when building the runtime executable.

  9. #9
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,650
    Example of what happens.

    headers.h
    Code:
    #ifndef MY_HEADERS_H
    #define MY_HEADERS_H
    
    int anotherFunc(int n);
    
    #define MY_DEFAULT 5
    #define DOUBLE_VAL(x_) (x_ * 2)
    
    #endif
    headers.c
    Code:
    #include "headers.h"
    
    int main()
    {
       int myVal = MY_DEFAULT;
       int myValDoubled = DOUBLE_VAL(myVal);
    
       return 0;
    }
    Source file after pre-processing (including of headers):

    Code:
    gcc -E -P headers.c
    
    
    
    
    
    int anotherFunc(int n);
    int main()
    {
     int myVal = 5;
     int myValDoubled = (myVal * 2);
     return 0;
    }
    Notice also how the macros have been expanded in the .c file.

    If we include stdio.h as well (before the #include "headers.h"), here's the result (Mac OS X):

    Code:
    gcc -E -P headers.c
    typedef signed char __int8_t;
    typedef unsigned char __uint8_t;
    typedef short __int16_t;
    typedef unsigned short __uint16_t;
    typedef int __int32_t;
    typedef unsigned int __uint32_t;
    typedef long long __int64_t;
    typedef unsigned long long __uint64_t;
    typedef long __darwin_intptr_t;
    typedef unsigned int __darwin_natural_t;
    typedef int __darwin_ct_rune_t;
    typedef union {
     char __mbstate8[128];
     long long _mbstateL;
    } __mbstate_t;
    typedef __mbstate_t __darwin_mbstate_t;
    typedef long int __darwin_ptrdiff_t;
    typedef long unsigned int __darwin_size_t;
    typedef __builtin_va_list __darwin_va_list;
    typedef int __darwin_wchar_t;
    typedef __darwin_wchar_t __darwin_rune_t;
    typedef int __darwin_wint_t;
    typedef unsigned long __darwin_clock_t;
    typedef __uint32_t __darwin_socklen_t;
    typedef long __darwin_ssize_t;
    typedef long __darwin_time_t;
    struct __darwin_pthread_handler_rec
    {
     void (*__routine)(void *);
     void *__arg;
     struct __darwin_pthread_handler_rec *__next;
    };
    struct _opaque_pthread_attr_t { long __sig; char __opaque[56]; };
    struct _opaque_pthread_cond_t { long __sig; char __opaque[40]; };
    struct _opaque_pthread_condattr_t { long __sig; char __opaque[8]; };
    struct _opaque_pthread_mutex_t { long __sig; char __opaque[56]; };
    struct _opaque_pthread_mutexattr_t { long __sig; char __opaque[8]; };
    struct _opaque_pthread_once_t { long __sig; char __opaque[8]; };
    struct _opaque_pthread_rwlock_t { long __sig; char __opaque[192]; };
    struct _opaque_pthread_rwlockattr_t { long __sig; char __opaque[16]; };
    struct _opaque_pthread_t { long __sig; struct __darwin_pthread_handler_rec *__cleanup_stack; char __opaque[1168]; };
    typedef __int64_t __darwin_blkcnt_t;
    typedef __int32_t __darwin_blksize_t;
    typedef __int32_t __darwin_dev_t;
    typedef unsigned int __darwin_fsblkcnt_t;
    typedef unsigned int __darwin_fsfilcnt_t;
    typedef __uint32_t __darwin_gid_t;
    typedef __uint32_t __darwin_id_t;
    typedef __uint64_t __darwin_ino64_t;
    typedef __darwin_ino64_t __darwin_ino_t;
    typedef __darwin_natural_t __darwin_mach_port_name_t;
    typedef __darwin_mach_port_name_t __darwin_mach_port_t;
    typedef __uint16_t __darwin_mode_t;
    typedef __int64_t __darwin_off_t;
    typedef __int32_t __darwin_pid_t;
    typedef struct _opaque_pthread_attr_t
       __darwin_pthread_attr_t;
    typedef struct _opaque_pthread_cond_t
       __darwin_pthread_cond_t;
    typedef struct _opaque_pthread_condattr_t
       __darwin_pthread_condattr_t;
    typedef unsigned long __darwin_pthread_key_t;
    typedef struct _opaque_pthread_mutex_t
       __darwin_pthread_mutex_t;
    typedef struct _opaque_pthread_mutexattr_t
       __darwin_pthread_mutexattr_t;
    typedef struct _opaque_pthread_once_t
       __darwin_pthread_once_t;
    typedef struct _opaque_pthread_rwlock_t
       __darwin_pthread_rwlock_t;
    typedef struct _opaque_pthread_rwlockattr_t
       __darwin_pthread_rwlockattr_t;
    typedef struct _opaque_pthread_t
       *__darwin_pthread_t;
    typedef __uint32_t __darwin_sigset_t;
    typedef __int32_t __darwin_suseconds_t;
    typedef __uint32_t __darwin_uid_t;
    typedef __uint32_t __darwin_useconds_t;
    typedef unsigned char __darwin_uuid_t[16];
    typedef char __darwin_uuid_string_t[37];
    typedef int __darwin_nl_item;
    typedef int __darwin_wctrans_t;
    typedef __uint32_t __darwin_wctype_t;
    typedef __darwin_va_list va_list;
    typedef __darwin_off_t off_t;
    typedef __darwin_size_t size_t;
    
    
    typedef __darwin_off_t fpos_t;
    struct __sbuf {
     unsigned char *_base;
     int _size;
    };
    struct __sFILEX;
    typedef struct __sFILE {
     unsigned char *_p;
     int _r;
     int _w;
     short _flags;
     short _file;
     struct __sbuf _bf;
     int _lbfsize;
     void *_cookie;
     int (*_close)(void *);
     int (*_read) (void *, char *, int);
     fpos_t (*_seek) (void *, fpos_t, int);
     int (*_write)(void *, const char *, int);
     struct __sbuf _ub;
     struct __sFILEX *_extra;
     int _ur;
     unsigned char _ubuf[3];
     unsigned char _nbuf[1];
     struct __sbuf _lb;
     int _blksize;
     fpos_t _offset;
    } FILE;
    
    extern FILE *__stdinp;
    extern FILE *__stdoutp;
    extern FILE *__stderrp;
    
    
    void clearerr(FILE *);
    int fclose(FILE *);
    int feof(FILE *);
    int ferror(FILE *);
    int fflush(FILE *);
    int fgetc(FILE *);
    int fgetpos(FILE * , fpos_t *);
    char *fgets(char * , int, FILE *);
    FILE *fopen(const char * , const char * ) __asm("_" "fopen" );
    int fprintf(FILE * , const char * , ...) ;
    int fputc(int, FILE *);
    int fputs(const char * , FILE * ) __asm("_" "fputs" );
    size_t fread(void * , size_t, size_t, FILE * );
    FILE *freopen(const char * , const char * ,
         FILE * ) __asm("_" "freopen" );
    int fscanf(FILE * , const char * , ...) ;
    int fseek(FILE *, long, int);
    int fsetpos(FILE *, const fpos_t *);
    long ftell(FILE *);
    size_t fwrite(const void * , size_t, size_t, FILE * ) __asm("_" "fwrite" );
    int getc(FILE *);
    int getchar(void);
    char *gets(char *);
    extern const int sys_nerr;
    extern const char *const sys_errlist[];
    void perror(const char *);
    int printf(const char * , ...) ;
    int putc(int, FILE *);
    int putchar(int);
    int puts(const char *);
    int remove(const char *);
    int rename (const char *, const char *);
    void rewind(FILE *);
    int scanf(const char * , ...) ;
    void setbuf(FILE * , char * );
    int setvbuf(FILE * , char * , int, size_t);
    int sprintf(char * , const char * , ...) ;
    int sscanf(const char * , const char * , ...) ;
    FILE *tmpfile(void);
    char *tmpnam(char *);
    int ungetc(int, FILE *);
    int vfprintf(FILE * , const char * , va_list) ;
    int vprintf(const char * , va_list) ;
    int vsprintf(char * , const char * , va_list) ;
    int asprintf(char **, const char *, ...) ;
    int vasprintf(char **, const char *, va_list) ;
    
    
    char *ctermid(char *);
    char *ctermid_r(char *);
    FILE *fdopen(int, const char *) __asm("_" "fdopen" );
    char *fgetln(FILE *, size_t *);
    int fileno(FILE *);
    void flockfile(FILE *);
    const char
     *fmtcheck(const char *, const char *);
    int fpurge(FILE *);
    int fseeko(FILE *, off_t, int);
    off_t ftello(FILE *);
    int ftrylockfile(FILE *);
    void funlockfile(FILE *);
    int getc_unlocked(FILE *);
    int getchar_unlocked(void);
    int getw(FILE *);
    int pclose(FILE *);
    FILE *popen(const char *, const char *) __asm("_" "popen" );
    int putc_unlocked(int, FILE *);
    int putchar_unlocked(int);
    int putw(int, FILE *);
    void setbuffer(FILE *, char *, int);
    int setlinebuf(FILE *);
    int snprintf(char * , size_t, const char * , ...) ;
    char *tempnam(const char *, const char *) __asm("_" "tempnam" );
    int vfscanf(FILE * , const char * , va_list) ;
    int vscanf(const char * , va_list) ;
    int vsnprintf(char * , size_t, const char * , va_list) ;
    int vsscanf(const char * , const char * , va_list) ;
    FILE *zopen(const char *, const char *, int);
    
    
    FILE *funopen(const void *,
      int (*)(void *, char *, int),
      int (*)(void *, const char *, int),
      fpos_t (*)(void *, fpos_t, int),
      int (*)(void *));
    
    
    int __srget(FILE *);
    int __svfscanf(FILE *, const char *, va_list) ;
    int __swbuf(int, FILE *);
    
    static __inline int __sputc(int _c, FILE *_p) {
     if (--_p->_w >= 0 || (_p->_w >= _p->_lbfsize && (char)_c != '\n'))
      return (*_p->_p++ = _c);
     else
      return (__swbuf(_c, _p));
    }
    extern int __sprintf_chk (char * , int, size_t,
         const char * , ...)
      ;
    extern int __snprintf_chk (char * , size_t, int, size_t,
          const char * , ...)
      ;
    extern int __vsprintf_chk (char * , int, size_t,
          const char * , va_list)
      ;
    extern int __vsnprintf_chk (char * , size_t, int, size_t,
           const char * , va_list)
      ;
    int anotherFunc(int n);
    int main()
    {
     int myVal = 5;
     int myValDoubled = (myVal * 2);
     return 0;
    }
    Last edited by rags_to_riches; 11-30-2011 at 04:31 AM.

  10. #10
    Registered User
    Join Date
    Nov 2011
    Posts
    83
    Suppose we made a function call printf. So will the whole function be included our program or will it just load from the library?

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Swoorup View Post
    Suppose we made a function call printf. So will the whole function be included our program or will it just load from the library?
    It will get it from the library...

    When you compile your source code, you are actually building libraries and handing them to the linker.

    The minimum library is one object. Most libraries are collections of objects.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 11-06-2011, 05:20 PM
  2. [GCC] Specifying include libraries in source files
    By _Mike in forum C Programming
    Replies: 5
    Last Post: 03-17-2010, 10:06 AM
  3. #include <windows.h> and #include <wininet.h>
    By steve1_rm in forum C++ Programming
    Replies: 4
    Last Post: 03-30-2009, 11:14 AM
  4. Replies: 3
    Last Post: 02-10-2009, 01:51 PM
  5. Which came first? #include <stdio.h> or #include <stdlib.h> ?
    By Brian in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 01-14-2002, 09:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21