473,396 Members | 1,826 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,396 software developers and data experts.

struct point not identified by gcc

I wrote a simply program with a 3 D coordinate called point. I am
adding two points, taking their distance etc. gcc compiler is not
identifying the struct. Here is the code. I get bunch of errors. The
main ones are : useless storage class specifier in empty declaration,
'point' undeclared (first use in this function), 'p1' undeclared
(first use in this function) etc. Any help will be appreciated. Thanks

#include <stdio.h>
#include <math.h>

/* create a 3d point struct*/
typedef struct point{
int x,y,z;
};

/* Function declaration*/
float dist(point *, point *);
point add(point *, point *);
void display(point *);

int main(void)
{
point p1,p2,p3;
p1.x=2;p1.y=4;p1.z=5;
p2.x=5;p1.y=2;p1.z=6;
printf("\n dist = %f", dist(&p1,&p2));
add(&p1, &p2);
display(&p3);
return;
}

float dist(point *a, point *b)
{
float length=0.0;
length = sqrt( (a->x-b->x)*(a->x-b->x)+ (a->y-b->y)*(a->y-b->y)+
(a->z-b->z)*(a->z-b->z));
return length;
}

point add(point *a, point *b)
{
point temp;
temp.x=a->x + b->x;
temp.y=a->y + b->y;
temp.z=a->z + b->z;
return temp;
}

void display(point *a)
{
printf("\npoint= %d,%d,%d\n",a->x,a->y,a->z);
}

Feb 22 '07 #1
39 3059
On Feb 22, 9:27 am, "DanielJohnson" <diffuse...@gmail.comwrote:
I wrote a simply program with a 3 D coordinate called point. I am
adding two points, taking their distance etc. gcc compiler is not
identifying the struct. Here is the code. I get bunch of errors. The
main ones are : useless storage class specifier in empty declaration,
'point' undeclared (first use in this function), 'p1' undeclared
(first use in this function) etc. Any help will be appreciated. Thanks

#include <stdio.h>
#include <math.h>

/* create a 3d point struct*/
typedef struct point{
int x,y,z;

};

/* Function declaration*/
float dist(point *, point *);
point add(point *, point *);
void display(point *);
Try:
typedef struct tag_point {
int x,y,z;
} point;
>From the C FAQ:
Section 2. Structures, Unions, and Enumerations

2.1: What's the difference between these two declarations?

struct x1 { ... };
typedef struct { ... } x2;

A: The first form declares a "structure tag"; the second declares a
"typedef". The main difference is that you subsequently refer
to the first type as "struct x1" and the second simply as "x2".
That is, the second declaration is of a slightly more abstract
type -- its users don't necessarily know that it is a structure,
and the keyword struct is not used when declaring instances of it.

2.2: Why doesn't

struct x { ... };
x thestruct;

work?

A: C is not C++. Typedef names are not automatically generated for
structure tags. See also question 2.1 above.

[snip]

Feb 22 '07 #2
DanielJohnson wrote:
I wrote a simply program with a 3 D coordinate called point. I am
adding two points, taking their distance etc. gcc compiler is not
identifying the struct. Here is the code. I get bunch of errors. The
main ones are : useless storage class specifier in empty declaration,
'point' undeclared (first use in this function), 'p1' undeclared
(first use in this function) etc. Any help will be appreciated. Thanks

#include <stdio.h>
#include <math.h>

/* create a 3d point struct*/
typedef struct point{
int x,y,z;
};
Change this to:

typedef struct {
int x;
int y;
int z;
} point;
/* Function declaration*/
float dist(point *, point *);
point add(point *, point *);
void display(point *);

int main(void)
{
point p1,p2,p3;
p1.x=2;p1.y=4;p1.z=5;
p2.x=5;p1.y=2;p1.z=6;
Why not use more whitespace to aid in clarity? You also seem to be
overwriting the values in p1.y and p1.z.
printf("\n dist = %f", dist(&p1,&p2));
Add a newline after %f as well. Also as per the initialisations given
above, p2.y, p2.z, p3.x, p3.y, p3.z are all uninitialised. If you
operate on uninitialised objects you'll most likely get back useless
results.
add(&p1, &p2);
The function add returns a local object, (as given below). You're not
assigning the return value to anything here, thus loosing it.
display(&p3);
return;
Return an explicit value; don't return random values.
}

float dist(point *a, point *b)
{
float length=0.0;
length = sqrt( (a->x-b->x)*(a->x-b->x)+ (a->y-b->y)*(a->y-b->y)+
(a->z-b->z)*(a->z-b->z));
You do realise that b->y and b->z are uninitialised don't you? You
might also want to split up the messy function call into more readable
steps.
return length;
}

point add(point *a, point *b)
{
point temp;
temp.x=a->x + b->x;
temp.y=a->y + b->y;
temp.z=a->z + b->z;
return temp;
}

void display(point *a)
{
printf("\npoint= %d,%d,%d\n",a->x,a->y,a->z);
}
Correct these errors and try to compile again.

Feb 22 '07 #3
On Feb 22, 12:43 pm, "santosh" <santosh....@gmail.comwrote:
DanielJohnson wrote:
I wrote a simply program with a 3 D coordinate called point. I am
adding two points, taking their distance etc. gcc compiler is not
identifying the struct. Here is the code. I get bunch of errors. The
main ones are : useless storage class specifier in empty declaration,
'point' undeclared (first use in this function), 'p1' undeclared
(first use in this function) etc. Any help will be appreciated. Thanks
#include <stdio.h>
#include <math.h>
/* create a 3d point struct*/
typedef struct point{
int x,y,z;
};

