1

我对 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: file format not recognized; treating as linked script

2)[same path as above]/bin/ld:new:11: syntax error collect2: ld returned 1 exit status

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

4

2 回答 2

1

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
于 2013-06-23T03:15:13.540 回答
-1

一个基本的语法错误:

 ' main(){ ... } '

在 C/C++ 中,您的主要功能应该是:

'int main(){ ... }'
于 2017-04-09T20:39:53.207 回答