0

So, I'm trying to build a DLL for a Java project I'm working on and when I'm trying to tell g++ to build a DLL, it spits out a bunch of errors.

This is the command I tried

g++ -I "C:\Users\my-user\Desktop\jni"                                       # JNI (jni.h, jni_md.h)
    -I "C:\Program Files (x86)\Windows Kits\10\Include\10.0.18362.0\shared" # Headers for winternl.h
    -I "C:\Program Files (x86)\Windows Kits\10\Include\10.0.18362.0\um"     # Location of winternl.h
    -shared -o native_lib.dll NativeCall.cpp                                # This should create a dll?

g++ tells me that somehow something is wrong in my source code, in MinGw and windows header files like winternl.h and winbase.h. Here are a few errors g++ outputted in the command prompt:

NativeCall.cpp:20:39: error: invalid conversion from 'FARPROC' {aka 'int (*)()'} to 'LPVOID' {aka 'void*'} [-fpermissive]
   21 |  LPVOID lpFuncAddress2 = GetProcAddress(GetModuleHandle(L"ntdll.dll"), "NtRaiseHardError");
      |                                                         ^~~~~~~~~~~~
      |                                                         |
      |                                                         const wchar_t*

c:\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\istream.tcc:1031:62: error: expected primary-expression before '.' token
 1031 |     const __ctype_type& __ct = use_facet<__ctype_type>(__in.getloc());

C:\Program Files (x86)\Windows Kits\10\Include\10.0.18362.0\um/winbase.h: In function 'long unsigned int InterlockedIncrement(volatile long unsigned int*)':
C:\Program Files (x86)\Windows Kits\10\Include\10.0.18362.0\um/winbase.h:9326:28: error: '_InterlockedIncrement' was not declared in this scope; did you mean 'InterlockedIncrement'?
 9326 |     return (unsigned long) _InterlockedIncrement((volatile long*) Addend);

I tested my code in Visual Studio and it worked just fine. The entire error

Anybody got an idea what I'm doing wrong?

  • It looks like you are missing an include somewhere that Visual Studio gives you for free. Note that the first error in your pastebin points to a preprocessor directed error message indicating that your architecture is not defined: `C:\Program Files (x86)\Windows Kits\10\Include\10.0.18362.0\um/winnt.h:1027:2: error: #error Must define a target architecture.`. Likely it is some other header that has the preprocessing macros in it that figures out what architecture to define. – dmedine Aug 24 '22 at 01:45
  • Generally, function pointers are *not* convertible to `void*` – user253751 Aug 24 '22 at 01:55
  • 1
    Something seems terribly wrong here. You compile with *G++*, while the error is from *MSSDK*. Those 2 should not be put together.*G++* comes with its own set of header files, while *MSSDK* is for *VStudio*. Pick one and stay with it. What toolchain is *G++* part of? – CristiFati Aug 24 '22 at 06:31
  • @CristiFati There is absolutely nothing wrong with using gcc with MSSDK. – n. m. could be an AI Aug 24 '22 at 07:05
  • 1
    @n.1.8e9-where's-my-sharem.: Hmm, is that true, so toolchains can be mixed? I'll have to do some more digging, because as I found out in https://stackoverflow.com/a/50838055/4788546, they were incompatible. – CristiFati Aug 24 '22 at 07:38
  • What if you pass *-m64* (or *-m32*) option to *G++*? Or (this shouldn't be attempted) *-D\_M\_AMD64*? – CristiFati Aug 24 '22 at 07:57
  • @CristiFati That link doesn't say a word about gcc. Try googling "gcc windows sdk". Or go [here](https://stackoverflow.com/questions/1549123/how-to-use-the-windows-api-in-mingw). – n. m. could be an AI Aug 24 '22 at 07:58
  • @n.1.8e9-where's-my-sharem.: It was a *CLang* test (that I did at the end). Nevertheless I will definitely go deeper into the problem (and if necessary, correct the answer). – CristiFati Aug 24 '22 at 08:40
  • @n.1.8e9-where's-my-sharem. You can't use the MSSDK with gcc, they aren't compatible at all. – ssbssa Aug 24 '22 at 10:32
  • Lose all the includes pointing to *MSSDK*, and you should be fine (if the code is written in a cross platform manner). – CristiFati Sep 12 '22 at 08:21

0 Answers0