473,382 Members | 1,362 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

lgamma function: ANSI C or not?

I'm having a .c source file which at the top contains the line

#include <math.h>

In that source file, i declare a function dt which in its body uses
the lgamma function. `man lgamma' on my linux system tells me
that i have to include math.h so it seems like I'm doing the
right thing in order to be able to use the lgamma function.

When I compile the .c file with

$ gcc -ansi -c rand.c

or

$ gcc -Wall -c rand.c

then I get no errors or warnings. When I compile it with

$ gcc -Wall -ansi -c rand.c

I get the warning:

rand.c: In function `dt':
rand.c:544: warning: implicit declaration of function `lgamma'

which seems strange to me. Why am I getting this warning and how do I
resolve this warning? I assume it has something to do with ANSI C or not,
but I'm confused...

Thanks,
Bart

PS: the version of gcc I'm using is gcc (GCC) 3.3.5 (Debian 1:3.3.5-13).
Upgrading to a newer version is no option because I do not have admin rights
to install or upgrade packages on my system.

--
"Share what you know. Learn what you don't."
May 10 '06 #1
15 2920

Bart Vandewoestyne wrote:
I'm having a .c source file which at the top contains the line

#include <math.h>

In that source file, i declare a function dt which in its body uses
the lgamma function. `man lgamma' on my linux system tells me
that i have to include math.h so it seems like I'm doing the
right thing in order to be able to use the lgamma function.

When I compile the .c file with

$ gcc -ansi -c rand.c

or

$ gcc -Wall -c rand.c

then I get no errors or warnings. When I compile it with

$ gcc -Wall -ansi -c rand.c

I get the warning:

rand.c: In function `dt':
rand.c:544: warning: implicit declaration of function `lgamma'

which seems strange to me. Why am I getting this warning and how do I
resolve this warning? I assume it has something to do with ANSI C or not,
but I'm confused...

Thanks,
Bart

PS: the version of gcc I'm using is gcc (GCC) 3.3.5 (Debian 1:3.3.5-13).
Upgrading to a newer version is no option because I do not have admin rights
to install or upgrade packages on my system.


Function `lgamma()` is only available in C99. GCC `-ansi` assumes C89
(same as `-std=c89`). Therein lies your problem. Have you tried
`-std=c99` instead?

May 10 '06 #2
"Bart Vandewoestyne" <My********************@telenet.be> wrote in message
news:11***************@seven.kulnet.kuleuven.ac.be ...
I'm having a .c source file which at the top contains the line

#include <math.h>

In that source file, i declare a function dt which in its body uses
the lgamma function. `man lgamma' on my linux system tells me
that i have to include math.h so it seems like I'm doing the
right thing in order to be able to use the lgamma function.

When I compile the .c file with

$ gcc -ansi -c rand.c

or

$ gcc -Wall -c rand.c

then I get no errors or warnings. When I compile it with

$ gcc -Wall -ansi -c rand.c

I get the warning:

rand.c: In function `dt':
rand.c:544: warning: implicit declaration of function `lgamma'

which seems strange to me. Why am I getting this warning and how do I
resolve this warning? I assume it has something to do with ANSI C or not,
but I'm confused...


lgamma is required by C99 and the Single Unix Specification
(effectively Posix). It is found in some older math libraries,
but not all.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
May 10 '06 #3
On 2006-05-09, Vladimir Oka <no****@btopenworld.com> wrote:

Function `lgamma()` is only available in C99. GCC `-ansi` assumes C89
(same as `-std=c89`). Therein lies your problem. Have you tried
`-std=c99` instead?


Indeed: this doesn't generate warnings:

$ gcc -Wall -std=c99 -c rand.c

This does:

$ gcc -Wall -std=c89 -c rand.c
rand.c: In function `dt':
rand.c:545: warning: implicit declaration of function `lgamma'
So my options are:

* implement lgamma myself if i want to be c89 compatible
* don't try to be c89 compatible and compile with -std=c99

Thanks!
Bart

--
"Share what you know. Learn what you don't."
May 10 '06 #4
Bart Vandewoestyne wrote:
I'm having a .c source file which at the top contains the line

#include <math.h>

In that source file, i declare a function dt which in its body uses
the lgamma function. `man lgamma' on my linux system tells me
that i have to include math.h so it seems like I'm doing the
right thing in order to be able to use the lgamma function.
Yes you are.
When I compile the .c file with

$ gcc -ansi -c rand.c

