The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
A basic question
Discuss A basic question in the C Programming forum on Dev Shed. A basic question C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

February 23rd, 2013, 05:15 AM
|
|
Registered User
|
|
Join Date: Feb 2013
Posts: 3
Time spent in forums: 1 h 30 m 5 sec
Reputation 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.
|

February 23rd, 2013, 05:29 AM
|
 |
Contributed User
|
|
|
|
|
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
|
 |
Banned ;)
|
|
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
|
|
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:
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
|

February 23rd, 2013, 05:37 AM
|
|
Registered User
|
|
Join Date: Feb 2013
Posts: 3
Time spent in forums: 1 h 30 m 5 sec
Reputation Power: 0
|
|
Quote: | 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"
|

February 23rd, 2013, 05:42 AM
|
|
Registered User
|
|
Join Date: Feb 2013
Posts: 3
Time spent in forums: 1 h 30 m 5 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by Scorpions4ever 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:
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--
And i got a.out
|

February 23rd, 2013, 06:46 AM
|
 |
Contributed User
|
|
|
|
|
> 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 06:48 AM.
|

February 23rd, 2013, 12:46 PM
|
 |
Contributing User
|
|
Join Date: Jan 2003
Location: USA
|
|
Quote: | 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--
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.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|