ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hi
I have a header file thart contains two function
function A{
}
and function b{
}
the function A calls the function B
I have noticed that the gcc compiler returns a warninkg
monitor.h:12: warning: implicit declaration of function `snmp_fetch_oid_value'
I have also noticed that when i have declared the funtion at the start of the file in one line.. the compiler has stopped printing the warning message... Do u have any suggestion abou that?
And if it was in a .c file, you have to declare the prototype
of function B if you want to call it in function A
(you may want to put the prototypes in the header file)
I write c code and i use gcc for compiling it.. I cant understand why do u say that the function code must not be in the header file.... I have seen many .h files that contain fulll code for functions....
depending on what is your intention you may or may not wanna have a function code in the header file: compiler doesn't care. useful inline functions, for example, may go to the header file. you may also want to do some onorthodox (if you're onorthodox greek ;-)) things and use functions definitions in the header (which is then not really a header but rather an include-file, although it can still have .h extension). as long as you understand why you want a function body in include file and what does that imply - there's no problem with that.
as for the warning: when c compiler sees a function call and it doesn't yet know such a function (it wasn't declared earlier in a program) the compiler makes some assumptions about it. It always assumes that function returns int, for example. It also assumes that function expects the types of arguments you have supplied in this particular call to it.
The reason I guess is that people who designed it were lazy hackers who hated to declare functions which mostly returned int anyway ;-) K&R, if you read this - no offence, i'm just kidding.
when later compiler encounters the function declaration or definition and it doesn't match its assumption - it will complain.
In your case the assumption seems to match, so it only warns you that it had to make that assumption. When you add a declaration of the second function before its use it doesn't have to assume anything and is happy.
Last edited by femistofel; 07-16-2005 at 05:23 AM.
Header files are a lot more clear if you don't put the function definitions there. The user of the file isn't generally concerned with what's in the function unless something is wrong; they just need to know the prototype so they know how to use it. When I write my libs I always put prototypes in the header and definitions in the source (global vars also belong here), but when I have an implementation of the libs that I intend to be compiled into an application I just put everything in source files.
The main purpose of having a header file with only prototypes and sources with only definitions is so if you have multiple targets that use the same lib, you can include the header in each them, compile the source separately, then link the compiled source into whatever needs it. This keeps you from having to recompile the entire source for each module it's included in.
ta0kira
PS Templates in C++ are an exception to this; it's a huge pain to separate the header from the source in this case (but I do it anyway), and you really need to recompile it in each instance to make sure each different template is instantiated and compiled.
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789
Rep:
Quote:
Header files are a lot more clear if you don't put the function definitions there.
Not to mention if you put a function definition in a header file, one side effect is preventing this header file from being included more than once in a program, defeating most of the interest of header files ...
If you want your inline functions to get inlined by the compiler you've got to put the definitions into the header, otherwise the compiler won't know what to inline.
To jllagre
whether you put function definitions into your header files or not, it's a good idea to prevent them from being included twice. most header files use the construction:
to prevent double including, or rather file may be included more then once, but it'll be stripped by preprocessor second, third etc time.
As for including the same file when compiling different mdules which are then linked together - you can either ensure the function names change using macros, or you can declare the included functions static.
I think Jilliagre was talking about the problem where you have two .c files that both include a .h that has the full function definition. In this case, the linker will give you errors because BOTH of the intermediate .o files will contain a definition for that given function.
example:
Code:
$ cat >blah.h <<EOF
> #ifndef _blah_h_
> #define _blah_h_
> int f()
> {
> }
> #endif
> EOF
$ cat >blah1.c <<EOF
> #include "blah.h"
> int main()
> {
> f();
> }
> EOF
$ cat >blah2.c <<EOF
> #include "blah.h"
> int f2()
> {
> f();
> }
> EOF
$ gcc -o blah blah1.c blah2.c
cc3tZodC.o(.text+0x0):blah2.c: multiple definition of `_f'
As6QvL.o(.text+0x0):blah1.c: first defined here
collect2: ld returned 1 exit status
Declaring it static will rid you of the error, but then object code for that function is essentially created for every module it is used in. If you have a project with 100s of source code files, this can be a bad thing.
So the moral is, unless you have a really good reason and understand all the implications, don't put full function definitions in the header.
Declaring it static will rid you of the error, but then object code for that function is essentially created for every module it is used in. If you have a project with 100s of source code files, this can be a bad thing.
So the moral is, unless you have a really good reason and understand all the implications, don't put full function definitions in the header. [/B]
Sure thing, but as I said before "as long as you understand why you want a function body in include file and what does that imply - there's no problem with that." ;-)
I won't be including the same static function in 100s of modules, but I can imagine some magick with preprocessor making the functions not really the same. something like:
Code:
in foo.h file:
...
static void foo()
{
#if FOO
do it like this
#else
do it like that
#endif
}
...
in foo.c file
#define FOO
#include "foo.h"
...
in bar.c file
#undef FOO
#include "foo.h"
This looks stupid, but may have its uses, e.g. emulating partial evaluation with cpp.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.