or

$ gcc -Wall -c rand.c

then I get no errors or warnings. When I compile it with

$ gcc -Wall -ansi -c rand.c

I get the warning:

rand.c: In function `dt':
rand.c:544: warning: implicit declaration of function `lgamma'

which seems strange to me. Why am I getting this warning and how do I
resolve this warning? I assume it has something to do with ANSI C or not,
but I'm confused...


Yes and no. It was not part of the original ANSI standard but it is part
of the latest C standard known colloquially as C99. See
http://clc-wiki.net/wiki/c_standard for more about the various versions
of the C standard.

<OT>
With gcc you could try using -std=c99 instead of -ansi, however you
should be aware that gcc does not fully implement C99.

My man page for lgamma included
| CONFORMING TO
| C99, SVID 3, BSD 4.3

which tells you that it is C99 and which other standard provided it as
an extension to C.
</OT>

If you need further help on getting gcc to recognise the lgamma function
gnu.gcc.help could be useful.
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro:
http://clc-wiki.net/wiki/Intro_to_clc

Inviato da X-Privat.Org - Registrazione gratuita http://www.x-privat.org/join.php
May 10 '06 #5
Bart Vandewoestyne wrote:
On 2006-05-09, Vladimir Oka <no****@btopenworld.com> wrote:
Function `lgamma()` is only available in C99. GCC `-ansi` assumes C89
(same as `-std=c89`). Therein lies your problem. Have you tried
`-std=c99` instead?


Indeed: this doesn't generate warnings:

$ gcc -Wall -std=c99 -c rand.c

This does:

$ gcc -Wall -std=c89 -c rand.c
rand.c: In function `dt':
rand.c:545: warning: implicit declaration of function `lgamma'
So my options are:

* implement lgamma myself if i want to be c89 compatible
* don't try to be c89 compatible and compile with -std=c99

or, you could edit <math.h> and remove the c99 guard on math functions
you consider eligible for c89 compatibility.
May 10 '06 #6
Bart Vandewoestyne wrote:
On 2006-05-09, Vladimir Oka <no****@btopenworld.com> wrote:
Function `lgamma()` is only available in C99. GCC `-ansi` assumes C89
(same as `-std=c89`). Therein lies your problem. Have you tried
`-std=c99` instead?


Indeed: this doesn't generate warnings:

$ gcc -Wall -std=c99 -c rand.c

This does:

$ gcc -Wall -std=c89 -c rand.c
rand.c: In function `dt':
rand.c:545: warning: implicit declaration of function `lgamma'
So my options are:

* implement lgamma myself if i want to be c89 compatible
* don't try to be c89 compatible and compile with -std=c99


If you chose to implement it yourself then I would strongly suggest
using a different name. That way you avoid confusion.
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro:
http://clc-wiki.net/wiki/Intro_to_clc
May 10 '06 #7
Ico
Bart Vandewoestyne <My********************@telenet.be> wrote:
I'm having a .c source file which at the top contains the line

#include <math.h>

