简体   繁体   中英

Writing first c program in vi editor in cygwin

I [extremely] new to c programming and cygwin so thanks for being patient. I am using a PDF of Michael Vine's C programming for beginners and trying to enter and compile the first example.

Here is what i wrote in vi:

 #include <stdio.h>

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

When I try to compile using gcc , I get two errors:

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

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. Am I missing a package or is my path to cygwin screwed up? 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. Since you named your source file new , GCC is assuming it's a compiled object and is trying to link it. Either rename it new.c or pass the -xc flag when compiling.

For future reference, a good way to debug funny business with the GCC compiler driver is to pass the -v flag. 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:

'int main(){ ... }'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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