Warnings with gcc

T

thibault.langlois

Hello,
I get a warning when I compile:
#include <string.h>

int main (int argc, char ** argv)
{
char * s;
s = strdup("a string");
return 0;
}

with gcc (GCC) 3.3.5 (Debian 1:3.3.5-8ubuntu2) :

$ gcc -Wall -ansi warning.c
warning.c: In function `main':
warning.c:7: warning: implicit declaration of function `strdup'
warning.c:7: warning: assignment makes pointer from integer without a
cast

Does anybody knows how can I supress this warning ?

if I put a cast : s = (char *) strdup("a string");
I get another warning message:
warning.c:7: warning: implicit declaration of function `strdup'

I do not understand why this happens because strdup is declared in
string.h

Thanks,

Thibault Langlois
 
R

rayw

Hello,
I get a warning when I compile:
#include <string.h>

int main (int argc, char ** argv)
{
char * s;
s = strdup("a string");
return 0;
}

with gcc (GCC) 3.3.5 (Debian 1:3.3.5-8ubuntu2) :

$ gcc -Wall -ansi warning.c
warning.c: In function `main':
warning.c:7: warning: implicit declaration of function `strdup'
warning.c:7: warning: assignment makes pointer from integer without a
cast

Does anybody knows how can I supress this warning ?

if I put a cast : s = (char *) strdup("a string");
I get another warning message:
warning.c:7: warning: implicit declaration of function `strdup'

I do not understand why this happens because strdup is declared in
string.h

Well, it can't be can it - unless the compiler's wrong?

Have you confirmed that it's in [your] string.h?

Does your preprocessed output contain it?

Does it link?

rayw
 
R

Richard Bos

I get a warning when I compile:
#include <string.h>

int main (int argc, char ** argv)
{
char * s;
s = strdup("a string");
return 0;
}
warning.c:7: warning: implicit declaration of function `strdup'

I do not understand why this happens because strdup is declared in
string.h

strdup() is not ISO C, and should therefore _not_ be declared in
<string.h> in a conforming implementation. Looks like gcc with -ansi is
conforming in this respect (as in most, perhaps all, others).

Richard
 
R

Robert Gamble

Hello,
I get a warning when I compile:
#include <string.h>

int main (int argc, char ** argv)

this is better than int main () or void main (void) which we see too
often around here, but since you are not using either of these
variables, why not just int main (void)?
{
char * s;
s = strdup("a string");

strdup is not a Standard C function, it is a BSD extension. You
specified the -ansi option when you tried to compile this telling the
compiler you want to compile as a strictly conforming program. This
option defines the macro __STRICT_ANSI__ which will keep the prototype
for strdup from being visible from string.h. You can either remove the
-ansi switch, or place the following define before your include
directive:
#define _GNU_SOURCE
return 0;
}

with gcc (GCC) 3.3.5 (Debian 1:3.3.5-8ubuntu2) :

$ gcc -Wall -ansi warning.c
warning.c: In function `main':
warning.c:7: warning: implicit declaration of function `strdup'

The prototype for strdup is not in scope, hence the diagnostic, see
above.
warning.c:7: warning: assignment makes pointer from integer without a
cast

Since the prototype for strdup is not in scope, the compiler assumes it
returns an int which you are storing into a pointer without a cast,
this is required to produce a diagnostic.
Does anybody knows how can I supress this warning ?

The -w switch turns off all warnings but this justs hides the problem,
fix the issue instead.
if I put a cast : s = (char *) strdup("a string");
I get another warning message:
warning.c:7: warning: implicit declaration of function `strdup'

Since you cast the result of strdup, which the compiler thinks is an
int, to a char * a diagnostic is not required. You still have the
issue of strdup not being declared. If you are using a cast to supress
a compiler diagnostic, this is a good clue you are doing something
wrong.

Robert Gamble
 
B

Barry

Hello,
I get a warning when I compile:
#include <string.h>

int main (int argc, char ** argv)
{
char * s;
s = strdup("a string");
return 0;
}

with gcc (GCC) 3.3.5 (Debian 1:3.3.5-8ubuntu2) :

$ gcc -Wall -ansi warning.c
warning.c: In function `main':
warning.c:7: warning: implicit declaration of function `strdup'
warning.c:7: warning: assignment makes pointer from integer without a
cast

Does anybody knows how can I supress this warning ?

