gost1212
gost1212

Reputation: 45

GCC can't find headers on Windows

I'm new in winAPI and I was learning how code programs with some special functions and such, so I downloaded the Windows's SDK.

Problem is, GCC decided to put the blind glasses and say:

Documents_path.c:6:25: fatal error: KnownFolders.h: No such file or directory
#include<KnownFolders.h>
                        ^
compilation terminated.

I said "OK, next one then" and there's another header with the same problem:

thread.c:3:30: fatal error: processthreadsapi.h: No such file or directory
#include<processthreadsapi.h>
                             ^
compilation terminated.

I checked if these headers are even in my PC and here they are setting with windows.h, which it was working when I tried basic functions with it.

I searched an answer for this problem but didn't find any, either it was a external\binary libraries problem, is it local or not or a macro fix (which it didn't work).

How can I fix the problem?

EDIT: I'm using VS Code

EDIT2:

This is the code of "Documents_path.c" example:

#include<stdio.h>
#include<stdlib.h>
#include<Windows.h>
#include<ShlObj.h>
#include<initguid.h>
#include<KnownFolders.h>
#pragma comment(lib, "user32.lib")
int main(){
    int a;
    PWSTR path = NULL;
    HRESULT hr = SHGetKnownFolderPath(&FOLDERID_Documents, 0, NULL, &path);
    if(SUCCEEDED(hr)){
        printf("path for Documents is: %ls", path);
    }
    scanf("%d",&a);
    CoTaskMemFree(path);
    return 0;
}

And I'm reading the basics of winAPI from this website: https://zetcode.com/gui/winapi/

as for structure of project folder: C:\Users\ %USER%\Documents\C\dawd

Upvotes: 3

Views: 1712

Answers (1)

Brecht Sanders
Brecht Sanders

Reputation: 7287

MSVC uses Windows SDK while GCC does not.

On Windows GCC uses MinGW or MinGW-w64 as standard library, which is an open source implementation of Windows API.

So GCC+MinGW will use its own headers and will not look for any Windows SDK. So GCC+MinGW on Windows works without having any Microsoft developer tools installed at all.

MinGW-w64 - which is more recent than MinGW and which supports both Windows 32-bit and 64-bit - exists in a standalone package that can be downloaded from https://winlibs.com/. But you can still use it from an IDE like VSCode or Code::Blocks.

MinGW-w64 has the files like knownfolders.h and processthreadsapi.h which you had issues with.

But be aware that #pragma comment(lib, "user32.lib") is MSVC-specific and will not work in other compilers like GCC. Instead you must use linker flag -luser32. Because you call CoTaskMemFree() you will also need to add -lole32.

I tried your code on my system and it compiles and links fine with:

gcc -c -o Documents_path.o Documents_path.c
gcc -o Documents_path.exe Documents_path.o -luser32 -lole32

Upvotes: 3

Related Questions