linking error

G

geek

Hi all,

I am trying to create a global structure and want to use it in other
file.
Here is the file that contains the global structure.

typedef struct {
int a;
int b;
}test;

When I try to compile it with gcc I get linker error:
Undefined first referenced
symbol in file
main
/usr/global/lib/gcc-lib/sparc-sun-solaris2.5.1/2.95.2/crt1.o
ld: fatal: Symbol referencing errors. No output written to a.out
collect2: ld returned 1 exit status

Can somebody please help me with this.
Thanks in advance.

-Metrri
 
F

Fred Kleinschmidt

geek said:
Hi all,

I am trying to create a global structure and want to use it in other
file.
Here is the file that contains the global structure.

typedef struct {
int a;
int b;
}test;

When I try to compile it with gcc I get linker error:
Undefined first referenced
symbol in file
main
/usr/global/lib/gcc-lib/sparc-sun-solaris2.5.1/2.95.2/crt1.o
ld: fatal: Symbol referencing errors. No output written to a.out
collect2: ld returned 1 exit status

Can somebody please help me with this.
Thanks in advance.

-Metrri

file1.h:
#ifndef file1_h_included
#define file_1_included
typedef struct {
int a;
int b;
}test;
#endif


file2.c:
#include "file1.h"
/* other code here */
 
K

Keith Thompson

geek said:
I am trying to create a global structure and want to use it in other
file.
Here is the file that contains the global structure.

typedef struct {
int a;
int b;
}test;

Presumably that's a header file. (If it isn't, it probably should be.)
When I try to compile it with gcc I get linker error:
Undefined first referenced
symbol in file
main

Of course you do; you don't have a main() function.

Are you trying to compile a header file? Don't do that. Headers
(usually .h files) are #included in other .h files and/or in .c files.
You compile .c files.

Many compilers, including gcc, assume by default that you're compiling
something with a main function, so they invoke the linker for you. If
you have nothing to link, don't invoke the linker. <OT>gcc -c</OT>
 
G

geek

The help that Fred provided doesn't help either.

This is what he suggested.


file1.h:
#ifndef file1_h_included
#define file_1_included
typedef struct {
int a;
int b;
}test;

#endif

file2.c:
#include "file1.h"
/* other code here */

Thanks!
 
F

Flash Gordon

geek said:
Hi all,

I am trying to create a global structure and want to use it in other
file.
Here is the file that contains the global structure.

typedef struct {
int a;
int b;
}test;

Apart from the fact that many people consider using a typedef for a
struct bad style, you should put the definition of shared struct's and
#defined together with prototypes for external functions in header files
and #include them from the C source files that need them. This should be
covered in your text book. If you don't have a text book, but a copy of
K&R2 and read the comp.lang.c FAQ (which tells you what K&R2 is.
When I try to compile it with gcc I get linker error:
Undefined first referenced
symbol in file
main
/usr/global/lib/gcc-lib/sparc-sun-solaris2.5.1/2.95.2/crt1.o
ld: fatal: Symbol referencing errors. No output written to a.out
collect2: ld returned 1 exit status

Can somebody please help me with this.

You also need to learn to use your compiler. Details of how specific
tool chains work are not topical here, but "info gcc" should get you
started. The group gnu.gcc.help may also be of use for help on driving gcc.
 
F

Flash Gordon

geek said:
Thanks, but that doesn't work either.

What doesn't help? Learn to quote, Google isn't Usenet and there is no
guarantee that everyone receives all the messages in a thread. See the
URL in my sig for information about this group including pointer to how
to reply properly using Google.
 
F

Fred Kleinschmidt

geek said:
Thanks, but that doesn't work either.

What doesn't work?

Show us a complete, short, code file that exhibits the problem, and tell us
what you mean by "it doesn't work"
 
I

Ivanna Pee

Flash said:
Apart from the fact that many people consider using a typedef for a
struct bad style, you should put the definition of shared struct's and

What? "typedef struct" is bad style? I have never heard of such
nonsense.
 
K

Keith Thompson

geek said:
Thanks Gordon for the help.
I really appreciate it.

"geek":

You have been told at least twice in this thread that you need to
provide context when you post a followup. The groups.google.com
interface is badly broken in this regard, but there is a workaround.
<http://cfaj.freeshell.org/google/> explains how and why to do this.
Please read it.
 
C

CBFalconer

geek said:
Thanks Gordon for the help.
I really appreciate it.

I think you will get very little more help around here if you don't
smarten up and provide proper context.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>
 
B

Barry Schwarz

Hi all,

I am trying to create a global structure and want to use it in other
file.
Here is the file that contains the global structure.

typedef struct {
int a;
int b;
}test;

This is not the definition of a structure object. It is the
declaration of the structure type.
When I try to compile it with gcc I get linker error:

Show us the code you compiled.
Undefined first referenced
symbol in file
main
/usr/global/lib/gcc-lib/sparc-sun-solaris2.5.1/2.95.2/crt1.o
ld: fatal: Symbol referencing errors. No output written to a.out
collect2: ld returned 1 exit status


Remove del for email
 

Ask a Question

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

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

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top