Thread: How to tell gcc to compile for Windows ?

  1. #1
    Registered User
    Join Date
    Apr 2021
    Posts
    19

    How to tell gcc to compile for Windows ?

    I wrote a C program in which valid instructions are used depending on the platform on which the program is to be started: Windows or Linux. In the C code I use #ifdef USE_WIN to say that that piece of code goes for the Windows platform, unlike #else ....
    How do I go about passing USE_WIN inside the Makefile via the command line with make ?

  2. #2
    Registered User
    Join Date
    Apr 2021
    Posts
    19
    I forgot, I use gcc as a compiler, both in Windows and Linux.

  3. #3
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    There are some predefined symbols for Windows: _WIN32 (this exists on mingw and VC++), __WINNT__

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  5. #5
    Registered User
    Join Date
    Apr 2021
    Posts
    19
    As a compiler I use gcc of MinGW in Windows 10 and gcc in Linux.
    So far, I know too, but I would like to go command line with make, telling which platform to use in the build. Example: make WIN_USE and use WIN_USE in gcc which it defines in source code (using #ifdef WIN_USE do this #else do that).

  6. #6
    Registered User
    Join Date
    Apr 2021
    Posts
    19
    I'll explain. I have the following source code main.c:
    Code:
    #include <stdio.h>
    
    int main(int argc, char *argv[]) {
    #ifdef LINUX
        printf("Ciao Linux\n");
    #else
        printf("Ciao Windows\n");
    #endif
        
        return 0;
    }
    The Makefile is:
    Code:
    all: uno
    
    uno:
        gcc -D $(CPLAT) -c main.c
        gcc -o test main.o
    When I start make I have to pass the CPLAT variable (which is either LINUX or WIN) to the Makefile, thus compiling the source code main.c obtaining a different result depending on the variable passed CPLAT (which will affect the #ifdef LINUX ... #else in main .c).
    How should I do ?

  7. #7
    Registered User
    Join Date
    Apr 2021
    Posts
    19
    I got there now. The solution is very simple:
    Code:
    CPLAT = LINUX
    
    all: uno
    
    uno:
        gcc -D $(CPLAT) -c main.c
        gcc -o test main.o
    then from the command line I type:
    make CPLAT=LINUX
    or
    make CPLAT=WIN
    so I get respectively with ./test:
    Ciao Linux
    ,
    Ciao Windows

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compile/ runs on MAC but not on Windows?
    By SlowSpeedChase in forum C Programming
    Replies: 3
    Last Post: 12-02-2013, 06:18 PM
  2. Compile libVISCA in windows?
    By andreasleon in forum C++ Programming
    Replies: 0
    Last Post: 06-07-2009, 10:09 PM
  3. Compile id3lib for Windows
    By @nders in forum C Programming
    Replies: 1
    Last Post: 04-07-2006, 11:27 PM
  4. how do i compile pipe() API on windows ?
    By intruder in forum C++ Programming
    Replies: 2
    Last Post: 01-04-2006, 06:39 PM
  5. Compile under linux, To windows
    By Pandora in forum Linux Programming
    Replies: 0
    Last Post: 04-12-2003, 12:55 PM

Tags for this Thread