PDA

View Full Version : stupid gcc & ld problem



odradek
April 11th, 2007, 10:53 PM
jimihex@b0x:~/devel/C$ ls
hello.c
jimihex@b0x:~/devel/C$ cat hello.c
#include <stdio.h>
int main () {
printf("Hello world\n");
getchar();
return 0;
}
jimihex@b0x:~/devel/C$ gcc -o hello hello.c
jimihex@b0x:~/devel/C$ ls
hello hello.c
jimihex@b0x:~/devel/C$ ./hello
Hello world

jimihex@b0x:~/devel/C$ gcc -c -o hello2.o hello.c
jimihex@b0x:~/devel/C$ ld -o hello2 hello2.o
ld: warning: cannot find entry symbol _start; defaulting to 0000000008048094
hello2.o: In function `main':hello.c:(.text+0x24): undefined reference to `puts'
:hello.c:(.text+0x29): undefined reference to `getchar'
jimihex@b0x:~/devel/C$


Help, please...

WW
April 11th, 2007, 11:06 PM
Use gcc again, instead of ld:


$ gcc -o hello2 hello2.o

phossal
April 11th, 2007, 11:08 PM
GCC is doing much more than most people think on an Ubuntu System. For example, it typically links with ld using something like this:


/usr/lib/gcc/i486-linux-gnu/4.1.2/collect2 --eh-frame-hdr -m elf_i386 -dynamic-linker /lib/ld-linux.so.2 -oS /usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../lib/crt1.o /usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../lib/crti.o /usr/lib/gcc/i486-linux-gnu/4.1.2/crtbegin.o -L/usr/lib/gcc/i486-linux-gnu/4.1.2 -L/usr/lib/gcc/i486-linux-gnu/4.1.2 -L/usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../lib -L/lib/../lib -L/usr/lib/../lib S.o -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed /usr/lib/gcc/i486-linux-gnu/4.1.2/crtend.o /usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../lib/crtn.o
You can always compile using the -v (verbose) flag to see everything gcc is doing to your files.

Regards.

odradek
April 12th, 2007, 12:14 AM
Use gcc again, instead of ld:


$ gcc -o hello2 hello2.o


Yes, I know that. I need ld for some other purposes, so I want to know what is going on during the linking.

phossal, thank you a lot, this helped, but I still didn't find with what arguments I should use ld for right linking. Any sugestions ?

DoubleQuadword
April 12th, 2007, 12:23 AM
The following command line should link your program without warnings and/or unresolved references:


ld hello2.o -o hello2 -lc --entry main

Note that -lc tells the compiler to use the standard C library and --entry main tells the compiler to use the address of 'main' as the application's entry point.

Regards.

phossal
April 12th, 2007, 12:35 AM
Yes, I know that. I need ld for some other purposes, so I want to know what is going on during the linking.

phossal, thank you a lot, this helped, but I still didn't find with what arguments I should use ld for right linking. Any sugestions ?

In the example I posted earlier, I highlighted my object file - it's S.o - in blue. Everything else was included by gcc. I had prepared S.o manually using GAS. It was simply a "Hello, World!" program.

DoubleQuadWord may have correctly identified the minimal things necessary for linking, but if that fails, you should be able to substitute your object file in place of S.o and get it to work.

I'm stuck playing in Windows, so I can't see what does/doesn't work like I normally would. Sorry.

Wybiral
April 12th, 2007, 12:54 AM
I'm curious what you need ld for... ld is just a linker, it doesn't detect C's "main" as the starting point.

As DoubleQuadword showed, you can link to the C library... But if you're linking to the C library, why use ld and not GCC?

odradek
April 12th, 2007, 01:14 AM
The following command line should link your program without warnings and/or unresolved references:


ld hello2.o -o hello2 -lc --entry main

Note that -lc tells the compiler to use the standard C library and --entry main tells the compiler to use the address of 'main' as the application's entry point.

Regards.

Thank you. This works, but, something strange is going on :



jimihex@b0x:~/devel/C$ ld -lc --entry main -o hello2 hello2.o
jimihex@b0x:~/devel/C$ ls
hello2 hello2.o hello.c
jimihex@b0x:~/devel/C$ ./hello2
bash: ./hello2: No such file or directory
jimihex@b0x:~/devel/C$


phossal, I saw that too with "-v" (Sorry because of my poor english), but, are all those arguments necessary ? On Windows, with DJGPP this works fine, without problems. Btw, thanks a lot for helping me. :)

odradek
April 12th, 2007, 01:27 AM
I'm curious what you need ld for... ld is just a linker, it doesn't detect C's "main" as the starting point.

As DoubleQuadword showed, you can link to the C library... But if you're linking to the C library, why use ld and not GCC?

I am working on some small OS development project and I need to link several object files into one flat binary. But, I am using Ubuntu only for a few months, so, it's little hard to adjust.

rplantz
April 12th, 2007, 03:09 AM
I am working on some small OS development project and I need to link several object files into one flat binary. But, I am using Ubuntu only for a few months, so, it's little hard to adjust.

You can include your object files, assuming they are ELF format, on the same line with your C/C++ files. For example,


gcc myProg.c sub1.c sub2.o -o myProg

I do this often when I write functions in assembly language. I first assemble them, giving their respective object files. Then I use gcc to compile the main function (which is written in C) and link it with the already assembled object files. gcc is smart enough to know whether it has to compile the function or not. Then it goes on to the ld phase, automatically linking in the required C libraries.

(I do not show various options you may wish to use in my command above.)

Wybiral
April 12th, 2007, 11:01 AM
You can include your object files, assuming they are ELF format, on the same line with your C/C++ files. For example,


gcc myProg.c sub1.c sub2.o -o myProg



That's also pretty standard for C when you're working on something that requires more then one source file. Just compile them all with the "-c" flag (as you seem to already know)...



gcc file1.c -c
gcc file2.c -c
gcc file3.c -c


Then you link them and whatever libraries you're using...



gcc file1.o file2.o file3.o -lSomeLibrary -o program


And thats it. I doubt you would want to add the linker to any build system (makefile or whatever) so using GCC to do all of it is usually the way to go.

odradek
April 12th, 2007, 02:01 PM
How can I use ld script in linking with GCC ?


ld -T link.ld -o final file1.o file2.o

Wybiral
April 12th, 2007, 02:10 PM
What do you need a linker script for?

odradek
April 12th, 2007, 02:18 PM
I was trying to link with "-Xlinker -T link.ld", but, it still doesn't work. I need it because it's not a usualy linking, so I need to define sections and some other parameters by myself. (Once again, I am sorry because of my poor english)

rplantz
April 12th, 2007, 10:20 PM
I have worked with a Motorola 68k single-board computer that had no OS. We developed the code under linux, then loaded it onto the board. We used an ld script to link in our own libraries.

We learned how to do this by reading info for ld.

I don't mean this in the "rtfm" sense, but only you know what you need to do, so it probably makes more sense for you to try to figure it out from info. It's been several years since I have done this, so I would need to consult info myself in order to answer any questions you might have. :)

thornbird
September 13th, 2010, 04:07 PM
jimihex@b0x:~/devel/C$ ls
hello.c
jimihex@b0x:~/devel/C$ cat hello.c
#include <stdio.h>
int main () {
printf("Hello world\n");
getchar();
return 0;
}
jimihex@b0x:~/devel/C$ gcc -o hello hello.c
jimihex@b0x:~/devel/C$ ls
hello hello.c
jimihex@b0x:~/devel/C$ ./hello
Hello world

jimihex@b0x:~/devel/C$ gcc -c -o hello2.o hello.c
jimihex@b0x:~/devel/C$ ld -o hello2 hello2.o
ld: warning: cannot find entry symbol _start; defaulting to 0000000008048094
hello2.o: In function `main':hello.c:(.text+0x24): undefined reference to `puts'
:hello.c:(.text+0x29): undefined reference to `getchar'
jimihex@b0x:~/devel/C$


Help, please...
I also have this problem!!do you resolve this question?if you resolve it,could you tell me what to do? thanks~~

spjackson
September 13th, 2010, 04:42 PM
jimihex@b0x:~/devel/C$ ld -o hello2 hello2.o
ld: warning: cannot find entry symbol _start; defaulting to 0000000008048094
hello2.o: In function `main':hello.c:(.text+0x24): undefined reference to
`puts':hello.c:(.text+0x29): undefined reference to `getchar'
I also have this problem!!do you resolve this question?if you resolve it,could you tell me what to do? thanks~~
Why are you linking by calling ld directly? Please explain why you need to do this. Linking by using gcc will do the right thing.

jimihex@b0x:~/devel/C$ gcc -o hello2 hello2.oIf you must use ld directly then you will have to explicitly include the C runtime library.

van7hu
October 29th, 2010, 03:44 AM
Thank you. This works, but, something strange is going on :



jimihex@b0x:~/devel/C$ ld -lc --entry main -o hello2 hello2.o
jimihex@b0x:~/devel/C$ ls
hello2 hello2.o hello.c
jimihex@b0x:~/devel/C$ ./hello2
bash: ./hello2: No such file or directory
jimihex@b0x:~/devel/C$
phossal, I saw that too with "-v" (Sorry because of my poor english), but, are all those arguments necessary ? On Windows, with DJGPP this works fine, without problems. Btw, thanks a lot for helping me. :)

Has anyone solved this problem ?

NathanB
October 29th, 2010, 04:08 AM
Has anyone solved this problem ?

Yes. Some versions of 'ld' look for the wrong version of stdlib, so you must tell it to use "/lib/ld-linux.so.2" instead.

van7hu
July 2nd, 2011, 06:12 AM
Yes. Some versions of 'ld' look for the wrong version of stdlib, so you must tell it to use "/lib/ld-linux.so.2" instead.

What did you really do?

and more,


van7hu@van7hu-laptop:~/C$ ldd main
linux-gate.so.1 => (0x00776000)
libc.so.6 => /lib/tls/i686/cmov/libc.so.6 (0x00d29000)
/usr/lib/libc.so.1 => /lib/ld-linux.so.2 (0x00689000)
van7hu@van7hu-laptop:~/C$ whereis libc
libc: /usr/lib/libc.so /usr/lib/libc.a /usr/share/man/man7/libc.7.gz

