【发布时间】:2017-05-14 00:40:21
【问题描述】:
我正在尝试用 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(相同,但将gcc 替换为clang)。
问题:在这两种情况下./HelloC 都返回exec format error: ./HelloC。
我的设置
OSX:10.12.4
clang --version
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 --version
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
【问题讨论】:
-
为什么你的代码在
.h文件中? -
您是如何决定使用
chmod的?这不是您链接到的说明的一部分。 -
1) 代码应该驻留在
.c文件中,而不是.h。 2) 不带参数的函数应该看起来像func_name(void),以避免被任意数量的参数调用。 3) 你实际上应该return来自main的东西。 4) 你的编译器应该让输出文件自动执行——如果你不得不使用chmod,这是一个不好的迹象。 -
@Siguza:
main末尾的return 0;从 C99 开始是不必要的。 -
这与 gcc 无关。阅读消息,仅仅因为您使用命令
gcc并不意味着它是 gcc。 (将这种愚蠢的混乱归咎于 Apple!)