SteveTake2
SteveTake2

Reputation: 41

Unable to compile Hello World

Sorry if this is a repeat question, but I couldn't find an answer that worked.

I'm writing a Hello World C program for the first time in a long time. I'm fairly certain the code is right, but it won't compile.

Running MAC OS 10.13.6 and I just downloaded XCode last week. The program compiles into an object file using

cc -c test.c -o test.o

without a problem. However, I can't create an executable using

cc test.o -o test

Here's the code:

#include <stdio.h>
int Main()
{
    printf("Hello World");
    return 0;
}

When I go to create the executable, I get

Undefined symbols for architecture x86_64:
  "_main", referenced from:
     implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64

I'm guessing I need to add some compiler flags but can't figure out which ones.

Upvotes: 1

Views: 407

Answers (1)

James McAnanama
James McAnanama

Reputation: 121

It sounds like you are just starting out in your journey into c code. This is a great opportunity to learn about c, compiling, and linking.

What you have written can compile just fine into an object file (containing a function called Main()). However, to link into an executable that can run from your OS, you need to define an entry point which is assumed to be a function called main (case sensitive as John mentioned above).

Check out this resource: http://www.ntu.edu.sg/home/ehchua/programming/cpp/gcc_make.html for an explanation of what the gcc compiler gets up to behind the scenes.

You just need to make a couple of small changes to get your code to work as expected:

#include <stdio.h>

int main(void) {
    printf("Hello, world!\n");
    return 0;
}

Upvotes: 5

Related Questions