Thread: Including file containing a custom function

  1. #1
    Registered User
    Join Date
    Nov 2016
    Location
    Italy
    Posts
    2

    Question Including file containing a custom function

    Hi,

    I am new here, and I am a beginner in C coding, and I hope you can help me to understand better. I am reading the book An Introduction to GCC by Brian Gough. I don't understand a part where the program "hello world" has been split in three files:

    Here I post the code:

    file (1) main.c:
    Code:
    #include "hello.h"
    
    int main (void) {
       hello ("world");
       return 0
    }
    file (2) hello_fn.c:
    Code:
    #include <stdio.h>
    #include "hello.h"
    
    void hello (const char * name) {
       printf ("Hello, %s\n", name);
    }
    file (3) hello.h:
    Code:
    void hello (const char * name);
    I understand everything in the code, but, in what way the file hello_fn.c has been included in the program? and therefore why is it possible to use the custom function hello, even if in the source I can't see any inclusion for the file hello_fn.c?

    please, can you give me any suggestions? many thanks really!!

  2. #2
    Registered User
    Join Date
    Oct 2016
    Location
    India
    Posts
    1
    Ok, here source is written to be converted to object. so, files with .c extensions should be converted object. (source to object)
    after which both object files ".o" can be compiled into single executable file.

    to run the code :

    gcc -c hello_fn.c main.c
    gcc -o main.exe hello_fn.o main.o

    If you use Integrated Development Environment, it automatically does it for you..

  3. #3
    Nasal Demon Xupicor's Avatar
    Join Date
    Sep 2010
    Location
    Poland
    Posts
    179
    Or you can compile them in one go if you're just typing it in the terminal: g++ a.c b.c -o=result.exe
    Also, don't forget to use include guards in your header files.

    You can use custom function because the declaration from .h file is seen by the compiler, as you included it. You don't include .c file, but your compiler knows about definition in it because you supply it by the way of adding it to the compile line, in any of the above ways.

  4. #4
    Registered User
    Join Date
    Nov 2016
    Location
    Italy
    Posts
    2
    Quote Originally Posted by kr007 View Post
    after which both object files ".o" can be compiled into single executable file.
    so if I understand, I have not to "include" the file hello_fn.c, because, when we proceed to create an executable file, all the functions and variables, etc... after compiling they will look like as written in a unique file.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 09-25-2011, 12:22 AM
  2. Including a custom class file
    By Beowolf in forum C++ Programming
    Replies: 29
    Last Post: 09-29-2007, 05:29 PM
  3. Including default headerfiles in custom headerfiles
    By Sentral in forum C++ Programming
    Replies: 7
    Last Post: 02-18-2006, 09:42 AM
  4. Replies: 3
    Last Post: 05-03-2003, 12:04 PM
  5. including optional parameters in function
    By louiegonzalus in forum C Programming
    Replies: 4
    Last Post: 11-28-2002, 03:34 PM

Tags for this Thread