RintheGreat
May 1st, 2012, 09:54 AM
Hi everyone,
My gcc can not compile any file even the simplest hello.c one.
It worked just fine one Ubuntu 11.10 but when I've upgraded to 12.04 it cannot compile anything.
It said something about:

/usr/local/bin/ld: this linker was not configured to use sysroots
collect2: ld returned 1 exit status

Can anyone show me how to fix this problem???

Zugzwang
May 2nd, 2012, 08:57 AM
Hi everyone,
/usr/local/bin/ld: this linker was not configured to use sysroots
collect2: ld returned 1 exit status


First of all, check that you have the package "build-essential" installed. Then, you appear to have installed a custom version of the linker into "/usr/local/bin". Would you mind telling us why? Of course, you could just wipe everything from /usr/local, and this problem would go away, but there is probably a reason why you installed the custom linker there in the first place, so just removing stuff from "/usr/local" would surely break something else on the machine.

RintheGreat
May 2nd, 2012, 10:50 AM
I don't for sure. But the system worked just fine before I upgraded it. And I did not install anything strange after upgrading.

I search on the Internet I saw that this problem relates to many compilers but there is no result that relates to gcc.

Zugzwang
May 3rd, 2012, 09:58 AM
I don't for sure. But the system worked just fine before I upgraded it. And I did not install anything strange after upgrading.

The point is that you installed some stuff bypassing the package manager before you eventually upgraded. After the upgrade, the old stuff is no longer compatible. It's like with a car: if you installed some tweak to your car, and then change the car, but keep the tweak, it isn't guaranteed to still work.

mprat
April 5th, 2013, 03:47 PM
I find this entire thread a little bit sad - nowhere is a mention of a fix to the original problem and there is only ad-hominem on the original poster.

If /usr/local/bin is the custom linker path that is installed, how do you change it back to the default linker path?

codemaniac
April 5th, 2013, 05:10 PM
Really really old thread. Closed.

Please feel free to start afresh.