Page 1 of 3 123 LastLast
Results 1 to 10 of 26

Thread: stupid gcc & ld problem

  1. #1
    Join Date
    Apr 2007
    Beans
    8

    stupid gcc & ld problem

    Code:
    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...

  2. #2
    Join Date
    Oct 2004
    Beans
    1,532

    Re: stupid gcc & ld problem

    Use gcc again, instead of ld:
    Code:
    $ gcc -o hello2 hello2.o

  3. #3
    Join Date
    Aug 2006
    Beans
    1,225

    Re: stupid gcc & ld problem

    GCC is doing much more than most people think on an Ubuntu System. For example, it typically links with ld using something like this:
    Code:
    /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.

  4. #4
    Join Date
    Apr 2007
    Beans
    8

    Re: stupid gcc & ld problem

    Quote Originally Posted by WW View Post
    Use gcc again, instead of ld:
    Code:
    $ 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 ?

  5. #5
    Join Date
    Apr 2007
    Location
    Buenos Aires - Argentina
    Beans
    43
    Distro
    Ubuntu 7.04 Feisty Fawn

    Re: stupid gcc & ld problem

    The following command line should link your program without warnings and/or unresolved references:

    Code:
    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.
    Last edited by DoubleQuadword; April 12th, 2007 at 12:29 AM.
    Free software, free society. - Richard M. Stallman.

  6. #6
    Join Date
    Aug 2006
    Beans
    1,225

    Re: stupid gcc & ld problem

    Quote Originally Posted by odradek View Post
    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.

  7. #7
    Join Date
    Oct 2006
    Location
    Austin, Texas
    Beans
    2,712
    Distro
    Ubuntu 7.10 Gutsy Gibbon

    Re: stupid gcc & ld problem

    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?

  8. #8
    Join Date
    Apr 2007
    Beans
    8

    Re: stupid gcc & ld problem

    Quote Originally Posted by DoubleQuadword View Post
    The following command line should link your program without warnings and/or unresolved references:

    Code:
    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 :

    Code:
    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.

  9. #9
    Join Date
    Apr 2007
    Beans
    8

    Re: stupid gcc & ld problem

    Quote Originally Posted by Wybiral View Post
    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.

  10. #10
    Join Date
    Jul 2005
    Location
    Northern CA
    Beans
    634
    Distro
    Ubuntu 13.04 Raring Ringtail

    Re: stupid gcc & ld problem

    Quote Originally Posted by odradek View Post
    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,
    Code:
    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.)
    Intel i7-920; Nvidia GT 220, 1GB; MSI X58 Pro-E; 6GB DDR; 64-bit mode.

Page 1 of 3 123 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •