Thread: Cygwin not compilling correctly

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    25

    Cygwin not compilling correctly

    Hello,

    cygwin has decided to not compile any code for example:

    Code:
    #include <stdio.h>      
    #include <stdlib.h>   
    #include <string.h> 
    main()
    {
        printf("hello");
    
    }

    Code compiles but with:
    g++ -o file -c file.c

    creates a file which doesnt seem to have a file type as it not recognised when I try and execute from cmd.

    g++ -o file.exe -c file.c
    Makes a .exe file which when I try and execute from the cmd says access is denied.


    It was working before and I've tried with various .c files. cygwin1.dll is in the directory.

    Any ideas?

    Thanks.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    gcc is doing EXACTLY what you are telling it to do. The -c switch says "compile only" - meaning "do not link to a final executable, just form the intermediate object file of this source file on it's own".

    To produce a full executable file, your source file needs to be linked together with the C runtime library, since your source file does not have all the components needed to get a C program to run (even if you do not call ANY other functions than what is in the source - which in itself is fairly unlikely - you probably want to use printf to display something, for example).

    Remove the -c switch, and for this simple case all will be well.

    In a large project, you may want to compile each source file (using a "make" utility or similar) individually, using the -c option to gcc, and then link all the produced object files together into one executable as a last step - otherwise you'd have to list ALL source files as inputs to ONE gcc command. This would be quite time-consuming if the source is large.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    25
    Cheers, she works. Been used to programming on linux where you can compile with -c.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Witchfinder View Post
    Cheers, she works. Been used to programming on linux where you can compile with -c.
    I doubt that is true. I haven't tried it, but if it works, it's certainly not because it was meant to work that way, but because it HAPPENS to work that way.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. cygwin sshd weird behavior
    By jEssYcAt in forum Tech Board
    Replies: 6
    Last Post: 05-19-2008, 02:05 PM
  2. Problems with compiling code in cygwin
    By firyace in forum C++ Programming
    Replies: 4
    Last Post: 06-01-2007, 08:16 AM
  3. Cygwin and coLinux
    By Mario F. in forum Tech Board
    Replies: 8
    Last Post: 07-03-2006, 02:00 PM
  4. cygwin vs win32 help
    By dynomyte in forum C++ Programming
    Replies: 9
    Last Post: 05-12-2006, 01:02 AM
  5. Cygwin Server
    By osal in forum Networking/Device Communication
    Replies: 0
    Last Post: 03-07-2005, 12:58 PM