LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 12-15-2011, 06:13 AM   #1
ant___man
LQ Newbie
 
Registered: Dec 2011
Distribution: openSUSE 11.4
Posts: 2

Rep: Reputation: Disabled
c program compiling with header file


C program fails to compile when a header file is used.

ant@linux-kj8g:~/ex05> gcc-4.5 main.c header.h
/tmp/ccB42s3i.o: In function `main':
main.c.text+0x13): undefined reference to `add'
collect2: ld returned 1 exit status

Note: this compiles ok, when the header file is not used
ant@linux-kj8g:~/ex05> gcc-4.5 main.c add.c
ant@linux-kj8g:~/ex05> ./a.out
7

ant@linux-kj8g:~/ex05> more header.h main.c add.c
::::::::::::::
header.h
::::::::::::::
int add(int a, int b);

::::::::::::::
main.c
::::::::::::::
#include <stdio.h>
#include "header.h"
main()
{
int acummulate;
acummulate = add(3, 4);
printf("%d \n", acummulate);
}

::::::::::::::
add.c
::::::::::::::
#include "header.h"
#include <stdio.h>
int add(int a, int b)
{
return a + b;
}

I've spent an embarrassingly long time playing with this,
and looking for answers on the internet.
Will be very grateful to anyone who provides and answer. Thanks.
 
Old 12-15-2011, 06:15 AM   #2
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
The header doesn't contain the code for the function, only the function prototype. How is the compiler* supposed to know what the body of the function is if you only use the header?

Edit: *Actually, the linker.

Edit: There's a bit more of an explanation here. I've also reported this thread for moving to Programming, since it fits better there.

Last edited by Nylex; 12-15-2011 at 06:25 AM.
 
1 members found this post helpful.
Old 12-15-2011, 06:28 AM   #3
ant___man
LQ Newbie
 
Registered: Dec 2011
Distribution: openSUSE 11.4
Posts: 2

Original Poster
Rep: Reputation: Disabled
most excellent, thank you.
 
Old 12-15-2011, 09:02 AM   #4
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,498

Rep: Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956
Moved: This thread is more suitable in Programming and has been moved accordingly to help your thread/question get the exposure it deserves.
 
Old 12-15-2011, 03:53 PM   #5
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,395
Blog Entries: 2

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
You don't need to explicitly compile the header file, since it is included in the compilation for all C source files that need it:
Code:
#include "header.h"
What you may need to do is tell the compiler where to find the header file, using the compiler's '-I' (dash-eye) option. It is unusual to specify the specific version of the compiler. Generally, when installed, the installer will create generic links to the specific compiler version, so you only need to specify 'gcc'. It you have multiple versions of compilers mixed in the same directory, then that may cause problems. 'gcc' is made up of several separate components, and will launch them as needed. It may not know how to launch a specific version, and may end up using a component from a different version.

--- rod.
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to check missing header files included from another header file adisan82 Linux - Software 1 01-28-2011 03:57 AM
Missing header GLTK in compiling an openGL program BorgAssimilator Programming 1 01-23-2006 09:07 PM
Compiling C++ Header Files ?? JinBaba Programming 3 07-20-2005 03:23 AM
Compiling a header file with gcc Squall Linux - Software 2 04-07-2004 05:16 AM
How to compile a C program in Glade which is included the <pcap.h> header file. swaviswa Programming 0 03-21-2004 07:47 AM


All times are GMT -5. The time now is 07:38 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration