473,800 Members | 2,368 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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;p 1.z=5;
p2.x=5;p1.y=2;p 1.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
39 3124
ri*****@cogsci. ed.ac.uk (Richard Tobin) writes:
In article <45************ ***********@new s.orange.fr>,
jacob.navia <ja***@jacob.re mcomp.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_Keit h) 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_Keit h) 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
"DanielJohn son" <di********@gma il.comwrites:
>Keith Thompson (The_Other_Keit h) 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_Keit h) 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.or gKeith Thompson <ks***@mib.orgw rites:
....
[ 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.or gKeith Thompson <ks***@mib.orgw rites:
...
[ 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.or g>,
Keith Thompson <ks***@mib.orgw rote:
>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
--
"Considerat ion 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.or g>,
Keith Thompson <ks***@mib.orgw rote:
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.re mcomp.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>gnuli nux<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.re mcomp.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

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

Similar topics

15
9082
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 the purpose of learning to work with C++. It therefore makes some sense for me to give the situation the amount of consideration presented below. To be quite honest, I'm amazed at the amount there is to say about such a seemingly simple...
3
647
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 level pagetable. Thank you. struct _PageTable { struct _PageTable **PageArray; unsigned int *FrameArray;
9
7210
by: werasm | last post by:
Hi all, What is the difference between: typedef struct { ... } MyS1; ....and...
6
359
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 j; void f () {cout << "B::f()\n";} void g () {cout << "B::g()\n";}}; { A* const a = new B; // dangerous (1)
21
10590
by: Zytan | last post by:
Is it possible, as in C? I don't think it is. Just checking. Zytan
9
1629
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 <stdio.h> #include <stdlib.h>
27
5521
by: arkmancn | last post by:
Any comments? thanks. Jim
6
368
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
1853
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 chose to use an int. Why not a double? Because K&R chose to use an int. Note the phrase "syntactically analogous". It means that the /syntax/ of
0
9691
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9551
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10505
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10276
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10035
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7580
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6813
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5471
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5606
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.