In that source file, i declare a function dt which in its body uses
the lgamma function. `man lgamma' on my linux system tells me
that i have to include math.h so it seems like I'm doing the
right thing in order to be able to use the lgamma function.

When I compile the .c file with

$ gcc -ansi -c rand.c

or

$ gcc -Wall -c rand.c

then I get no errors or warnings. When I compile it with

$ gcc -Wall -ansi -c rand.c

I get the warning:

rand.c: In function `dt':
rand.c:544: warning: implicit declaration of function `lgamma'

which seems strange to me. Why am I getting this warning and how do I
resolve this warning? I assume it has something to do with ANSI C or not,
but I'm confused...

The manpages also have information on conformaing standards: look a bit
further down in the same manpage :

CONFORMING TO
C99, SVID 3, 4.3BSD

As you can see, ANSI is not in the list
--
:wq
^X^Cy^K^X^C^C^C^C
May 10 '06 #8
Tim Prince wrote:
Bart Vandewoestyne wrote:
On 2006-05-09, Vladimir Oka <no****@btopenworld.com> wrote:
Function `lgamma()` is only available in C99. GCC `-ansi` assumes C89
(same as `-std=c89`). Therein lies your problem. Have you tried
`-std=c99` instead?


Indeed: this doesn't generate warnings:

$ gcc -Wall -std=c99 -c rand.c

This does:

$ gcc -Wall -std=c89 -c rand.c
rand.c: In function `dt':
rand.c:545: warning: implicit declaration of function `lgamma'
So my options are:

* implement lgamma myself if i want to be c89 compatible
* don't try to be c89 compatible and compile with -std=c99

or, you could edit <math.h> and remove the c99 guard on math functions
you consider eligible for c89 compatibility.


This is probably a bad idea for several reasons. It could break valid
C89 code (e.g. code that defines an lgamma function with a different
prototype) which you might not hit until months or years later, the C99
stuff may depend on compiler support which is only enabled when C99
support is enabled, it may depend on things in other headers that are
only enabled when C99 is enabled etc. Also the OP does not have admin
writes so probably can't do this anyway.
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro:
http://clc-wiki.net/wiki/Intro_to_clc
May 10 '06 #9
"Flash Gordon" <sp**@flash-gordon.me.uk> wrote in message
news:ko************@news.flash-gordon.me.uk...
Tim Prince wrote:
Bart Vandewoestyne wrote:
On 2006-05-09, Vladimir Oka <no****@btopenworld.com> wrote:
Function `lgamma()` is only available in C99. GCC `-ansi` assumes C89
(same as `-std=c89`). Therein lies your problem. Have you tried
`-std=c99` instead?

Indeed: this doesn't generate warnings:

$ gcc -Wall -std=c99 -c rand.c

This does:

$ gcc -Wall -std=c89 -c rand.c
rand.c: In function `dt':
rand.c:545: warning: implicit declaration of function `lgamma'
So my options are:

* implement lgamma myself if i want to be c89 compatible
* don't try to be c89 compatible and compile with -std=c99

or, you could edit <math.h> and remove the c99 guard on math functions
you consider eligible for c89 compatibility.


This is probably a bad idea for several reasons. It could break valid C89
code (e.g. code that defines an lgamma function with a different
prototype) which you might not hit until months or years later, the C99
stuff may depend on compiler support which is only enabled when C99
support is enabled, it may depend on things in other headers that are only
enabled when C99 is enabled etc. Also the OP does not have admin writes so
probably can't do this anyway.


Most of those reasons are pretty esoteric, and not likely to matter
in real life. Nevertheless, I agree that it's a bad idea to edit
system headers. Besides, chances are all you have to do is put:

double (lgamma)(double);

at the top of the source file that calls the function. (The parens are
to protect the declaration against a macro override that may one day
come along.)

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
May 10 '06 #10
Ico wrote:
Bart Vandewoestyne <My********************@telenet.be> wrote:
I'm having a .c source file which at the top contains the line

#include <math.h>

In that source file, i declare a function dt which in its body uses
the lgamma function. `man lgamma' on my linux system tells me
that i have to include math.h so it seems like I'm doing the
right thing in order to be able to use the lgamma function.

<snip>
The manpages also have information on conformaing standards: look a bit
further down in the same manpage :

CONFORMING TO
C99, SVID 3, 4.3BSD

As you can see, ANSI is not in the list


ANSI adopted C99 just as earlier ISO adopted C89. So it is on the list.
It's just that gcc does not support C99 when the -ansi switch is used.
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro:
http://clc-wiki.net/wiki/Intro_to_clc
May 10 '06 #11
"P.J. Plauger" <pj*@dinkumware.com> wrote in message
news:R4******************************@giganews.com ...
"Flash Gordon" <sp**@flash-gordon.me.uk> wrote in message
news:ko************@news.flash-gordon.me.uk...
Tim Prince wrote:
or, you could edit <math.h> and remove the c99 guard on math
functions you consider eligible for c89 compatibility.


This is probably a bad idea for several reasons. It could break valid
C89 code (e.g. code that defines an lgamma function with a different
prototype) which you might not hit until months or years later, the
C99 stuff may depend on compiler support which is only enabled
when C99 support is enabled, it may depend on things in other
headers that are only enabled when C99 is enabled etc. Also the
OP does not have admin writes so probably can't do this anyway.


Most of those reasons are pretty esoteric, and not likely to matter
in real life. Nevertheless, I agree that it's a bad idea to edit
system headers. Besides, chances are all you have to do is put:

double (lgamma)(double);

at the top of the source file that calls the function. (The parens are
to protect the declaration against a macro override that may one day
come along.)


That will most likely work, yes, but one can envision a system that uses
different libraries depending on whether C89 or C99 mode is chosen.
Unlikely, sure, but comp.lang.c must maintain its reputation for pedantry.

