#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    6
    Rep Power
    0

    A basic question


    i am trying to write a multi-file program.I have written a file ,program.c --
    Code:
    #include<stdio.h>
    #include "fun.h"
    int main()
    {
             int a=9,b=8;
             printf("%d\n",add(a,b));
             return 0;
    }
    other c file is fun.c,which is having definition of a function "add".
    fun.c is as follows--
    Code:
    int add(int a,int b)
    {
          return a+b;
    }
    third and last file is a header file ,fun.h --
    Code:
    int add(int a,int b);
    all files are in same folder.When i am compiling --
    cc program.c
    i am getting following message--
    Code:
    /tmp/ccgX3rbl.o: In function `main':
    program.c:(.text+0x29): undefined reference to `add' collect2: ld returned 1 exit status
    help me with this please !!!
    may be this is too dumb question to be posted here,
    but i am a beginner and stuck.
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,417
    Rep Power
    1871
    You need to use the command
    cc program.c fun.c

    Later on, you will use something like this

    cc -c program.c
    cc -c fun.c
    cc program.o fun.o

    which is the essence of separate compilation, where you don't have to compile everything when you only change one thing.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  4. #3
  5. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,648
    Rep Power
    4248
    Thank you for posting with proper code tags. With respect to your question, you need to compile it as follows:
    Code:
    cc program.c fun.c
    You need to tell the compiler the names of all the .c files, so that the linker will know where to find the necessary function code. This will produce an executable called a.out

    If you want to name the executable something other than a.out, you can compile like this:
    Code:
    cc -o myprogram program.c fun.c
    which will produce an executable called myprogram.

    Later when you learn some more C, research the concept of Makefile and the -c option of your C compiler. This allows you to compile the .c files separately and then get the linker to join the .o files together as needed.
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    6
    Rep Power
    0
    Originally Posted by salem
    You need to use the command
    cc program.c fun.c

    Later on, you will use something like this

    cc -c program.c
    cc -c fun.c
    cc program.o fun.o

    which is the essence of separate compilation, where you don't have to compile everything when you only change one thing.
    Thank you salem for your reply,i tried what you told(both ways).Now i am able to compile and execute without error.But i have a doubt,even if i do not use that header file fun.h and do not include it in program.c the compilation is fine.So my question is
    WHY we use #include "something.h"
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    6
    Rep Power
    0
    Originally Posted by Scorpions4ever
    Thank you for posting with proper code tags. With respect to your question, you need to compile it as follows:
    Code:
    cc program.c fun.c
    You need to tell the compiler the names of all the .c files, so that the linker will know where to find the necessary function code. This will produce an executable called a.out

    If you want to name the executable something other than a.out, you can compile like this:
    Code:
    cc -o myprogram program.c fun.c
    which will produce an executable called myprogram.

    Later when you learn some more C, research the concept of Makefile and the -c option of your C compiler. This allows you to compile the .c files separately and then get the linker to join the .o files together as needed.
    Thank you for the reply,i have another doubt--
    what is the purpose of fun.h file i have created.
    I tried removing #include"fun.h" from program.c
    then i did like you said--
    Code:
    cc program.c fun.c
    And i got a.out
  10. #6
  11. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,417
    Rep Power
    1871
    > WHY we use #include "something.h"
    Because you should (will soon need to) prototype functions before you call them.

    In main, you had this.
    > printf("%d\n",add(a,b));
    The compiler didn't know what add was, but it knew enough to know it was a function. So it 'invented' the following declaration for you.

    extern int add();

    Now, luckily for you, passing int parameters is exactly what your implementation in fun.c expected, so it all worked.

    Now try this WITHOUT including the header file.
    printf("%d\n",add("hello","world"));

    Now try this WITH including the header file.
    printf("%d\n",add("hello","world"));
    Last edited by salem; February 23rd, 2013 at 07:48 AM.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  12. #7
  13. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,255
    Rep Power
    2222
    Originally Posted by aliveashish
    Thank you for the reply,i have another doubt--
    what is the purpose of fun.h file i have created.
    I tried removing #include"fun.h" from program.c
    then i did like you said--
    Code:
    cc program.c fun.c
    And i got a.out
    What warnings did you get? You had to have gotten a warning about the implicit declaration of add().

    Assuming that gcc got it from cc, your command should have been:
    Code:
    cc -Wall program.c fun.c
    which turns on all warnings.

    Never ignore warnings. Warnings are more important than error messages. And never run a program with warnings; you never know exactly what it will do.

IMN logo majestic logo threadwatch logo seochat tools logo