Linker prob

I am using gcc3.4 on solaris to compile my c++ codes.
At some places linker gives a prob as shown below:

Undefined first referenced
symbol in file
main /usr/local/lib/gcc/sparc-sun-solaris2.8/3.4.2/crt1.o

I made a search and found that someone suggests to put an empty main procedure. Well as far as I know its required for compiling c code. So what can be the prob here.

The command which I use is..._eprintf.o is something which I have extracted from libgcc.a...

gcc -g -O2 -Wall -I. -I/usr/local/include/c++/3.4.2 -Wno-deprecated -L/usr/local/lib test.cc -o test.o -lm -lc -lgcc -lstdc++ _eprintf.o
[731 byte] By [manish_velankani] at [2007-11-19 8:59:39]
# 1 Re: Linker prob
If you are creating an executable you need a main() procedure, even in C++.
NMTop40 at 2007-11-9 0:44:07 >
# 2 Re: Linker prob
I am creating a static library(libtest.a) using object file(test.o). I had a main in that before.
It did gave me a succesful completion.
When I used the lib(libtest.a) with other programs the linker showed that it cant read test.o in library and hence none of the links get resolved.

ld: warning: file /usr/local/lib/gcc/sparc-sun-solaris2.8/3.4.2/../../../libtest.a(test.o): unknown file type
manish_velankani at 2007-11-9 0:45:07 >
# 3 Re: Linker prob
There is one interesting thing i noticed:

gcc 3.4 has libgcc.a installed in /usr/local/lib. On dissecting libgcc.a I can see _eprintf.o. But what I did is give a command with lgcc option.

gcc -O2 -Wall -I. -I/usr/local/include/c++/3.4.2 -Wno-deprecated -L/usr/local/lib test1.cc -o test1.o -ltest -lstdc++ -lgcc

It gives me the error as shown below

ld: warning: file /usr/local/lib/libtest.a(test.o): unknown file type
Undefined first referenced
symbol in file
String::from(int) /var/tmp//ccOOWkZ1.o
String::String(char const*) /var/tmp//ccOOWkZ1.o
String::~String() /var/tmp//ccOOWkZ1.o
__eprintf /usr/local/lib/libstdc++.so
ld: fatal: Symbol referencing errors. No output written to test.o
collect2: ld returned 1 exit status

Now if I give the command with _eprintf.o that has been extracted from libgcc.a, it doesnt show error of _eprintf.

gcc -O2 -Wall -I. -I/usr/local/include/c++/3.4.2 -Wno-deprecated -L/usr/local/lib test1.cc -o test1.o -ltest -lstdc++ _eprintf.o
ld: warning: file /usr/local/lib/libtest.a(test.o): unknown file type
Undefined first referenced
symbol in file
String::from(int) /var/tmp//cclJVsf8.o
String::String(char const*) /var/tmp//cclJVsf8.o
String::~String() /var/tmp//cclJVsf8.o
ld: fatal: Symbol referencing errors. No output written to test.o
collect2: ld returned 1 exit status

Same thing if i try to do with test.o instead of using libtest.o there is no success.
manish_velankani at 2007-11-9 0:46:04 >
# 4 Re: Linker prob
When I look at the command you are using, I can not see any option to tell the compiler that you are creating a static library (the command you are using is to create an executable ). That is why you get undefined external references.
I guess the easiest way to a static lib. is to compile the sources to .o with gcc and use ld to create the library afterwards.
Kurt
ZuK at 2007-11-9 0:47:03 >
# 5 Re: Linker prob
I was creating library using ar command.

Nevertheless I now compiled the code test.cc using -fPIC and then created a shared library test.so using gcc -shared. Then I used the library for reference and it worked.

What I have heard is that if I have static and dynamic library in /usr/local/lib. And if I have linked to some shared library the compiler will search for shared lib for all the options. I had also used -lm -lc -lstdc++ -lgcc, all of them have .so as well as .a. But libtest had only .a. So probably compiler was not able to pick up the format of libtest.a since it was picking up .so for rest. Now when I converted it to shared object then the compiler did resolved all.

Ne suggestions on this feature of gcc coz I am not sure. Its hit n trial ;-)
manish_velankani at 2007-11-9 0:48:13 >