C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 23rd, 2013, 05:15 AM
aliveashish aliveashish is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 3 aliveashish User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #2  
Old February 23rd, 2013, 05:29 AM
salem's Avatar
salem salem is offline
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,840 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 2 Days 19 h 19 m 34 sec
Reputation Power: 1774
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

Reply With Quote
  #3  
Old February 23rd, 2013, 05:33 AM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 9th Plane (9000 - 9499 posts)
 
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
Posts: 9,389 Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 4 Weeks 1 Day 22 h 1 m 11 sec
Reputation Power: 4080
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

Reply With Quote
  #4  
Old February 23rd, 2013, 05:37 AM
aliveashish aliveashish is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 3 aliveashish User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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"

Reply With Quote
  #5  
Old February 23rd, 2013, 05:42 AM
aliveashish aliveashish is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 3 aliveashish User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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:
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

Reply With Quote
  #6  
Old February 23rd, 2013, 06:46 AM
salem's Avatar
salem salem is offline
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,840 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 2 Days 19 h 19 m 34 sec
Reputation Power: 1774
> 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.

Reply With Quote
  #7  
Old February 23rd, 2013, 12:46 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is online now
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,136 dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 3 Days 21 h 10 m 31 sec
Reputation Power: 1974
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--
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > A basic question

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap