LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
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 11-28-2006, 04:09 AM   #1
BiThian
Member
 
Registered: Aug 2006
Location: Romania
Distribution: NetBSD 3.1
Posts: 118

Rep: Reputation: 15
warning: implicit declaration of function `snprintf'


Everytime I compile a program using snprintf I get that warning message. The headers included in my application are:
Code:
#include<sys/types.h>
#include<sys/stat.h>
#include<errno.h>
#include<stdio.h> /* header where snprintf is declared */
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<signal.h>
The CFLAGS are: "-Wall -pedantic -ansi". What's the problem?
Thanks!
 
Old 11-28-2006, 06:42 AM   #2
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
Quote:
Originally Posted by BiThian
Everytime I compile a program using snprintf I get that warning message. The headers included in my application are:
Code:
#include<sys/types.h>
#include<sys/stat.h>
#include<errno.h>
#include<stdio.h> /* header where snprintf is declared */
Do you have the package installed that contains the file /usr/include/stdio.h? (in Debian that package is called "libc6-dev").

Last edited by Hko; 11-28-2006 at 06:47 AM.
 
Old 11-28-2006, 06:57 AM   #3
BiThian
Member
 
Registered: Aug 2006
Location: Romania
Distribution: NetBSD 3.1
Posts: 118

Original Poster
Rep: Reputation: 15
I have installed glibc-2.3.6-i486-5.
LE: That warning message I get only when I use snprintf (printf and other stdio.h functions work just fine)

Last edited by BiThian; 11-28-2006 at 07:00 AM.
 
Old 11-28-2006, 07:25 AM   #4
demon_vox
Member
 
Registered: May 2006
Location: Argentina
Distribution: SuSE 10
Posts: 173

Rep: Reputation: 30
Hi,
maybe you are using snprintf wrongly. Perhaps you are not putting the arguments in the correct order, or you have put a non-matching type for one of those. So, the compiler could say "implicit declaration" since it does not match to the prototype it expects.

Just a wild thought
Cheers!
 
Old 11-28-2006, 07:51 AM   #5
BiThian
Member
 
Registered: Aug 2006
Location: Romania
Distribution: NetBSD 3.1
Posts: 118

Original Poster
Rep: Reputation: 15
Nope, I checked the man page and ... no problems with the syntax.
 
Old 11-28-2006, 09:56 AM   #6
demon_vox
Member
 
Registered: May 2006
Location: Argentina
Distribution: SuSE 10
Posts: 173

Rep: Reputation: 30
I did not mean the syntax, I mean the semantics. But it was just a guess, since I dont know what it says in the code

Cheers!
 
Old 11-28-2006, 11:12 AM   #7
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 62
post how you are using snprintf, and the declarations of / assignments to any variables you use.
 
Old 11-28-2006, 11:19 AM   #8
BiThian
Member
 
Registered: Aug 2006
Location: Romania
Distribution: NetBSD 3.1
Posts: 118

Original Poster
Rep: Reputation: 15
Here's the code:
Code:
#include<sys/types.h>
#include<sys/stat.h>
#include<errno.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<signal.h>
#define MAXLINE 4096
#define SYS_FAT 3
#define SYS_NOFAT 2
#define NOSYS_FAT 1
#define	FAT 1
#define	SYS 2
#define systemFatalError(functionCall) errorHandler(functionCall,SYS_FAT)
#define systemNoFatalError(functionCall) errorHandler(functionCall,SYS_NOFAT)
#define nosystemFatalError(functionCall) errorHandler(functionCall,NOSYS_FAT) 
static void errorHandler(const char* functionCall,int flag)
{
	int errorValue=errno;
	char buf[MAXLINE];
	if(flag&SYS)	snprintf(buf,MAXLINE,"%s: %s\n",functionCall,strerror(errorValue));
	else snprintf(buf,MAXLINE,"%s\n",functionCall);
	fputs(buf,stderr);
	if(flag&FAT)exit(EXIT_FAILURE);
	return;
}
 
Old 11-28-2006, 11:19 AM   #9
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 62
Does this program give the same error? (I used all your include statements)
Code:
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <stdio.h> /* header where snprintf is declared */
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>

int main(int argc, char** argv)
{
        char buf[13];
        char *wld = "WORLD";

        snprintf(buf, 12, "hello %s", wld);

        printf("%s\n", buf);

        return 0;
}
 
Old 11-28-2006, 11:24 AM   #10
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 62
Quote:
Originally Posted by BiThian
Here's the code:
Code:
...
Compiles OK for me. How are you building it - what compiler flags are you using?
 
Old 11-28-2006, 11:24 AM   #11
BiThian
Member
 
Registered: Aug 2006
Location: Romania
Distribution: NetBSD 3.1
Posts: 118

Original Poster
Rep: Reputation: 15
Quote:
Does this program give the same error? (I used all your include statements)
Yes, I get the same warning
LE: I compile it with "gcc -Wall -pedantic -ansi".

Last edited by BiThian; 11-28-2006 at 11:33 AM.
 
Old 11-28-2006, 11:46 AM   #12
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,011

Rep: Reputation: 110Reputation: 110
Code:
#if defined __USE_BSD || defined __USE_ISOC99 || defined __USE_UNIX98
__BEGIN_NAMESPACE_C99
/* Maximum chars of output to write in MAXLEN.  */
extern int snprintf (char *__restrict __s, size_t __maxlen,
                     __const char *__restrict __format, ...)
     __THROW __attribute__ ((__format__ (__printf__, 3, 4)));

extern int vsnprintf (char *__restrict __s, size_t __maxlen,
                      __const char *__restrict __format, _G_va_list __arg)
     __THROW __attribute__ ((__format__ (__printf__, 3, 0)));
__END_NAMESPACE_C99
#endif
From the header, snprintf is only available if you are compiling C99, BSD, or UNIX98 code. Since ANSI C (pedantic or not) isn't any of those, it doesn't declare snprintf.
 
1 members found this post helpful.
Old 11-28-2006, 11:51 AM   #13
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 62
what he said
 
Old 10-06-2011, 04:26 PM   #14
verwall
LQ Newbie
 
Registered: Oct 2011
Posts: 1

Rep: Reputation: Disabled
this is because when you use -ansi, you think C99. However, man gcc tells us that:

-ansi
In C mode, this is equivalent to -std=c89. In C++ mode, it is equivalent to -std=c++98.
 
  


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
warning: implicit declaration of function liguorir Linux - Software 5 10-22-2012 03:20 PM
Implicit declaration means alaios Programming 7 03-09-2009 06:20 AM
how to get rid of "warning : implicit declaration of function 'getpid' " ? diodio Linux - Newbie 6 02-01-2006 02:47 PM
warning: incompatible implicit declaration of built-in function ‘exit’ xzotech Programming 1 08-14-2005 05:43 AM
implicit declaration of function 'getdelim' MicahCarrick Programming 2 05-02-2005 11:03 AM


All times are GMT -5. The time now is 09:56 AM.

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