简体   繁体   English

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

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

I [extremely] new to c programming and cygwin so thanks for being patient. 我对C编程和cygwin非常陌生,非常感谢您耐心等待。 I am using a PDF of Michael Vine's C programming for beginners and trying to enter and compile the first example. 我正在使用面向初学者的Michael Vine C编程PDF,并尝试输入和编译第一个示例。

Here is what i wrote in vi: 这是我在vi中写的:

 #include <stdio.h>

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

When I try to compile using gcc , I get two errors: 当我尝试使用gcc进行编译时,出现两个错误:

1) usr/lib/gcc/i686-pc-cygwin/3.4.4./... /bin/ld:new: file format not recognized; treating as linked script 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 2) [same path as above]/bin/ld:new:11: syntax error collect2: ld returned 1 exit status

I'm pretty sure the actual syntax I used in vi is correct (its straight out of an example) and the gcc command is also correct. 我很确定我在vi中使用的实际语法是正确的(直接出于示例),并且gcc命令也是正确的。 Am I missing a package or is my path to cygwin screwed up? 我是否缺少包裹,或者我通往Cygwin的道路搞砸了? Anyone know what's going on with this? 有人知道这是怎么回事吗?

GCC, for better or for worse, uses the filename you pass to it to figure out what operation to do - run the compiler, the assembler, or the linker, or some combination, for example. 无论好坏,GCC都会使用传递给它的文件名来确定要执行的操作-例如运行编译器,汇编器,链接器或某种组合。 Since you named your source file new , GCC is assuming it's a compiled object and is trying to link it. 由于您将源文件命名为new ,因此GCC假定它是已编译的对象,并正在尝试链接它。 Either rename it new.c or pass the -xc flag when compiling. 在编译时将其重命名为new.c或通过-xc标志。

For future reference, a good way to debug funny business with the GCC compiler driver is to pass the -v flag. 为了将来参考,使用GCC编译器驱动程序调试有趣的业务的一种好方法是传递-v标志。 If you do so for your original command line you'll see that it just invokes the linker, skipping the compilation step. 如果对原始命令行执行此操作,则会看到它只是调用链接器,而跳过了编译步骤。 An example from my machine: 我机器上的一个例子:

$ 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

One basic syntax error: 一个基本的语法错误:

 ' main(){ ... } '

In C/C++, your main function should be: 在C / C ++中,您的主要功能应为:

'int main(){ ... }'

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

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