![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
Join Date: May 2005
Location: ma
Posts: 130
Rep Power: 4
![]() |
question about header files and prototyping functions
hi. i was wondering if you had to do anything besides #include "foo.h" to include a header file. Does the compiler automatically link the two or do you need to explicitly link them? Im using gcc on a linux box. I didn't think you had too but i am trying to use a header file and it didn't work the way i expected it to - pretty sure because i didn't write it correctly. Also if you used a header file with function foo would foo's prototype be in the header file or in your source file? thanks for any replies in advance.
|
|
|
|
|
|
#2 |
|
Expert Programmer
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4
![]() |
Let me explain. Say, you have to files:
foo.c bar.c where foo is the file which needs functions from bar, so you write a bar.h file, with only the definitions, not the functions itself, and include it in foo.c. When you want to compile it, you use this command: GCC foo.c bar.c -o outputfile |
|
|
|
|
|
#3 |
|
Hobbyist Programmer
Join Date: Jun 2005
Location: here
Posts: 137
Rep Power: 0
![]() |
Header files generally contain function declarations (not definitions), structures, enums, and defined variables (i.e. #define ...)
They can also include other header files. C files contain the definitions of the functions contained in the header as well as other functions (possibly static) declared and defined within the file. When you compile with gcc, by default everything is linked for you, which is why you will sometimes get linker errors regarding unfound functions... You can stop before the link stage as well as at other points. man gcc will give you all the details.
__________________
"...and though our kids are blessed their parents let them shoulder all the blame." - The Quiet Things That No One Ever Knows [BrandNew] |
|
|
|
|
|
#4 |
|
Hobbyist Programmer
Join Date: May 2005
Location: ma
Posts: 130
Rep Power: 4
![]() |
ok just so everyone is on the same page what is the definition of declaration and definitions? The declaration is something like
int add(int num, int num2)? and the definition would be int add(int num, int num2){ int num; int num2; int sum; sum = num1 + num2; return sum; } ? |
|
|
|
|
|
#5 |
|
Hobbyist Programmer
Join Date: May 2005
Location: ma
Posts: 130
Rep Power: 4
![]() |
ok thinking about this has made me think about teh subject a little more. is a header and a library the same thing? Here is what i thought a .h file was for - i thought it was to keep similar functions together so that when your writing the main program it isn't cluttered with functions and you can just call the functions to get the work done. And that way you could use the .h for different projects that needed the same functions. However when i look at the .h files in /usr/include on my system a lot of them are a bunch of preprocessor stuff. so am i wrong?
|
|
|
|
|
|
#6 |
|
Hobbyist Programmer
Join Date: Aug 2005
Posts: 137
Rep Power: 4
![]() |
Function declaration is another term for prototyping. Function definition is the actual coded function so you are correct.
|
|
|
|
|
|
#7 | |
|
Expert Programmer
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4
![]() |
Quote:
|
|
|
|
|
|
|
#8 | |
|
Hobbyist Programmer
Join Date: May 2005
Location: ma
Posts: 130
Rep Power: 4
![]() |
Quote:
|
|
|
|
|
|
|
#9 | |
|
Hobbyist Programmer
Join Date: May 2005
Location: ma
Posts: 130
Rep Power: 4
![]() |
Quote:
"What is a header file? They contain prototypes and other compiler/pre-processor directives. Prototypes are basic abstract function definitions. More on these later..." from site: http://www.programmingforums.org/for...wreply&p=50212 isn't a prototype just int prototype(int num, char letter) so when you go to define prototype as prototype(char letter, char letter2) { function code } you'd get a compiler error because they don't match up? I think i read for C++ before that defining a function with same name but different arguments was a way of being able to have two functions with teh same name. Is that correct? Is that site linking a prototype with definition too much? Kind of hazy area i think. |
|
|
|
|
|
|
#10 |
|
Hobbyist Programmer
Join Date: May 2005
Location: ma
Posts: 130
Rep Power: 4
![]() |
http://www.google.com/search?hl=en&l...le&btnG=Search
i think the more i look into it the more i get confused. lol. Here is what i was going to do. For a first practice program i was going to write a calculator. I was going to have a file calc.h or similar that would have all the functions for multipling, addition, etc. something like int add(int num1, num2) { int num1, num2, sum; sum = num1 + num2; return sum; } so then i'd have the main program as calc.c and have something like case switches so when the user selected + i would just have to put add(input#, input#) to call the function in calc.h. So that would make the main program more concise and readable. Is that the correct use of header files? Last edited by linuxpimp20; Sep 6th, 2005 at 5:31 PM. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|