Scheduled Maintenance: We are aware of an issue with Google, AOL, and Yahoo services as email providers which are blocking new registrations. We are trying to fix the issue and we have several internal and external support tickets in process to resolve the issue. Please see: viewtopic.php?t=158230

 

 

 

ansi c error

Programming languages, Coding, Executables, Package Creation, and Scripting.
Post Reply
Message
Author
tomt
Posts: 65
Joined: 2005-09-09 20:19

ansi c error

#1 Post by tomt »

I am beginning to learn C and have run into a problem compiling with
the following for 'cbrt(number)':

'gcc -pedantic -ansi -Wall -m64 -lm program.c -o program'

Headers are '<stdio.h>' and '<math.h>'

The error is:

'warning: implicit declaration of function ‘cbrt’
[-Wimplicit-function-declaration]'

When I remove the '-ansi' switch the function works. How do I get it
to work with -ansi?

There is no similar problem with 'sqrt(number)'.

Regards

User avatar
alansmithee
Posts: 41
Joined: 2013-02-02 08:02

Re: ansi c error

#2 Post by alansmithee »

The 'cbrt' function is not part of the ANSI C Library.
'alansmithee' is the user formerly known as 'saulgoode'.

tomt
Posts: 65
Joined: 2005-09-09 20:19

Re: ansi c error

#3 Post by tomt »

Thanks for that info. Not critical since this is just an exercise.

Regards, Tom

tomazzi
Posts: 730
Joined: 2013-08-02 21:33

Re: ansi c error

#4 Post by tomazzi »

Actually, this is a critical error. Your code will not work properly.

This warning means that function body is not available and a call to this fn will do nothing (*)

If You would read the f..riendly manual, before posting, then You could quickly discover that cbrt() is part of c99. It is also available as an extension to c89, but in such case You need to define a proper keyword first.

Regards.

ps.
Another thing is, that the standard is broken regarding this case: the compiler shoud generate an error, not a warning (IMO).

(to clarify:)
* In the worst possible case, the program can compile just fine, but it will do something completely different, f.e. it can call completely different function with the same name.
Last edited by tomazzi on 2015-03-02 23:35, edited 2 times in total.
Odi profanum vulgus

tomt
Posts: 65
Joined: 2005-09-09 20:19

Re: ansi c error

#5 Post by tomt »

Thanks for the comments.

What I meant was that the function was not citical to my needs since
this is merely an exercise (or I could compile without the ansi switch).

If you're new to the subject it is not always obvious what the error
could be - or what c99 or c89 could be.

'man gcc' yields 'No manual entry for gcc'. :(

Regards.

tomazzi
Posts: 730
Joined: 2013-08-02 21:33

Re: ansi c error

#6 Post by tomazzi »

You should install gcc-doc package to have the manpage.

However, I was thinking of "man cbrt" rather than man gcc ;)

In any case, all manpages are available online...

edit:
To be more clear:
In case of any function, You should check the section named:
"Feature Test Macro Requirements"

This section describes what conditions are needed for the function to work, or even to be visible in the included header. In case of cbrt() You can either switch to c99 by using gcc option -std=c99 OR
You can define one of the keywords (with proper value) listed in "Feature Test Macro Requirements" section. The keyword (preprocessor symbol) must be defined before the header is included, f.e.
#define _ISOC99_SOURCE
#include <math.h>
/edit

edit2:
I think than in this case it'll be "safer" for You to just switch to c99 - as the "backports way" can sometimes lead to really strange situations... /edit.

Regards.
Odi profanum vulgus

tomt
Posts: 65
Joined: 2005-09-09 20:19

Re: ansi c error

#7 Post by tomt »

Thanks again for your comments. Nothing like being thrown into the
deep end to learn to swim (or drown).

I have been able to get the code working using switches -ansi _and_
-std=c99 although why this is superior to using default GNU extensions
is something I will have to work out one of these days.

$ sudo aptitude install gcc-doc
No candidate version found for gcc-doc

Looks like it is non free
http://forums.debian.net/viewtopic.php?t=104087
I shall download it before long.

I did find man page for cbrt, though. 'Feature Test Macro
Requirements' is currently meaningless to me although I can see a
reference to -std=c99

I have been looking through Gough's Introduction to GCC and think I
should go through that before embarking on more coding.

Thanks to both respondents for setting me right. You'll probably see
me here again.

Regards

tomazzi
Posts: 730
Joined: 2013-08-02 21:33

Re: ansi c error

#8 Post by tomazzi »

tomt wrote: I have been able to get the code working using switches -ansi _and_
-std=c99 although why this is superior to using default GNU extensions
is something I will have to work out one of these days.
Perhaps one more thing needs clarification here:
C99 is an ANSI C, so in other words, by using "-ansi -std=c99", You just saying to the compiler:
"I want ansi c99, and not that old ansi c89, which does not include cbrt() function" ;)

However, if You need only this particular function while the whole rest of the program must generally meet ansi C89 std, then You can use "a hack", which allows to reveal the existence of this fn for a single c-unit file, by defining _ISOC99_SOURCE in this single file, before including the math.h.

Why is this a hack? Well, first, if You are writting a code which is required to meet some particular standard then You should follow its rules - otherwise it makes no sense to use -ansi switch at all.

Besides that, what is obvious, GNU extensions are not usable with non-GNU systems.

And finally: definitions of some structures, values of some constants and even implementation of some functions may depend on the combination of keywords defined to describe the system environment - it is possible to define an environment which is incompatible with the underlying operating system - really strange things may happen in such case... ;)

So, if You want to use default GNU extensions, don't use ansi mode - and vice versa - those are 2 opposite directions of software development.

Regards.
Odi profanum vulgus

tomt
Posts: 65
Joined: 2005-09-09 20:19

Re: ansi c error

#9 Post by tomt »

I should have prefaced my posting by stating I was learning C out of
pure interest. I don't have any expectation my code will be read by
anyone else, much less that any program I write will be useful to
anyone. Still I would rather write code to an established standard
than otherwise. ANSI C it is.

Just for practice I am building a little calculator that does
conversions such as from links to meters (a lot of local town plans
show length in links) and rai to acres. cubrt(x) is a function I'll
probably never use in my lifetime, but that is beside the point. I
have learnt more from cbrt(x) failing than with sqrt(x) working at
first attempt.

I seem to be learning more about compiler idiosyncrasies than C coding
but I'd have to learn the former sooner or later so this is as good
time as any.

I am grateful for your help in explaining all this to me in fairly
basic terms.

Regards

Post Reply