我正在尝试用 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/