5

I have a simple "Hello World!" c program, named hello.c on my desktop:

#include <stdio.h>

int main() {
    printf("Hello world!\n");

    return 0;
}

I run the following commands.

  • I pre-process it with : cpp hello.c > hello.i
  • I compile it with : gcc -S hello.i
  • I assemble it with : as -o hello.o hello.s

All good so far. But, i'm unable to link it. I've tried, among other commands, these:

ld -o hello.exe hello.o
ld -o hello.exe hello.o -lgcc
ld -o hello.exe hello.o -nostdlib -lgcc

Nothing works. The link errors i get in every single case are :

hello.o:hello.c:(.text+0x9): undefined reference to `__main'
hello.o:hello.c:(.text+0x15): undefined reference to `puts'

How can i link this assembled program hello.o in order to finally produce the executable program hello.exe? What am i missing? [Using Windows 8.1, Mingw version 0.6.2.] Thanks in advance.

KeyC0de
  • 4,728
  • 8
  • 44
  • 68
  • What does `gcc -o hello.exe hello.c` do? "Nothing works" is not a very useful description of an error situation. – tofro Sep 29 '16 at 10:39
  • gcc is the compiler. That command will do all the work to produce the executable. I was going step-by-step. Now i'm in the final step and i want to link with the linker (ld). – KeyC0de Sep 29 '16 at 10:42
  • Why are you linking? Is there an assembly file here? And what is your overall goal (To link the assembly and C Files ? ) ? – amanuel2 Sep 29 '16 at 10:44
  • @amanuel2: What does assembly have to do with any of this? You need to link object code, irrespective of the source language that produced it. That could well be C. – IInspectable Sep 29 '16 at 10:46
  • @amanuel2 I have mentioned what i want to do. I want to link hello.o, to produce the executable. Isn't that what the linker is supposed to do (after accumulating all the libs). By the way why the negative votes? – KeyC0de Sep 29 '16 at 10:46
  • @RestlessC0bra I'm going to say , I didn't downvote this question . Yeah , sorry i have been using linkers for combining C and Assembly files a lot lately , got confused :/ . – amanuel2 Sep 29 '16 at 10:51
  • I think this has the answer you need. http://stackoverflow.com/questions/32164478/when-using-ld-to-link-undefined-reference-to-main – Lucas Sep 29 '16 at 11:11
  • @LucasHarskamp Nope. No answer there works. It gives me the errors i provided above and more. Still trying to figure it out. – KeyC0de Sep 29 '16 at 11:17
  • you could use `gcc` instead of `ld` to link, it will supply the appropriate libraries – M.M Sep 29 '16 at 11:24
  • So to link we must type gcc hello.o -o hello.exe. It does supply all the libraries automatically. It's probably impossible to type them all by hand. – KeyC0de Sep 29 '16 at 15:28

2 Answers2

1

Even if your answers to clarification questions are not particularly useful:

Try something like

ld hello.o -lmsvcrt -entry=_main -subsystem=console -o hello.exe

If you want to see the linker command line the standard gcc uses, invoke gcc like so:

gcc test.c -o test -Wl,-v

The last lines output is what you should be using...

Clifford
  • 88,407
  • 13
  • 85
  • 165
tofro
  • 5,640
  • 14
  • 31
  • 2
    It *is* so complicated. If you'd use gcc, the compiler frontend, this would relieve you from that complications. Mingw is built to use the MSC runtimes. In my installation, libmsvcrt.a is in \MingW\lib - maybe you need to add a -L towards the dir where it is in yours. – tofro Sep 29 '16 at 11:28
  • See above edit in order to find out how the compiler frontend calls the linker. – tofro Sep 29 '16 at 12:41
1

If you want to compile something rather than experimenting with tools, don't link it using the linker directly. Use gcc, which will call the linker for you with the right options:

Compile step:

gcc -c hello.c

Link step:

gcc -o hello.exe hello.o

Or all in one:

gcc -o hello.exe hello.c

You can also use a simple Makefile, then run make:

all: hello.exe
Sam Watkins
  • 7,819
  • 3
  • 38
  • 38