Two "correct" solutions are to either require C99 or to create a macro that
calls his own implementation in non-C99 environments but calls the provided
(lgamma)() on a C99 system.

S

--
Stephen Sprunk "Stupid people surround themselves with smart
CCIE #3723 people. Smart people surround themselves with
K5SSS smart people who disagree with them." --Aaron Sorkin
*** Posted via a free Usenet account from http://www.teranews.com ***
May 10 '06 #12
P.J. Plauger wrote:
"Flash Gordon" <sp**@flash-gordon.me.uk> wrote in message
news:ko************@news.flash-gordon.me.uk...

Tim Prince wrote:
Bart Vandewoestyne wrote:

On 2006-05-09, Vladimir Oka <no****@btopenworld.com> wrote:

>Function `lgamma()` is only available in C99. GCC `-ansi` assumes C89
>(same as `-std=c89`). Therein lies your problem. Have you tried
>`-std=c99` instead?

Indeed: this doesn't generate warnings:

$ gcc -Wall -std=c99 -c rand.c

This does:

$ gcc -Wall -std=c89 -c rand.c
rand.c: In function `dt':
rand.c:545: warning: implicit declaration of function `lgamma'
So my options are:

* implement lgamma myself if i want to be c89 compatible
* don't try to be c89 compatible and compile with -std=c99
or, you could edit <math.h> and remove the c99 guard on math functions
you consider eligible for c89 compatibility.


This is probably a bad idea for several reasons. It could break valid C89
code (e.g. code that defines an lgamma function with a different
prototype) which you might not hit until months or years later, the C99
stuff may depend on compiler support which is only enabled when C99
support is enabled, it may depend on things in other headers that are only
enabled when C99 is enabled etc. Also the OP does not have admin writes so
probably can't do this anyway.

Most of those reasons are pretty esoteric, and not likely to matter
in real life. Nevertheless, I agree that it's a bad idea to edit
system headers. Besides, chances are all you have to do is put:

double (lgamma)(double);

at the top of the source file that calls the function. (The parens are
to protect the declaration against a macro override that may one day
come along.)

Isn't that rather a dangerous thing to recommend?

I can see two potential problems, the function isn't in the library, so
the application fails to link, or worse, the function exists with a
different prototype resulting in bizarre run time errors.

I'd have thought the OP should compile with the correct options to bring
the function in, there may be other types or definitions required in
that mode.

--
Ian Collins.
May 10 '06 #13
"Stephen Sprunk" <st*****@sprunk.org> wrote in message
news:44***********************@free.teranews.com.. .
"P.J. Plauger" <pj*@dinkumware.com> wrote in message
news:R4******************************@giganews.com ...
"Flash Gordon" <sp**@flash-gordon.me.uk> wrote in message
news:ko************@news.flash-gordon.me.uk...
Tim Prince wrote:
or, you could edit <math.h> and remove the c99 guard on math
functions you consider eligible for c89 compatibility.

This is probably a bad idea for several reasons. It could break valid
C89 code (e.g. code that defines an lgamma function with a different
prototype) which you might not hit until months or years later, the
C99 stuff may depend on compiler support which is only enabled
when C99 support is enabled, it may depend on things in other
headers that are only enabled when C99 is enabled etc. Also the
OP does not have admin writes so probably can't do this anyway.


Most of those reasons are pretty esoteric, and not likely to matter
in real life. Nevertheless, I agree that it's a bad idea to edit
system headers. Besides, chances are all you have to do is put:

double (lgamma)(double);

at the top of the source file that calls the function. (The parens are
to protect the declaration against a macro override that may one day
come along.)


That will most likely work, yes, but one can envision a system that uses
different libraries depending on whether C89 or C99 mode is chosen.
Unlikely, sure, but comp.lang.c must maintain its reputation for pedantry.

Two "correct" solutions are to either require C99 or to create a macro
that calls his own implementation in non-C99 environments but calls the
provided (lgamma)() on a C99 system.


Okay, if you're going to be pedantic, how can C99 dictate what happens in
non-C99 environments?

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
May 10 '06 #14
"Ian Collins" <ia******@hotmail.com> wrote in message
news:4c*************@individual.net...
P.J. Plauger wrote:
"Flash Gordon" <sp**@flash-gordon.me.uk> wrote in message
news:ko************@news.flash-gordon.me.uk...

Tim Prince wrote:

