[英]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. 我很长时间以来第一次在编写Hello World C程序。 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. 运行MAC OS 10.13.6,我上周刚刚下载了XCode。 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. 我猜我需要添加一些编译器标记,但无法弄清楚哪个标记。
It sounds like you are just starting out in your journey into c code. 听起来您刚刚开始进入C代码之旅。 This is a great opportunity to learn about c, compiling, and linking.
这是学习c,编译和链接的绝佳机会。
What you have written can compile just fine into an object file (containing a function called Main()). 您编写的内容可以很好地编译为目标文件(包含一个称为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).
但是,要链接到可以在您的OS上运行的可执行文件,您需要定义一个入口点,该入口点被假定为一个称为main的函数(如上文所述,区分大小写)。
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. 查阅以下资源: http : //www.ntu.edu.sg/home/ehchua/programming/cpp/gcc_make.html ,以获取有关gcc编译器在幕后工作的解释。
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
. Main
需要是小写的main
。 int main()
will work, int main(void)
is technically more accurate (see void main(void) vs main() ) int main()
可以工作,但int main(void)
在技术上更准确(请参见void main(void)vs main() ) \\n
. \\n
。 (https://www.tutorialspoint.com/c_standard_library/c_function_printf.htm )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.