Change this to:

typedef struct {
int x;
int y;
int z;
} point;
/* Function declaration*/
float dist(point *, point *);
point add(point *, point *);
void display(point *);
int main(void)
{
point p1,p2,p3;
p1.x=2;p1.y=4;p1.z=5;
p2.x=5;p1.y=2;p1.z=6;

Why not use more whitespace to aid in clarity? You also seem to be
overwriting the values in p1.y and p1.z.
printf("\n dist = %f", dist(&p1,&p2));

Add a newline after %f as well. Also as per the initialisations given
above, p2.y, p2.z, p3.x, p3.y, p3.z are all uninitialised. If you
operate on uninitialised objects you'll most likely get back useless
results.
add(&p1, &p2);

The function add returns a local object, (as given below). You're not
assigning the return value to anything here, thus loosing it.
display(&p3);
return;

Return an explicit value; don't return random values.
}
float dist(point *a, point *b)
{
float length=0.0;
length = sqrt( (a->x-b->x)*(a->x-b->x)+ (a->y-b->y)*(a->y-b->y)+
(a->z-b->z)*(a->z-b->z));

You do realise that b->y and b->z are uninitialised don't you? You
might also want to split up the messy function call into more readable
steps.
return length;
}
point add(point *a, point *b)
{
point temp;
temp.x=a->x + b->x;
temp.y=a->y + b->y;
temp.z=a->z + b->z;
return temp;
}
void display(point *a)
{
printf("\npoint= %d,%d,%d\n",a->x,a->y,a->z);
}

Correct these errors and try to compile again.
I did all your changes and now I get this error

undefined reference to `sqrt'

Is math.sqrt() correct or just sqrt()

Feb 22 '07 #4
On Thu, 22 Feb 2007 09:27:06 -0800, DanielJohnson wrote:
I wrote a simply program with a 3 D coordinate called point. I am
adding two points, taking their distance etc. gcc compiler is not
identifying the struct. Here is the code. I get bunch of errors. The
main ones are : useless storage class specifier in empty declaration,
'point' undeclared (first use in this function), 'p1' undeclared
(first use in this function) etc. Any help will be appreciated. Thanks

#include <stdio.h>
#include <math.h>

/* create a 3d point struct*/
typedef struct point{
int x,y,z;
};
You need a name for the typedef, so you could do something like:

typedef struct {
int x, y, z;
} point;

-Alok
Feb 22 '07 #5
DanielJohnson wrote:
On Feb 22, 12:43 pm, "santosh" <santosh....@gmail.comwrote:
DanielJohnson wrote:
I wrote a simply program with a 3 D coordinate called point. I am
adding two points, taking their distance etc. gcc compiler is not
identifying the struct. Here is the code. I get bunch of errors.
<snip code and corrections>
Correct these errors and try to compile again.

I did all your changes and now I get this error

undefined reference to `sqrt'
Under most UNIX systems, the math library is seperate and not linked
in by default. Add the '-lm' command line switch, (without the
quotes), to cc when you compile it.
Is math.sqrt() correct or just sqrt()
The latter, with an argument. The former is C++.

Feb 22 '07 #6

santosh wrote:
DanielJohnson wrote:
On Feb 22, 12:43 pm, "santosh" <santosh....@gmail.comwrote:
DanielJohnson wrote:
I wrote a simply program with a 3 D coordinate called point. I am
adding two points, taking their distance etc. gcc compiler is not
identifying the struct. Here is the code. I get bunch of errors.
<snip code and corrections>
Is math.sqrt() correct or just sqrt()

The latter, with an argument. The former is C++.
Correction: It's not even C++.

Feb 22 '07 #7
Here is the updated program but it doesnt compile and says undefined
refrence to sqrt. Can you please tell whats going wrong in this
variant of the program.
#include <stdio.h>
#include <math.h>

/* create a 3d point struct*/
typedef struct{
int x;
int y;
int z;
} point;

/* Function declaration*/
double dist(point *, point *);
point add(point *, point *);
void display(point *);

int main(void)
{
point p1,p2,p3;
p1.x=2;p1.y=4;p1.z=5;
p2.x=5;p2.y=2;p2.z=6;
printf("\n dist = %lf \n", dist(&p1,&p2));
p3 = add(&p1, &p2);
display(&p3);
return 0;
}

double dist(point *a, point *b)
{
double length=0.0;
length = sqrt( (a->x-b->x)*(a->x-b->x) + (a->y-b->y)*(a->y-b->y)+
(a->z-b->z)*(a->z-b->z) );
return length;
}

point add(point *a, point *b)
{
point temp;
temp.x=a->x + b->x;
temp.y=a->y + b->y;
temp.z=a->z + b->z;
return temp;
}

void display(point *a)
{
printf("\npoint= %d,%d,%d\n",a->x,a->y,a->z);
}

Feb 22 '07 #8
Under most UNIX systems, the math library is seperate and not linked
in by default. Add the '-lm' command line switch, (without the
quotes), to cc when you compile it.
Is math.sqrt() correct or just sqrt()

The latter, with an argument. The former is C++.
Thanks it worked. Do I always need to compile using -lm switch. I was
completely unawre if this.
Feb 22 '07 #9
On Feb 22, 10:20 am, "DanielJohnson" <diffuse...@gmail.comwrote:
Under most UNIX systems, the math library is seperate and not linked
in by default. Add the '-lm' command line switch, (without the
quotes), to cc when you compile it.
Is math.sqrt() correct or just sqrt()
The latter, with an argument. The former is C++.

