c - 在 cygwin 的 vi 编辑器中编写第一个 c 程序

标签 c gcc cygwin printf vi

我对 c 编程和 cygwin [非常] 陌生,所以感谢您的耐心等待。我正在使用 Michael Vine 的 C 编程初学者的 PDF,并尝试输入和编译第一个示例。

这是我在 vi 中写的:

 #include <stdio.h>

main ()
{
printf("\nHello World\n");
}

当我尝试使用 gcc 进行编译时,出现两个错误:

1) usr/lib/gcc/i686-pc-cygwin/3.4.4./.../bin/ld:new: 文件格式无法识别;视为链接脚本

2) [同上路径]/bin/ld:new:11: syntax error collect2: ld returned 1 exit status

我很确定我在 vi 中使用的实际语法是正确的(直接来自示例)并且 gcc 命令也是正确的。我错过了一个包裹还是我通往 cygwin 的道路搞砸了?有人知道这是怎么回事吗?

最佳答案

无论好坏,GCC 使用您传递给它的文件名来确定要执行的操作 - 例如,运行编译器、汇编器或链接器,或某些组合。由于您将源文件命名为 new,GCC 假设它是一个编译对象并尝试链接它。将其重命名为 new.c 或在编译时传递 -x c 标志。

为了将来引用,使用 GCC 编译器驱动程序调试有趣业务的一个好方法是传递 -v 标志。如果您对原始命令行执行此操作,您将看到它只是调用链接器,跳过了编译步骤。我机器上的一个例子:

$ gcc -v new -o new.exe
Using built-in specs.
Target: i686-apple-darwin11
Configured with: /private/var/tmp/llvmgcc42/llvmgcc42-2336.11~182/src/configure --disable-checking --enable-werror --prefix=/Applications/Xcode.app/Contents/Developer/usr/llvm-gcc-4.2 --mandir=/share/man --enable-languages=c,objc,c++,obj-c++ --program-prefix=llvm- --program-transform-name=/^[cg][^.-]*$/s/$/-4.2/ --with-slibdir=/usr/lib --build=i686-apple-darwin11 --enable-llvm=/private/var/tmp/llvmgcc42/llvmgcc42-2336.11~182/dst-llvmCore/Developer/usr/local --program-prefix=i686-apple-darwin11- --host=x86_64-apple-darwin11 --target=i686-apple-darwin11 --with-gxx-include-dir=/usr/include/c++/4.2.1
Thread model: posix
gcc version 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.11.00)
 /usr/llvm-gcc-4.2/bin/../libexec/gcc/i686-apple-darwin11/4.2.1/collect2 -dynamic -arch x86_64 -macosx_version_min 10.8.4 -weak_reference_mismatches non-weak -o new.exe -lcrt1.10.6.o -L/usr/llvm-gcc-4.2/bin/../lib/gcc/i686-apple-darwin11/4.2.1/x86_64 -L/Applications/Xcode.app/Contents/Developer/usr/llvm-gcc-4.2/lib/gcc/i686-apple-darwin11/4.2.1/x86_64 -L/usr/llvm-gcc-4.2/bin/../lib/gcc/i686-apple-darwin11/4.2.1 -L/usr/llvm-gcc-4.2/bin/../lib/gcc -L/Applications/Xcode.app/Contents/Developer/usr/llvm-gcc-4.2/lib/gcc/i686-apple-darwin11/4.2.1 -L/usr/llvm-gcc-4.2/bin/../lib/gcc/i686-apple-darwin11/4.2.1/../../.. -L/Applications/Xcode.app/Contents/Developer/usr/llvm-gcc-4.2/lib/gcc/i686-apple-darwin11/4.2.1/../../.. new -lSystem -lgcc -lSystem
ld: warning: ignoring file new, file was built for unsupported file format ( 0x20 0x23 0x69 0x6e 0x63 0x6c 0x75 0x64 0x65 0x20 0x3c 0x73 0x74 0x64 0x69 0x6f ) which is not the architecture being linked (x86_64): new
Undefined symbols for architecture x86_64:
  "_main", referenced from:
      start in crt1.10.6.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status

关于c - 在 cygwin 的 vi 编辑器中编写第一个 c 程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17257344/

相关文章:

c - 我正在做一个混合 2 个单词的(字符串)函数,但程序根本不工作

c++ - 交互设备的定义

c - 融合乘加和默认舍入模式

c - 如果 exec 失败,则终止子进程

c - 释放结构数组的元素?

c - 构建 C 代码的技巧/资源?

c++ - g++ 在 freebsd 上包含路径

linux - GCC是否支持命令文件

regex - grep:匹配文字 "+"

c - C-函数定义具有用于参数的指针,在调用时传递的变量不是指针,而是编译和运行(有时)。怎么样?