簡體   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