Thanks it worked. Do I always need to compile using -lm switch. I was
completely unawre if this.
How did you miss this when you read the C-FAQ:

14.3: I'm trying to do some simple trig, and I am #including <math.h>,
but I keep getting "undefined: sin" compilation errors.

A: Make sure you're actually linking with the math library. For
instance, under Unix, you usually need to use the -lm option, at
the *end* of the command line, when compiling/linking. See also
questions 13.25, 13.26, and 14.2.

Feb 22 '07 #10
DanielJohnson wrote:
Under most UNIX systems, the math library is seperate and not linked
in by default. Add the '-lm' command line switch, (without the
quotes), to cc when you compile it.
Is math.sqrt() correct or just sqrt()
The latter, with an argument. The former is C++.

Thanks it worked. Do I always need to compile using -lm switch. I was
completely unawre if this.
No, only when you use one or more functions from the math library. The
interface for the math functions are in the headers math.h, float.h,
complex.h, tgmath.h and fenv.h. The last three are specific to C99.

BTW, this is an instance of the more general requirement that if you
use functions from library XY, then you should usually supply an -lXY
switch to the compiler. The exception is the C standard library
itself, which is linked in automatically. The standard doesn't specify
the details of compilation and linkage, so for more details consult
your system's compiler and linker manuals.

Feb 22 '07 #11
DanielJohnson wrote, On 22/02/07 17:55:
On Feb 22, 12:43 pm, "santosh" <santosh....@gmail.comwrote:
<snip>
>Correct these errors and try to compile again.

I did all your changes and now I get this error

undefined reference to `sqrt'

Is math.sqrt() correct or just sqrt()
1) Why on earth would you want math.sqrt()?
2) You need to learn how to drive the tools you are using and specific
tools are not on topic here.
3) You need to read the comp.lang.c FAQ at http://c-faq.com/
specifically Q14.3, but you should always consult it before asking a
question in case it is covered.
--
Flash Gordon
Feb 22 '07 #12
DanielJohnson wrote, On 22/02/07 18:15:
Here is the updated program but it doesnt compile and says undefined
refrence to sqrt. Can you please tell whats going wrong in this
<snip>

Did you wait a reasonable time to see if anyone would ask last time you
asked this question (without the source which in this case was not
needed) before asking again? No. I fetched news, wrote a reply, and when
I did the next post/fetch cycle I got you asking again as well as two
other people answering the question and me posting my answer.

In future, please wait at least a day before assuming no one has
answered, and even then check the FAQ in case the reason no one bothers
to answer is because it is covered there.
--
Flash Gordon
Feb 22 '07 #13
In future, please wait at least a day before assuming no one has
answered, and even then check the FAQ in case the reason no one bothers
to answer is because it is covered there.
--
Flash Gordon
Santosh suggested me to do something and I tried his changes and still
it won't work. The thing which he told me was to use the switch -lm
for compiling correctly.

OK Sir, Learning the rules of the game.
Feb 22 '07 #14
"DanielJohnson" <di********@gmail.comwrites:
[...]
typedef struct point{
int x,y,z;
};
[...]

As several others have pointed out, this is your problem. You create
a structure type "struct point" and, in the same declaration, attempt
to create a typedef (alias) for it -- but you don't provide a name for
the typedef. This isn't necessarily illegal, but you should have
gotten a warning; if you didn't, crank up the warning level on your
compiler.

If you want a typedef, the way to do it is:

typedef struct {
int x, y, z;
} point;

(you can optionally have a tag name after the word "struct"). Or
you can lose the typedef altogether and just do this:

struct point {
int x, y, z;
};

This creates a type called "struct point". Since it already has a
perfectly good name, there's no need to create an alias for it using
"typedef". If you use this declaration, you'll need to refer to the
type as "struct point" rather than just "point":

struct point p0;
struct point p1;

In some people's opinions, including mine, this is a cleaner approach;
it's more verbose, but that's a good thing, since it's obvious from
the declaration that p0 and p1 are struct objects.

On the other hand, a lot of programmers prefer to use typedefs so they
can have a one-word name for the type. Either approach is legitimate;
even if you choose one or the other, you should understand both.

See section 2 of the comp.lang.c FAQ, <http://www.c-faq.com/>.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Feb 22 '07 #15
DanielJohnson a écrit :
>Under most UNIX systems, the math library is seperate and not linked
in by default. Add the '-lm' command line switch, (without the
quotes), to cc when you compile it.
>>Is math.sqrt() correct or just sqrt()
The latter, with an argument. The former is C++.

Thanks it worked. Do I always need to compile using -lm switch. I was
completely unawre if this.

You need the -lm switch when using the gcc software. This switch is
never used in other compilers not under unix, that are smart enough to
including the C math functions by default.

We have often discussed here this msfeature of gcc but they will never
change it as it seems.
Feb 22 '07 #16
"DanielJohnson" <di********@gmail.comwrote:
>Santosh suggested me to do something and I tried his changes and still
it won't work. The thing which he told me was to use the switch -lm
for compiling correctly.
"for linking correctly"
>OK Sir, Learning the rules of the game.
One rule down, another 999,999,999 to go. ;)
Roberto Waltman

[ Please reply to the group,
return address is invalid ]
Feb 22 '07 #17
On Thu, 22 Feb 2007 22:12:48 +0100, in comp.lang.c , "jacob.navia"
<ja***@jacob.remcomp.frwrote:
>DanielJohnson a écrit :
>Thanks it worked. Do I always need to compile using -lm switch. I was
completely unawre if this.
We have often discussed here this msfeature of gcc but they will never
change it as it seems.
Its not a gcc-ism, please don't mischaracterise it. You're probably
aware that all DOS compilers exhibited this same feature for many
years. I think some early Windows ones even did it (certainly C6 and
Watcom's C compiler did, even when compiling windows-compatible code).
The reason, as far as I recall, is their roots in hardware that didn't
provide floating point support.
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
Feb 22 '07 #18
"jacob.navia" <ja***@jacob.remcomp.frwrites:
DanielJohnson a écrit :
>>Under most UNIX systems, the math library is seperate and not linked
in by default. Add the '-lm' command line switch, (without the
quotes), to cc when you compile it.

Is math.sqrt() correct or just sqrt()
The latter, with an argument. The former is C++.

Thanks it worked. Do I always need to compile using -lm switch. I was
completely unawre if this.

You need the -lm switch when using the gcc software. This switch is
never used in other compilers not under unix, that are smart enough to
including the C math functions by default.

We have often discussed here this msfeature of gcc but they will never
change it as it seems.
gcc is not the only compiler that behaves this way. Sun's "cc" under
Solaris, for example, also requires "-lm" to link the math library.
It may be specific to Unix-like systems; it's not specific to gcc.

I agree that the explicit "-lm" shouldn't be necessary, but I don't
think it's a huge deal.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Feb 23 '07 #19
In article <45***********************@news.orange.fr>,
jacob.navia <ja***@jacob.remcomp.frwrote:
>You need the -lm switch when using the gcc software. This switch is
never used in other compilers not under unix, that are smart enough to
including the C math functions by default.
This doesn't have anything much to do with gcc. Before gcc was
thought of, unix needed -lm to link the maths library, and today the
Mac I'm using doesn't need -lm even though its compiler is gcc.

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Feb 23 '07 #20
ri*****@cogsci.ed.ac.uk (Richard Tobin) writes:
In article <45***********************@news.orange.fr>,
jacob.navia <ja***@jacob.remcomp.frwrote:
>>You need the -lm switch when using the gcc software. This switch is
never used in other compilers not under unix, that are smart enough to
including the C math functions by default.

This doesn't have anything much to do with gcc. Before gcc was
thought of, unix needed -lm to link the maths library, and today the
Mac I'm using doesn't need -lm even though its compiler is gcc.
<OT>
Are you sure about the latter? I tried a small test program that
computes sqrt(2.0) and it didn't require "-lm" because gcc was smart
enough to compute the value at compilation time. Try something like
sqrt(time(NULL)); it's a silly thing to compute, but it should prevent
the compiler from optimizing away the sqrt() call.
</OT>

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Feb 23 '07 #21
DanielJohnson wrote:
>
I wrote a simply program with a 3 D coordinate called point. I am
adding two points, taking their distance etc. gcc compiler is not
identifying the struct. Here is the code. I get bunch of errors. The
main ones are : useless storage class specifier in empty declaration,
'point' undeclared (first use in this function), 'p1' undeclared
(first use in this function) etc. Any help will be appreciated. Thanks

#include <stdio.h>
#include <math.h>

/* create a 3d point struct*/
typedef struct point{
int x,y,z;
};
Well, you might consider giving the typedef a name to define as
"struct point". The name "point" would agree with the other code.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
Feb 23 '07 #22
Keith Thompson (The_Other_Keith) k...@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
I did the same thing, i.e. wrote a small program and sqrt just worked
fine without -lm switch. Whats the matter ?, but in my previous
program it did require me to do the -lm switch.
Feb 23 '07 #23
"DanielJohnson" <di********@gmail.comwrites:
>Keith Thompson (The_Other_Keith) k...@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.

I did the same thing, i.e. wrote a small program and sqrt just worked
fine without -lm switch. Whats the matter ?, but in my previous
program it did require me to do the -lm switch.
It's usually more helpful to quote the relevant context and *not* the
signature.

Probably the compiler was able to compute sqrt() at compile time,
avoiding the need to use the math library at run time.

But it's not really a C question; it's about a specific compiler. Try
looking at an assembly listing, or posting to a system-specific
newsgroup (if you're using gcc, try gnu.gcc.help).

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Feb 23 '07 #24
In article <ln************@nuthaus.mib.orgKeith Thompson <ks***@mib.orgwrites:
....
[ about sqrt findable without specific switch.]
But it's not really a C question; it's about a specific compiler.
It is not about the compiler either. It is about the OS. When the
standard C library supplied on the OS contains sqrt, the switch is
not needed. When that library does *not* contain sqrt, the switch
is needed (unless the compiler can calculate the value compile-time).

Originally on Unix the standard C library did *not* contain the math
functions, so you had to supply the -lm switch. But in some versions
the two libraries are unified to a single library.
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
Feb 23 '07 #25
"Dik T. Winter" <Di********@cwi.nlwrote:
In article <ln************@nuthaus.mib.orgKeith Thompson <ks***@mib.orgwrites:
...
[ about sqrt findable without specific switch.]
But it's not really a C question; it's about a specific compiler.

It is not about the compiler either. It is about the OS. When the
standard C library supplied on the OS contains sqrt,
No, it's about the implementation. Some implementations are delivered
complete, with the library and the compiler; some are delivered
piecemeal, with the compiler hoping and praying that someone sent a
library along with the OS, and that perhaps they can form a valid C
implementation together.

Richard
Feb 23 '07 #26
In article <ln************@nuthaus.mib.org>,
Keith Thompson <ks***@mib.orgwrote:
>This doesn't have anything much to do with gcc. Before gcc was
thought of, unix needed -lm to link the maths library, and today the
Mac I'm using doesn't need -lm even though its compiler is gcc.
>Are you sure about the latter? I tried a small test program that
computes sqrt(2.0) and it didn't require "-lm" because gcc was smart
enough to compute the value at compilation time.
Yes, I made exactly that mistake - I even used sqrt(2.0).
>Try something like
sqrt(time(NULL)); it's a silly thing to compute, but it should prevent
the compiler from optimizing away the sqrt() call.
But in fact that still works without -lm.

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Feb 23 '07 #27
Richard Tobin wrote:
In article <ln************@nuthaus.mib.org>,
Keith Thompson <ks***@mib.orgwrote:
This doesn't have anything much to do with gcc. Before gcc was
thought of, unix needed -lm to link the maths library, and today the
Mac I'm using doesn't need -lm even though its compiler is gcc.
Are you sure about the latter? I tried a small test program that
computes sqrt(2.0) and it didn't require "-lm" because gcc was smart
enough to compute the value at compilation time.

Yes, I made exactly that mistake - I even used sqrt(2.0).
Try something like
sqrt(time(NULL)); it's a silly thing to compute, but it should prevent
the compiler from optimizing away the sqrt() call.

But in fact that still works without -lm.
Then either the compiler has an intrinsic for sqrt or else the latter
is present in the same library that contains the non-math standard C
functions. Also maybe the environment is secretly supplying the
library option?

Feb 23 '07 #28
On 2007-02-22, jacob.navia <ja***@jacob.remcomp.frwrote:
DanielJohnson a écrit :
>>Under most UNIX systems, the math library is seperate and not linked
in by default. Add the '-lm' command line switch, (without the
quotes), to cc when you compile it.

Is math.sqrt() correct or just sqrt()
The latter, with an argument. The former is C++.

Thanks it worked. Do I always need to compile using -lm switch. I was
completely unawre if this.

You need the -lm switch when using the gcc software. This switch is
never used in other compilers not under unix, that are smart enough to
including the C math functions by default.

We have often discussed here this msfeature of gcc but they will never
change it as it seems.
You cannot remove historical quirks like this without breaking
anything, especially if they've been almost universial practice
for a couple of decades. I've considered adding -lm by default
to my compiler as well, but I figured this would end up doing
the users a disservice. Because if you write build scripts for
a compiler which does not require you to link that library
explicitly, then chances are your project will break if you
use it with a different compiler.

In that case the time you have to deal with this problem is
just deferred until a later date. So I think it's best to have
to encounter, recognize, and fix this problem as early as
possible.

It's not just compilers by different vendors that would be
affected, but also older versions of a compiler that recently
abandoned this requirement. If gcc comes out with a new
release that always links with that library by default, as you
you suggest, then code developed with that compiler version is
more likely not to build with older versions.

There are many other more system-specific libraries that can
break the necks of developers, such as the networking libraries
you explicitly have to link with on Solaris/UnixWare/Reliant,
and then there are realtime, threading and dynamic linking libs
you need on some Unices, which tend to be very inconsistent.
You may also run into problems with weak empty function stubs
in a Unix standard C library object, which make it seem like you
don't have to link with any other libs when in fact you do.

I've given some thought to this topic as well, and one approach
that came to my mind, is to have the linker or compiler give
you more info than ``symbol x is undefined!''. Maybe we should
have some sort of database of all standard library functions
available on a given system. Then the linker error message
could read ``symbol x undefined (but available in /usr/lib/x.so:
add -lx to the linker flags!)'' instead.

I'm currently doing some ELF file access code, and maybe I'll
also end up writing a linker, so I may play with such a symbol
database some time soon. In general, though, I feel that system
library dependencies are already a terrible battlefield which
would benefit immensely from preserving at least a tiny bit of
consistency across multiple platforms and compilers.

(BTW, if I recall correctly, AIX by default does not even
install the math library, so a compiler that unconditionally
links with it would have an unneeded dependency.)

--
Nils R. Weller, Bremen (Germany)
My real email address is ``nils<at>gnulinux<dot>nl''
.... but I'm not speaking for the Software Libre Foundation!
Feb 23 '07 #29
Nils Weller a écrit :
On 2007-02-22, jacob.navia <ja***@jacob.remcomp.frwrote:
>>
We have often discussed here this msfeature of gcc but they will never
change it as it seems.


You cannot remove historical quirks like this without breaking
anything, especially if they've been almost universial practice
for a couple of decades. I've considered adding -lm by default
to my compiler as well, but I figured this would end up doing
the users a disservice. Because if you write build scripts for
a compiler which does not require you to link that library
explicitly, then chances are your project will break if you
use it with a different compiler.
Wrong!
They would say

What a bad compiler this one. It needs an explicit -lm switch.
And the builders of the other compiler would say
Mmmm many users are complaining that the xxx compiler does NOT
need the -lm switch... Should we include it by default?
In that case the time you have to deal with this problem is
just deferred until a later date. So I think it's best to have
to encounter, recognize, and fix this problem as early as
possible.
That is why no progress is done in unix... Old bugs are preserved
for posterity. The "make" utility needs a tab as the first char
in an action line. If you put spaces it will not recognize the
line and say:

Missing separator in rules. Stop.

This BUG has been in all unices since at least 1983, when I saw the
first one, and was bitten by this bug.
It's not just compilers by different vendors that would be
affected, but also older versions of a compiler that recently
abandoned this requirement. If gcc comes out with a new
release that always links with that library by default, as you
you suggest, then code developed with that compiler version is
more likely not to build with older versions.
So what?
There are many other more system-specific libraries that can
break the necks of developers, such as the networking libraries
you explicitly have to link with on Solaris/UnixWare/Reliant,
and then there are realtime, threading and dynamic linking libs
you need on some Unices, which tend to be very inconsistent.
You may also run into problems with weak empty function stubs
in a Unix standard C library object, which make it seem like you
don't have to link with any other libs when in fact you do.

I've given some thought to this topic as well, and one approach
that came to my mind, is to have the linker or compiler give
you more info than ``symbol x is undefined!''. Maybe we should
have some sort of database of all standard library functions
available on a given system. Then the linker error message
could read ``symbol x undefined (but available in /usr/lib/x.so:
add -lx to the linker flags!)'' instead.
I have given also much thought to this, and I have developed a
database that is used by the IDE. When it detects a message like
"undefined external reference" it wil look at the database and
propose the libraries to the user...

I'm currently doing some ELF file access code, and maybe I'll
also end up writing a linker, so I may play with such a symbol
database some time soon. In general, though, I feel that system
library dependencies are already a terrible battlefield which
would benefit immensely from preserving at least a tiny bit of
consistency across multiple platforms and compilers.
Yeah, but a more modern approach than the 1980 approach would be useful.
I wrote a linker for lcc-win32 under windows, and added most common
libraries to it automatically...
Not a big deal.
(BTW, if I recall correctly, AIX by default does not even
install the math library, so a compiler that unconditionally
links with it would have an unneeded dependency.)
I am working with AIX version too. Great system but needs some
more user friendliness.
jacob
Feb 23 '07 #30
In article <11**********************@h3g2000cwc.googlegroups. com>,
santosh <sa*********@gmail.comwrote:
>This doesn't have anything much to do with gcc. Before gcc was
thought of, unix needed -lm to link the maths library, and today the
Mac I'm using doesn't need -lm even though its compiler is gcc.
>Are you sure about the latter? I tried a small test program that
computes sqrt(2.0) and it didn't require "-lm" because gcc was smart
enough to compute the value at compilation time.
>Yes, I made exactly that mistake - I even used sqrt(2.0).
>Try something like
sqrt(time(NULL)); it's a silly thing to compute, but it should prevent
the compiler from optimizing away the sqrt() call.

But in fact that still works without -lm.
>Then either the compiler has an intrinsic for sqrt or else the latter
is present in the same library that contains the non-math standard C
functions. Also maybe the environment is secretly supplying the
library option?
Foiled again, it's using an inline instruction for sqrt. So this time
I changed it to tgamma(time(0)), on the assumption that not even the
latest Intel processors have a gamma instruction, and it *still*
doesn't need -lm. Looking at the output of gcc -S and gcc -v, it
appears to be calling a function and linking to a library "-lSystem",
which includes the maths functions.

-- Richard

--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Feb 23 '07 #31
Good evening,

On 2007-02-23, jacob navia <ja***@jacob.remcomp.frwrote:
Nils Weller a écrit :
>On 2007-02-22, jacob.navia <ja***@jacob.remcomp.frwrote:
>>>
We have often discussed here this msfeature of gcc but they will never
change it as it seems.


You cannot remove historical quirks like this without breaking
anything, especially if they've been almost universial practice
for a couple of decades. I've considered adding -lm by default
to my compiler as well, but I figured this would end up doing
the users a disservice. Because if you write build scripts for
a compiler which does not require you to link that library
explicitly, then chances are your project will break if you
use it with a different compiler.

Wrong!
They would say

What a bad compiler this one. It needs an explicit -lm switch.
Well, they may not appreciate it, but it would be "for their own
good". ;-)

This isn't a big deal anyway because compilers are already
incompatible in lots of other ways, and I can see where you're
coming from. But myself I would prefer to be warned about such
potential problems, and I can see good reasons for not changing
this particular oddity.

(It would also be nice to get warnings about functions with
dangerously nonportable semantics, such as the signal()
function which is still broken on most System V derivatives,
but that's another story ...)
And the builders of the other compiler would say
Mmmm many users are complaining that the xxx compiler does NOT
need the -lm switch... Should we include it by default?
Yes, this is not a fortunate situation. But you see, there is so
much existing practice that you can't get everyone to change this
without running into new problems. That's all I was saying. If
you get gcc to change, you'd still have to get lots and lots of
other Unix compilers to change, and lots of users to update their
gcc versions.
>It's not just compilers by different vendors that would be
affected, but also older versions of a compiler that recently
abandoned this requirement. If gcc comes out with a new
release that always links with that library by default, as you
you suggest, then code developed with that compiler version is
more likely not to build with older versions.

So what?
That would impose another implicit (and probably unexpected)
compiler version dependency on your software. The confusion would
be shifted from the developer to the end users, who you shouldn't
expect to have the most up-to-date compiler version available to
compile your stuff.

That would suck.
I have given also much thought to this, and I have developed a
database that is used by the IDE. When it detects a message like
"undefined external reference" it wil look at the database and
propose the libraries to the user...
Good!
I am working with AIX version too. Great system but needs some
more user friendliness.
True. Would also have been nice if it didn't force all-position-
independent code down everyone's throat.

--
Nils R. Weller, Bremen (Germany)
My real email address is ``nils<at>gnulinux<dot>nl''
.... but I'm not speaking for the Software Libre Foundation!
Feb 23 '07 #32
Richard Tobin wrote:
In article <11**********************@h3g2000cwc.googlegroups. com>,
santosh <sa*********@gmail.comwrote:
This doesn't have anything much to do with gcc. Before gcc was
thought of, unix needed -lm to link the maths library, and today the
Mac I'm using doesn't need -lm even though its compiler is gcc.
Are you sure about the latter? I tried a small test program that
computes sqrt(2.0) and it didn't require "-lm" because gcc was smart
enough to compute the value at compilation time.
Yes, I made exactly that mistake - I even used sqrt(2.0).

Try something like
sqrt(time(NULL)); it's a silly thing to compute, but it should prevent
the compiler from optimizing away the sqrt() call.

But in fact that still works without -lm.
Then either the compiler has an intrinsic for sqrt or else the latter
is present in the same library that contains the non-math standard C
functions. Also maybe the environment is secretly supplying the
library option?

Foiled again, it's using an inline instruction for sqrt. So this time
I changed it to tgamma(time(0)), on the assumption that not even the
latest Intel processors have a gamma instruction, and it *still*
doesn't need -lm. Looking at the output of gcc -S and gcc -v, it
appears to be calling a function and linking to a library "-lSystem",
which includes the maths functions.
Hmm..., curious system, (which one?.) This is a case in point for the
argument to keep this group loosely bound to ISO C.

Feb 23 '07 #33
On Fri, 23 Feb 2007 17:58:11 +0100, in comp.lang.c , jacob navia
<ja***@jacob.remcomp.frwrote:
>That is why no progress is done in unix... Old bugs are preserved
for posterity.
right - that'll be why unix and unix clones are so unsuccessful then.
>The "make" utility needs a tab as the first char
in an action line. If you put spaces it will not recognize the
line
Apparently you have a /really/ old version of make.
>This BUG has been in all unices since at least 1983,
A bug is when something doesn't behave like its supposed to. Ever used
a fortran compiler? Rry sticking a C character at column seven. Theres
a bug that means it skips that line, apparently.

As it happens, I think the need to put -lm in on some versions of gcc
and some osen is a bit annoying, but its hardly a bug, any more than
having to use vi is a bug, or some versions of SQL not liking iif()
statements. Its just How That Platform Works (tm).
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
Feb 23 '07 #34
In article <11**********************@s48g2000cws.googlegroups .com>,
santosh <sa*********@gmail.comwrote:
>Foiled again, it's using an inline instruction for sqrt. So this time
I changed it to tgamma(time(0)), on the assumption that not even the
latest Intel processors have a gamma instruction, and it *still*
doesn't need -lm. Looking at the output of gcc -S and gcc -v, it
appears to be calling a function and linking to a library "-lSystem",
which includes the maths functions.
>Hmm..., curious system, (which one?.)
Just a current Mac.

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Feb 23 '07 #35
jacob navia <ja***@jacob.remcomp.frwrites:
Nils Weller a écrit :
>On 2007-02-22, jacob.navia <ja***@jacob.remcomp.frwrote:
>>We have often discussed here this msfeature of gcc but they will
never change it as it seems.
You cannot remove historical quirks like this without breaking
anything, especially if they've been almost universial practice
for a couple of decades. I've considered adding -lm by default
to my compiler as well, but I figured this would end up doing
the users a disservice. Because if you write build scripts for
a compiler which does not require you to link that library
explicitly, then chances are your project will break if you
use it with a different compiler.

Wrong!
They would say

What a bad compiler this one. It needs an explicit -lm switch.
And the builders of the other compiler would say
Mmmm many users are complaining that the xxx compiler does NOT
need the -lm switch... Should we include it by default?
Oh, no, I have to type an extra 3 characters!

Most C compilers are not conforming by default; you have to specify
extra options to make then conform to the standard. I agree that the
need for "-lm" is annoying, but I just don't worry about it that much.
>In that case the time you have to deal with this problem is
just deferred until a later date. So I think it's best to have
to encounter, recognize, and fix this problem as early as
possible.

That is why no progress is done in unix... Old bugs are preserved
for posterity. The "make" utility needs a tab as the first char
in an action line. If you put spaces it will not recognize the
line and say:

Missing separator in rules. Stop.

This BUG has been in all unices since at least 1983, when I saw the
first one, and was bitten by this bug.
So you haven't actually been bitten by it since 1983? And what does a
misfeature in "make" have to do with C?

(I don't know whether correcting this might break some existing
Makefiles. Discuss in comp.unix.programmer if you're interested.)
>It's not just compilers by different vendors that would be
affected, but also older versions of a compiler that recently
abandoned this requirement. If gcc comes out with a new
release that always links with that library by default, as you
you suggest, then code developed with that compiler version is
more likely not to build with older versions.

So what?
So code developed with that compiler version is more likely not to
build with older versions.

[...]

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Feb 23 '07 #36
Mark McIntyre <ma**********@spamcop.netwrites:
On Fri, 23 Feb 2007 17:58:11 +0100, in comp.lang.c , jacob navia
<ja***@jacob.remcomp.frwrote:
>>That is why no progress is done in unix... Old bugs are preserved
for posterity.

right - that'll be why unix and unix clones are so unsuccessful then.
>>The "make" utility needs a tab as the first char
in an action line. If you put spaces it will not recognize the
line

Apparently you have a /really/ old version of make.
[...]

<OT>
Not necessarily. GNU Make 3.81, the latest version, is less than a
year old. (It at least suggests using a tab rather than 8 spaces.)
</OT>

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Feb 23 '07 #37
On Thu, 22 Feb 2007 12:35:49 -0600, user923005 wrote
(in article <11**********************@t69g2000cwt.googlegroups .com>):
On Feb 22, 10:20 am, "DanielJohnson" <diffuse...@gmail.comwrote:
>>Under most UNIX systems, the math library is seperate and not linked
in by default. Add the '-lm' command line switch, (without the
quotes), to cc when you compile it.
>>>Is math.sqrt() correct or just sqrt()
>>The latter, with an argument. The former is C++.

Thanks it worked. Do I always need to compile using -lm switch. I was
completely unawre if this.

How did you miss this when you read the C-FAQ:

14.3: I'm trying to do some simple trig, and I am #including <math.h>,
but I keep getting "undefined: sin" compilation errors.

A: Make sure you're actually linking with the math library. For
instance, under Unix, you usually need to use the -lm option, at
the *end* of the command line, when compiling/linking. See also
questions 13.25, 13.26, and 14.2.
You will notice that it doesn't actually answer his question.
Specifically, "Do i always" ...

And the answer is no, but if you are using any of the math functions
which require it, then yes obviously for a given program.
--
Randy Howard (2reply remove FOOBAR)
"The power of accurate observation is called cynicism by those
who have not got it." - George Bernard Shaw

Mar 7 '07 #38
On Thu, 22 Feb 2007 17:39:04 -0600, Mark McIntyre wrote
(in article <97********************************@4ax.com>):
On Thu, 22 Feb 2007 22:12:48 +0100, in comp.lang.c , "jacob.navia"
<ja***@jacob.remcomp.frwrote:
>DanielJohnson a écrit :
>>Thanks it worked. Do I always need to compile using -lm switch. I was
completely unawre if this.
We have often discussed here this msfeature of gcc but they will never
change it as it seems.

Its not a gcc-ism, please don't mischaracterise it. You're probably
aware that all DOS compilers exhibited this same feature for many
years. I think some early Windows ones even did it (certainly C6 and
Watcom's C compiler did, even when compiling windows-compatible code).
The reason, as far as I recall, is their roots in hardware that didn't
provide floating point support.
You had the option to pick what type of math implementation you wanted
to link against, software fp emulation, 8087, Weitek, 68881/2, etc. I
wonder if lcc-win32 can generate code for a Weitek coprocessor?
--
Randy Howard (2reply remove FOOBAR)
"The power of accurate observation is called cynicism by those
who have not got it." - George Bernard Shaw

Mar 7 '07 #39
Randy Howard wrote:
On Thu, 22 Feb 2007 17:39:04 -0600, Mark McIntyre wrote
><ja***@jacob.remcomp.frwrote:
>>DanielJohnson a écrit :

Thanks it worked. Do I always need to compile using -lm switch.
I was completely unawre if this.

We have often discussed here this msfeature of gcc but they will
never change it as it seems.

Its not a gcc-ism, please don't mischaracterise it. You're probably
aware that all DOS compilers exhibited this same feature for many
years. I think some early Windows ones even did it (certainly C6
and Watcom's C compiler did, even when compiling windows-compatible
code). The reason, as far as I recall, is their roots in hardware
that didn't provide floating point support.

You had the option to pick what type of math implementation you
wanted to link against, software fp emulation, 8087, Weitek,
68881/2, etc. I wonder if lcc-win32 can generate code for a
Weitek coprocessor?
I doubt it. It won't run on a 486 (at least the debugger portion).

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>

"A man who is right every time is not likely to do very much."
-- Francis Crick, co-discover of DNA
"There is nothing more amazing than stupidity in action."
-- Thomas Matthews
Mar 7 '07 #40

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

Similar topics

15
by: Steven T. Hatton | last post by:
The following may strike many of you as just plain silly, but it represents the kind of delelima I find myself in when trying to make a design decision. This really is a toy project written for...
3
by: Robert Rota | last post by:
Need help figuring out why my program core dumps and if I have the structures right. If you are interested in helping me I can send you a copy of the code. The program is supposed to mimic a 3...
9
by: werasm | last post by:
Hi all, What is the difference between: typedef struct { ... } MyS1; ....and...
6
by: Eric | last post by:
This IS material from a CS class on object oriented programming. It is NOT my homework. Consider the following: struct A {short i; void f () {cout << "A::f()\n";}}; struct B : A {long ...
21
by: Zytan | last post by:
Is it possible, as in C? I don't think it is. Just checking. Zytan
9
by: Carramba | last post by:
Hi! I want to have a struct representing a table, with int's and float. But I seem to get only stuckdump.. and not really sure why , hope you can help! Thanxk you in advance #include...
27
by: arkmancn | last post by:
Any comments? thanks. Jim
6
by: BIll Cunningham | last post by:
pg 128 of kandr2 speaks of a struct struct point{ int x; int y; }; I understand this but what is it saying about using intialize variables like such.
4
by: Lew Pitcher | last post by:
(having trouble getting my reply through - hopefully, third time is a charm) On November 11, 2008 19:53, in comp.lang.c, BIll Cunningham (nospam@nspam.invalid) wrote: Why an int? Because K&R...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.