【问题标题】:Compiling simple code in Mac Terminal在 Mac 终端中编译简单代码
【发布时间】:2017-03-18 09:45:38
【问题描述】:

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

#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)

我该如何解决这个问题?

【问题讨论】:

  • 如果你使用.cpp而不是.txt,编译器会知道它是什么类型的文件?

标签: c++ macos terminal


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-12
    • 1970-01-01
    • 2017-06-09
    • 2014-12-16
    • 1970-01-01
    • 2015-11-18
    • 1970-01-01
    相关资源
    最近更新 更多