C HelloWorld exec 格式错误

标签 c macos clang

我正在尝试用 C 语言编译并运行我的第一个代码。

我用了this code :

HelloC.h

#include<stdio.h>

// added int to prevent warning
int main()
{
    printf("Hello World");

}

我尝试使用 these instructions 进行编译...

gcc -Wall HelloC.h -o HelloC
chmod +x HelloC

...和this top answer (同样的事情,但用 clang 替换 gcc)。

问题:在这两种情况下./HelloC都会返回exec格式错误:./HelloC

我的设置

OSX:10.12.4

clang --版本

Apple LLVM version 8.1.0 (clang-802.0.42)
Target: x86_64-apple-darwin16.5.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

gcc --版本

Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 8.1.0 (clang-802.0.42)
Target: x86_64-apple-darwin16.5.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

最佳答案

感谢@ScottHunter 和@Siguza

我需要使用 .c 而不是 .h 作为文件扩展名。

关于C HelloWorld exec 格式错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43959323/

相关文章:

git - 在 Mac 上安装 gitk

macos - 基本 OS X 程序集和 Mach-O 格式

c++ - 卡特琳娜 C++ : Using <cmath> headers yield error: no member named 'signbit' in the global namespace

llvm - 如何告诉 clang 不要将寄存器保存到堆栈?

c++ - 在 Windows 上使用 clang 链接 msvc 内在函数的问题

c - 标准是否要求 EOF 为负数?

c++ - 如何在编译时或代码初始化时自动更新数组中的条目?

c - 使用 fopen 时出现段错误

c - C 中的类型转换是如何实现的?

iphone - 我有一个在 MAC OS 上使用 XCode 开发的项目,我如何在 Linux 上构建它?