cgccnetapi32

How to correctly build code for gcc with netapi32?


I just try build this example for GCC.

But I got several problems.

My system is:

I try: gcc main.c -o main.exe -lnetapi32

But gcc returned:

undefined reference to `WinMain' collect2.exe: error: ld returned 1 exit status

OK - wrong entry point.
Changed wMain in WinMain.
Result:

main.c:11:5: error: conflicting types for 'WinMain'; have 'int(int,  wchar_t **)' {aka 'int(int,  short unsigned int **)'}
   11 | int WinMain(int argc, wchar_t *argv[])
      |     ^~~~~~~
In file included from c:/msys64/ucrt64/include/windows.h:70,
                 from main.c:8:
c:/msys64/ucrt64/include/winbase.h:1128:14: note: previous declaration of 'WinMain' with type 'int(struct HINSTANCE__ *, struct HINSTANCE__ *, CHAR *, int)' {aka 'int(struct HINSTANCE__ *, struct HINSTANCE__ *, char *, int)'}
 1128 |   int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd);

What is wrong?


Solution

  • wmain is an extension by Microsoft, and insofar I know not supported by gcc. You need to change the entry function to a standard int main(int argc, char **args). You can also use WinMain but then you'll have to use int WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)