1

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

gost1212
  • 25
  • 6
  • I suggest you rather use the free [Visual Studio Community Edition](https://visualstudio.microsoft.com/vs/community/) than gcc if you want to target Windows. It also contains a very powerfull, yet easy to use debugger. Also your header problems will be gone. – Jabberwocky Jun 29 '22 at 12:31
  • I'm already using it, and compiling my C files with terminal. – gost1212 Jun 29 '22 at 12:34
  • Can you give me the structure of project folder? I think header must be in separate folder and you havent set target to windows. If you are using windows, linux, mac or any. I suggest you to either write a make file or cmake file to run independently. – iamniki Jun 29 '22 at 12:38
  • 1
    why aren't you just including `windows.h`? – Daniel A. White Jun 29 '22 at 12:38
  • 1
    Works for me with MinGW-W64 8.1.0 . ("KnownFolders.h" gives a lot of other errors because I did not include "windows.h" before.) You might want to [edit] your question and show a [mre]. – the busybee Jun 29 '22 at 12:40
  • @NikhilGowdaShivaswmay I doubt it's a problem with his project folder, the problem is that gcc doesn't find the headers of the Microsoft Windows SDK. So the actual question here is probably: *How can I use the Microsoft Windows SDK with gcc*. – Jabberwocky Jun 29 '22 at 12:50
  • I did include windows.h before anything, it just won't budge. – gost1212 Jun 29 '22 at 12:55
  • @Jabberwocky my GCC apparently can find some headers like "windows.h" but can't find other headers like "KnownFolders.h" even if they're in the same exact folder. Do you think there is something that blocks GCC from reading it? – gost1212 Jun 29 '22 at 13:13
  • 1
    @gost1212 are you sure the `windows.h` gcc finds is the `windows.h` in the Windows SDK folder? I'm pretty sure the `windows.h` gcc finds is the one that comes with gcc and not the one in the Windows SDK folder. You can check that easily by deleing tempoarily the `windows.h` in the Windows SDK folder. – Jabberwocky Jun 29 '22 at 13:18
  • @Jabberwocky all of these SDK headers tracks back to this folder: C:\Program Files (x86)\Windows Kits\10\Include\10.0.22621.0\um. this sure is an SDK folder. – gost1212 Jun 29 '22 at 13:25
  • @Jabberwocky odd, I deleted the windows.h and the program still runs just fine after compilation, VS Code is now freaking out. – gost1212 Jun 29 '22 at 13:30
  • 2
    Mingw should have it's own headers and libs out of the box. – Lundin Jun 29 '22 at 13:41
  • 1
    https://stackoverflow.com/questions/1549123/how-to-use-the-windows-api-in-mingw misc #define might be needed for shlobj.h etc. – Lundin Jun 29 '22 at 13:43
  • 1
    @gost1212 that's exactly what I supposed. There is a `windows.h` that comes with gcc and that is somewhere in the gcc folders. And gcc finds _this_ `windows.h`. I'm pretty sure gcc completly ignores the Microsoft SDK. – Jabberwocky Jun 29 '22 at 13:53
  • @Jabberwocky weird for it to ignore XD, then should I add path for SDK in the settings? – gost1212 Jun 29 '22 at 14:01
  • @gost1212 yes probably you should do that, but don't ask me how, I only use Visual Studio. OTOH I'm not sure if gcc can cope with the Microsoft specific stuff in the Microsoft header files.... And maybe this helps: https://stackoverflow.com/questions/1549123/how-to-use-the-windows-api-in-mingw$ – Jabberwocky Jun 29 '22 at 14:03

1 Answers1

1

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
Brecht Sanders
  • 6,215
  • 1
  • 16
  • 40