Reputation: 41
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
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;
}
Main
needs to be lowercase main
. int main()
will work, int main(void)
is technically more accurate (see
void main(void) vs main())\n
. (https://www.tutorialspoint.com/c_standard_library/c_function_printf.htm)Upvotes: 5