19

Can g++ and minGW on Windows XP use the Windows SDK?

Specifically, why does g++ fail to compile:

#include <stdio.h>
#include <windows.h>

int main(void) {
 printf("!!!Hello World!!!");
 return EXIT_SUCCESS;
}

I have tried compiling by by running:

g++ -c -Wall  Test.cpp -IC:/Program\ Files/Microsoft\ Platform\ SDK/Include/

I get a litany of compile errors beginning with

winnt.h:666:2: #error Must define a target architecture.

I have installed the Windows Server 2003 SP1 Platform SDK

Background

I am working on a large real-time image processing software project that up until now has used g++, minGW and gnu make files (written by hand). For a new feature, I need to interface with a frame grabber that has an SDK which was designed for Visual Studio. The framegrabber SDK depends on header files from the Windows SDK.

Do I need to learn Visual Studio or is there another way?

AndyL
  • 14,302
  • 14
  • 43
  • 70

4 Answers4

15

I use MinGW to compile Windows programs every day, with zero problems. There must be something wrong with your installation - try the version at Twilight Dragon Media.

Edit: Just re-read your post - you do not need to specify the include directory as you are doing, and probably should not do so. Also, you may (or may not) need the slightly mysterious -mwindows flag. I just compiled your program using MinGW (TDM build) g++ 4.4.1, with the command line:

g++ main.cpp

with absolutely no problems.

More Info: Just so you know what the -mwindows flag does, the GCC docs say:

This option is available for Cygwin and MinGW targets.It specifes that a GUI application is to be generated by instructing the linker to set the PE header subsystem type appropriately.

Personally, I've never found it necessary, but then my Windows apps are all command line tools or servers.

  • 1
    Why do you not need to specify the include directory? How does g++ know where the include files are? For example, when I include my framegrabber SDK my compiler complains that it can't find a crtDbg.h include file. That header file exists in C:\Program Files\Microsoft Platform SDK\Include\crt\ but g++ doesn't seem to know about it. – AndyL Jan 07 '10 at 18:36
  • 3
    MinGW GCC picks up the include files from its own include directory, which contains its own versions of the windows headers. If you need to specify a "non-standard" header then you may need to use the include path, but you should make sure it comes after the GCC default path. Also, you need to be a bit careful with the case of filenames if you want to write portable code - you should never use mixed case. –  Jan 07 '10 at 18:40
  • 1
    I see. Thanks. What is the order of precedence for including paths? Does a "-I" passed to the compiler override the GCC default path? – AndyL Jan 07 '10 at 18:44
  • 2
    Also, if you want a happy life, install any extra headers and libraries in a path without any spaces in the directory names. Remember GCC is a UNIX based tool chain, and UNIX users almost never create file names with spaces in them, so the tools have not been well tested on such names. –  Jan 07 '10 at 18:45
  • Can't remember what the precedence is - the GCC manual is available at http://gcc.gnu.org/onlinedocs. –  Jan 07 '10 at 18:46
4

According to the discussions from the MinGW-users mailing list, you might encounter compatibility problems with the Windows SDK, and you may have to fix those problems yourself.


Georgi Petrov [Dec 27, 2010; 8:33am]:

Hello,

I'm trying to develop a EVR (Vista/7 specific video presentation API) renderer for MPlayer on MSYS/MinGW. The problem is that I have Windows SDK v7.1 and I need evr.h from it. If I try to copy it to MinGW's include directory as well as the 12 header files it includes, the compilation is close to impossible. I have a TON of errors just by including evr.h, nothing else. I investigated and found that I can't use the Windows SDK header files directly with MinGW, because it ships with its own header files, which are sometimes very, very different. The problem is that Media Foundation/Enhanced Video Renderer header files are not part of MinGW's header files.

What should I do?


Ross Ridge [Oct 10, 2010; 10:16pm]:

For the most part the Windows SDK headers aren't incompatible with GCC. If there one particular header file you need that doesn't exist in win32api heapers or is missing something you need, then you can try just using that one header with from the Windows SDK and use win32api for the rest.

It's also possible to only use Windows SDK headers, and avoid using the win32api headers entirely, but you need to fix a number of problems in key header files. I used to just modify the headers, but the Windows SDK has actually gotten a bit more compatible and now I use wrappers to fix the problems.

There are some header files and libraries that are pretty much fundementally incompatible with GCC, like GDI+. If you want to use GDI+, you'll need to use a Microsoft compiler.

                                    Ross Ridge
XP1
  • 6,910
  • 8
  • 54
  • 61
1

Try using the latest version of MSYS2 UCRT64 with MingW64. I'm using it on Windows 11.

Use 'pacman' to install 'mingw-w64-x86_64-gcc'. Try re-installing it too to insure you have the WIN32 SDK which is included in this windows-based package:

$ pacman -S mingw-w64-x86_64-gcc
warning: mingw-w64-x86_64-gcc-12.2.0-6 is up to date -- reinstalling
resolving dependencies...
looking for conflicting packages...

Packages (1) mingw-w64-x86_64-gcc-12.2.0-6

Total Installed Size:  154.48 MiB
Net Upgrade Size:        0.00 MiB

:: Proceed with installation? [Y/n] Y
(1/1) checking keys in keyring                               [###############################] 100%
(1/1) checking package integrity                             [###############################] 100%
(1/1) loading package files                                  [###############################] 100%
(1/1) checking for file conflicts                            [###############################] 100%
(1/1) checking available disk space                          [###############################] 100%
:: Processing package changes...
(1/1) reinstalling mingw-w64-x86_64-gcc                      [###############################] 100%

I discovered the windows.h is located in '/ucrt64/include' within the shell session and 'C:\Msys64\ucrt64\include' outside the shell:

$ find . -name windows.h
./mingw64/include/windows.h
./ucrt64/include/windows.h

Here's a link to an article from Microsoft which shows how to troubleshoot setting up gcc to work with building windows applications:

https://code.visualstudio.com/docs/cpp/config-mingw

You can add the directories to your build using the '-I' parameter on 'make' or 'gcc':

make -I /ucrt64/include
justdan23
  • 560
  • 7
  • 9
0

try adding these defines before you include those windows headers

#define WINVER 0x0501
#define _WIN32_WINNT 0x0501

EDIT: my gcc compiles your script without problems (and without these defines) aswell. I am using mingw's gcc 4.40 (alpha ?!)

smerlin
  • 6,446
  • 3
  • 35
  • 58