if I put a cast : s = (char *) strdup("a string");
I get another warning message:
warning.c:7: warning: implicit declaration of function `strdup'

I do not understand why this happens because strdup is declared in
string.h

Thanks,

Thibault Langlois

drop the "-ansi" strdup is a non-ANSI extension (at least according to gcc).

barry
 
S

Skarmander

drop the "-ansi" strdup is a non-ANSI extension (at least according to gcc).
That's not quite it. gcc would in fact be allowed to provide strdup()
even in ANSI mode, but it chooses not to, presumably to help people
catch conformance errors (which is -ansi's primary purpose).

(To be even more precise, it actually depends on the standard library
implementation used with gcc.)

S.
 
B

Barry

Skarmander said:
That's not quite it. gcc would in fact be allowed to provide strdup()
even in ANSI mode, but it chooses not to, presumably to help people
catch conformance errors (which is -ansi's primary purpose).

(To be even more precise, it actually depends on the standard library
implementation used with gcc.)

S.

Thanks for the correction. My post was based solely on the comments in the
gcc
header files.

barry
 
S

Skarmander

jacob said:
strdup is in string.h in my linux system with
gcc version 2.96 20000731 (Mandrake Linux 8.2 2.96-0.76mdk)
strdup() is *not necessarily* in string.h. It's a nonstandard function;
the standard doesn't define it. Implementations are allowed to define
additional functions beginning with "str" in <string.h>, however (and if
<string.h> is included, a program may not define such functions).

S.
 
P

pete

jacob said:
pete wrote:
strdup is in string.h in my linux system with
gcc version 2.96 20000731 (Mandrake Linux 8.2 2.96-0.76mdk)

Maybe there's a newsgroup or two,
listed in the above quoted URL,
where that's on topic.
 
F

Flash Gordon

Skarmander wrote:

strdup() is *not necessarily* in string.h. It's a nonstandard function;
the standard doesn't define it. Implementations are allowed to define
additional functions beginning with "str" in <string.h>, however (and if
<string.h> is included, a program may not define such functions).

We've had this discussion here before, and not too many weeks back IIRC.
programs are not allowed to declare such identifiers with external
linkage even if they *don't* include string.h
 
D

Dik T. Winter

> strdup is in string.h in my linux system with
> gcc version 2.96 20000731 (Mandrake Linux 8.2 2.96-0.76mdk)

Unconditional? strdup is also in string.h here (Solaris), as follows:

#if defined(__EXTENSIONS__) || (__STDC__ == 0 && \
!defined(_POSIX_C_SOURCE) && !defined(_XOPEN_SOURCE)) || \
defined(_XPG4_2)
extern char *strdup(const char *);
#endif
 
I

Igmar Palsenberg

Hello,
I get a warning when I compile:
#include <string.h>

int main (int argc, char ** argv)
{
char * s;
s = strdup("a string");
return 0;
}

You should. strdup() isn't an ansi compliant function.

Does anybody knows how can I supress this warning ?

Don't use strdup()
I do not understand why this happens because strdup is declared in
string.h

Because you told gcc when using the -ansi directive.



Igmar
 
J

Jordan Abel

Skarmander wrote:



We've had this discussion here before, and not too many weeks back IIRC.
programs are not allowed to declare such identifiers with external
linkage even if they *don't* include string.h

What about with static linkage?

[incidentally, stdlib.h as well as string.h for the 'str' prefix]
 
S

Skarmander

Flash said:
Skarmander wrote:




We've had this discussion here before, and not too many weeks back IIRC.
programs are not allowed to declare such identifiers with external
linkage even if they *don't* include string.h

That's quite possible. I didn't mean to imply otherwise. They are
unambiguously not allowed to declare such functions regardless of
linkage if <string.h> *is* included, and that is all I said.

S.
 
T

thibault.langlois

Thank you (and all other posters) for your answer.
I found that using -sdt=gnu99 instead of -ansi is enough to supress the
warning.

I am a bit confused by ansi/iso features and someone may be kind engouh
to explain me or give some pointers where I can find the information.
1. The ansi/iso standards are about the language itself or about the
what K&R call the "standard library" ? or both ?
2. My source of info is the K&R book, the edition with the "ansi C" on
the cover. In the appendix the authors write about the "Standard
Library ... defined by the ANSI standard". The say also : "The standard
liabrary is not part of the C language proper but an environment that
supports the standard C and will provide the function declarations
[...] of this library."
Among the standard headers there is the string.h that should contain
strdup().
That is why I was expecting that -ansi would not cause such warnings.
Is it a gcc-specific feature/bug ?

Thibault Langlois
 
S

Skarmander

Thank you (and all other posters) for your answer.
I found that using -sdt=gnu99 instead of -ansi is enough to supress the
warning.
This does not "suppress" the warning, actually, because with -std=gnu99,
I am a bit confused by ansi/iso features and someone may be kind engouh
to explain me or give some pointers where I can find the information.
1. The ansi/iso standards are about the language itself or about the
what K&R call the "standard library" ? or both ?
Both.

2. My source of info is the K&R book, the edition with the "ansi C" on
the cover. In the appendix the authors write about the "Standard
Library ... defined by the ANSI standard". The say also : "The standard
liabrary is not part of the C language proper but an environment that
supports the standard C and will provide the function declarations
[...] of this library."

And that's right. It's not part of the language proper, but the standard
covers conforming implementations of C environments. That includes the
library.
Among the standard headers there is the string.h that should contain
strdup().

No. If K&R says this, it's wrong. But read carefully if that's what's
really said.
That is why I was expecting that -ansi would not cause such warnings.
Is it a gcc-specific feature/bug ?
Not really. strdup() doesn't have to exist, period. If your
implementation provides it, that's nice. Relying on its existence makes
your program unportable to platforms that don't have it.

strdup() is actually defined by the POSIX standard and it is available
on many platforms, it's just not part of ANSI C.

S.
 
F

Flash Gordon

Skarmander said:
That's quite possible. I didn't mean to imply otherwise. They are
unambiguously not allowed to declare such functions regardless of
linkage if <string.h> *is* included, and that is all I said.

OK, it's just that the way you worded it someone might think that if
they did not include string.h they could declare their own external
objects with such names.
 
F

Flash Gordon

Jordan said:
What about with static linkage?

The relevant part of section 7.1.3 from N1124 is:
| — All identifiers with external linkage in any of the following
| subclauses (including the future library directions) are always
| reserved for use as identifiers with external linkage.157)
| — Each identifier with file scope listed in any of the following
| subclauses (including the future library directions) is reserved for
| use as a macro name and as an identifier with file scope in the same
| name space if any of its associated headers is included.

So I would say that you can have:
static int strcpy(void) {return 0};
if you want and have not included string.h or stdlib.h
[incidentally, stdlib.h as well as string.h for the 'str' prefix]

Yes, you are correct. str[a-z] are reserved if you include stdlib.h
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,954
Messages
2,570,116
Members
46,704
Latest member
BernadineF

Latest Threads

Top