LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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 07-16-2005, 02:03 AM   #1
alaios
Senior Member
 
Registered: Jan 2003
Location: Aachen
Distribution: Opensuse 11.2 (nice and steady)
Posts: 2,203

Rep: Reputation: 45
why this mesasge exists?


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?
 
Old 07-16-2005, 04:05 AM   #2
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
The functions code should not be in a header file

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)
 
Old 07-16-2005, 04:12 AM   #3
vharishankar
Senior Member
 
Registered: Dec 2003
Distribution: Debian
Posts: 3,178
Blog Entries: 4

Rep: Reputation: 138Reputation: 138
Yes. In C++ all function prototypes must be declared before they are called. That is the reason why you get a warning.
 
Old 07-16-2005, 04:54 AM   #4
alaios
Senior Member
 
Registered: Jan 2003
Location: Aachen
Distribution: Opensuse 11.2 (nice and steady)
Posts: 2,203

Original Poster
Rep: Reputation: 45
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....
 
Old 07-16-2005, 05:20 AM   #5
femistofel
LQ Newbie
 
Registered: Jul 2005
Distribution: fedora
Posts: 17

Rep: Reputation: 4
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.
 
Old 07-16-2005, 05:24 AM   #6
vharishankar
Senior Member
 
Registered: Dec 2003
Distribution: Debian
Posts: 3,178
Blog Entries: 4

Rep: Reputation: 138Reputation: 138
In any case it's a good practise to write prototype declarations.

If you haven't been doing so up to now, I suggest you start doing it now.
 
Old 07-16-2005, 10:36 AM   #7
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
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.

Last edited by ta0kira; 07-16-2005 at 10:38 AM.
 
Old 07-16-2005, 11:00 AM   #8
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
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 ...
 
Old 07-16-2005, 03:57 PM   #9
femistofel
LQ Newbie
 
Registered: Jul 2005
Distribution: fedora
Posts: 17

Rep: Reputation: 4
To taOkira

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:

Code:
#ifndef _UNIQUE_SOMETHING_
#define _UNIQUE_SOMETHING_

...

#endif
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.
 
Old 07-16-2005, 04:16 PM   #10
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
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.

Last edited by deiussum; 07-16-2005 at 04:21 PM.
 
Old 07-16-2005, 04:39 PM   #11
femistofel
LQ Newbie
 
Registered: Jul 2005
Distribution: fedora
Posts: 17

Rep: Reputation: 4
Quote:
Originally posted by deiussum

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.
 
  


Reply



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
Why do dentist exists? irfanhab General 24 07-13-2005 06:39 PM
SIOCADDRT: File exists SIOCCADDRT: File Exists Failed to bring up eth0. opsraja Linux - Networking 0 01-10-2005 08:29 AM
File already exists mickstaff Programming 3 05-13-2004 03:34 AM
checking if a file exists veilig Programming 6 02-01-2004 04:16 AM
file exists? raven Programming 7 10-26-2003 07:44 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:13 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration