繁体   English   中英

用Cygwin在vi编辑器中编写第一个C程序

[英]Writing first c program in vi editor in cygwin

我对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 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的道路搞砸了? 有人知道这是怎么回事吗?

无论好坏,GCC都会使用传递给它的文件名来确定要执行的操作-例如运行编译器,汇编器,链接器或某种组合。 由于您将源文件命名为new ,因此GCC假定它是已编译的对象,并正在尝试链接它。 在编译时将其重命名为new.c或通过-xc标志。

为了将来参考,使用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

一个基本的语法错误:

 ' main(){ ... } '

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

'int main(){ ... }'

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM