在Mac终端中编译简单代码

时间:2017-03-18 09:45:38

标签: c++ macos terminal

我正在尝试在Terminal中编译简单的程序,如下所示:

#include <stdio.h>
int main()
{
int a;
printf("Enter length of square:");
scanf("%d",&a);
printf("Perimeter of your square is:%d\n", 4*a);
return 0;
}

将其保存在主目录中的文件begin1.txt中,然后键入Terminal cc begin1.txt,如下所示:

ld: warning: ignoring file begin1.txt, file was built for unsupported file format ( 0x23 0x69 0x6E 0x63 0x6C 0x75 0x64 0x65 0x20 0x3C 0x73 0x74 0x64 0x69 0x6F 0x2E ) which is not the architecture being linked (x86_64): begin1.txt
Undefined symbols for architecture x86_64:
  "_main", referenced from:
     implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

  

ld:警告:忽略文件begin1.txt 文件是为不支持的文件格式构建的 (0x23 0x69 0x6E 0x63 0x6C 0x75 0x64 0x65 0x20 0x3C 0x73 0x74 0x64 0x69 0x6F 0x2E) 这不是架构 被链接 (x86_64): begin1.txt

您的编译器发出的错误声明指定您的文件使用.txt文件扩展名保存,这使其成为文本文件,而不是C ++文件。您需要将文件名更改为以.cpp文件扩展名结尾:

 begin1.cpp