February 23rd, 2013, 05:15 AM
A basic question
i am trying to write a multi-file program.I have written a file ,program.c --
other c file is fun.c,which is having definition of a function "add".
fun.c is as follows--
third and last file is a header file ,fun.h --
int add(int a,int b)
all files are in same folder.When i am compiling --
int add(int a,int b);
i am getting following message--
help me with this please !!!
/tmp/ccgX3rbl.o: In function `main':
program.c:(.text+0x29): undefined reference to `add' collect2: ld returned 1 exit status
may be this is too dumb question to be posted here,
but i am a beginner and stuck.
February 23rd, 2013, 05:29 AM
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.
February 23rd, 2013, 05:33 AM
Thank you for posting with proper code tags. With respect to your question, you need to compile it as follows:
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:
which will produce an executable called myprogram.
cc -o myprogram program.c fun.c
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
February 23rd, 2013, 05:37 AM
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
Originally Posted by salem
WHY we use #include "something.h"
February 23rd, 2013, 05:42 AM
Thank you for the reply,i have another doubt--
Originally Posted by Scorpions4ever
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--
And i got a.out
February 23rd, 2013, 06:46 AM
> WHY we use #include "something.h"
Because you should (will soon need to) prototype functions before you call them.
In main, you had this.
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.
Now try this WITH including the header file.
Last edited by salem; February 23rd, 2013 at 06:48 AM.
February 23rd, 2013, 12:46 PM
What warnings did you get? You had to have gotten a warning about the implicit declaration of add().
Originally Posted by aliveashish
Assuming that gcc got it from cc, your command should have been:
which turns on all warnings.
cc -Wall program.c fun.c
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.