Bart Vandewoestyne wrote:

>On 2006-05-09, Vladimir Oka <no****@btopenworld.com> wrote:
>
>>Function `lgamma()` is only available in C99. GCC `-ansi` assumes C89
>>(same as `-std=c89`). Therein lies your problem. Have you tried
>>`-std=c99` instead?
>
>Indeed: this doesn't generate warnings:
>
>$ gcc -Wall -std=c99 -c rand.c
>
>This does:
>
>$ gcc -Wall -std=c89 -c rand.c
>rand.c: In function `dt':
>rand.c:545: warning: implicit declaration of function `lgamma'
>
>
>So my options are:
>
>* implement lgamma myself if i want to be c89 compatible
>* don't try to be c89 compatible and compile with -std=c99
>

or, you could edit <math.h> and remove the c99 guard on math functions
you consider eligible for c89 compatibility.

This is probably a bad idea for several reasons. It could break valid C89
code (e.g. code that defines an lgamma function with a different
prototype) which you might not hit until months or years later, the C99
stuff may depend on compiler support which is only enabled when C99
support is enabled, it may depend on things in other headers that are
only
enabled when C99 is enabled etc. Also the OP does not have admin writes
so
probably can't do this anyway.

Most of those reasons are pretty esoteric, and not likely to matter
in real life. Nevertheless, I agree that it's a bad idea to edit
system headers. Besides, chances are all you have to do is put:

double (lgamma)(double);

at the top of the source file that calls the function. (The parens are
to protect the declaration against a macro override that may one day
come along.)

Isn't that rather a dangerous thing to recommend?


Not in real life.
I can see two potential problems, the function isn't in the library, so
the application fails to link, or worse, the function exists with a
different prototype resulting in bizarre run time errors.
I said "chances are". And chances are that lgamma is out there in the
binary library and does just what it's supposed to do.
I'd have thought the OP should compile with the correct options to bring
the function in,
Now that I'm aware that the lgamma declaration is suppressed in C89 mode,
I agree that the best solution is simply to switch to C99 mode and get
the proper declaration (and runtime library, and semantics for the
function).
there may be other types or definitions required in
that mode.


What part of "double lgamma(double)" do you think will cause trouble?
I guess pedantry is its own reward.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
May 10 '06 #15
P.J. Plauger wrote:
"Ian Collins" <ia******@hotmail.com> wrote in message
news:4c*************@individual.net...
there may be other types or definitions required in
that mode.

What part of "double lgamma(double)" do you think will cause trouble?
I guess pedantry is its own reward.

In this specific case, none. But if the function used some form of
defined type as a parameter, that type might be different with different
compiler options.

I never have been and hope I never become a pedant. I simply though the
advice carried risks.

--
Ian Collins.
May 10 '06 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Gary Besta | last post by:
I am trying to add a simple case statement to a stored procedure or user defined function. However when I try and save the function/procedure I get 2 syntax errors. Running the query in query...
25
by: Java Böy | last post by:
could some body help me what's happening here... thanks.. char sc = "\x31\xc0" /* xor %eax, %eax */ "\x50" /* push %eax */...
2
by: hykim | last post by:
I want to call a unmanaged dll's function returning some STRUCT's pointer. the next is definition of a STRUCT. ----------------------------------------------------------------------- typedef...
4
by: Elephant | last post by:
Hello, The function Microsoft.VisualBasic.Chr( ), is there a similar function in System.Text ? How does this work? Thank you, me.
1
by: Shawn | last post by:
As if it won't be clear enough from my code, I'm pretty new to C programming. This code is being compiled with an ANSI-C compatible compiler for a microcontroller. That part, I believe, will be...
6
by: dndfan | last post by:
Hello, In the short time I have spent reading this newsgroup, I have seen this sort of declaration a few times: > int > func (string, number, structure) > char* string > int number >...
5
by: vicks | last post by:
Hi, I had just written the following code but not able to figure out why the sizeof() operator is printing the weired output when i give a function name as its operand? #include<stdio.h>...
8
by: vaib | last post by:
hi all , It really seems that C never ceases to amaze . All this time i've been doing C and i thought i was quite adept at it but i was wrong . So without wasting any more time , here's the...
3
by: =?Utf-8?B?TWluZ3lp?= | last post by:
Hi, I have the following question regarding communicating with a OCX control using C#. Idon't have access to the ocx code, so my c# code is actually mimic the behavior of C++ counterparts....
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.