0

I try to build some source code containig Win32 API code with GCC 8.3.0 (i686-posix-dwarf) that comes with Strawberry Perl Portable package. It easily builds code, but struggles when it comes to linking against Windows' core dlls:

ld.exe: CMakeFiles\ninja.dir/objects.a(util.cc.obj):util.cc:(.text+0x10ce):  
undefined reference to `_imp__GetLogicalProcessorInformationEx@12'

The code was configured using CMake with following cmd:

cmake -B build -S . -DCMAKE_C_COMPILER=gcc -DCMAKE_CXX_COMPILER=g++  
-DCMAKE_GENERATOR="MinGW Makefiles" -DCMAKE_MAKE_PROGRAM=gmake   
-DBUILD_TESTING=OFF

After some googling, I added -DCMAKE_EXE_LINKER_FLAGS="-L\"C:\Windows\System32\" -Wl,--as-needed -lkernel32 to cmake configuration cmd and it changed error to:

ld.exe: strawberry-perl-5.32.1.1-32bit-portable/c/bin/../lib/gcc/i686-w64-mingw32/8.3.0/../../../../i686-w64-mingw32/lib/../lib/libmingwex.a(lib32_libmingwex_a-misc.o):misc.c:(.text+0x60a):  
undefined reference to `_imp__LeaveCriticalSection@4'
ld.exe: strawberry-perl-5.32.1.1-32bit-portable/c/bin/../lib/gcc/i686-w64-mingw32/8.3.0/../../../../i686-w64-mingw32/lib/../lib/libmingwex.a(lib32_libmingwex_a-wcrtomb.o):wcrtomb.c:(.text+0x75):   
undefined reference to `_imp__WideCharToMultiByte@32'

As I understand, it struggles finding Win32 dll functions and by adding -lkernel32 I helped find some missing functions, but it still cannot find them all.

Is there a way to fix undefined referece errors?

5
  • 1
    _imp__LeaveCriticalSection@4 is a 32 bit ABI, but you link with 64 bit kennel32.lib. Maybe you need to link with libwindowsapp.a, i.e. -lwindowsapp. Commented Nov 1 at 17:17
  • 3
    Btw -L\"C:\Windows\System32\“ is nonsense, this directory doesn't contain .lib files. Commented Nov 1 at 17:18
  • I would also expect you link against a lib not a dll (.a) file directly. Are you sure you setup your library directories correctly? Commented Nov 2 at 8:37
  • @3CEZVQ I run windows on 32-bit cpu arch, can it still try to link against 64-bit arch? Also i did not try to link againts static libs, as i know linking staticaly against windows api is highly discouraged Commented Nov 2 at 9:28
  • @PepijnKramer i'm not sure whether i set up library directories correcty so i ask what is correct directories to link against win32 api. Other libraries (as libstdc++) are working just fine. Commented Nov 2 at 9:30

1 Answer 1

1

Turns out my version of mingw32 was to outdated and libkernel32.a was missing implementation of GetLogicalProcessorEx(). When i passed -L\"C:\Windows\System32\" -Wl,--as-needed -lkernel32 to linker, i forced it to use kernel32.dll that was in System32 folder that had implementation of GetLogicalProcessorEx(), but was missing (or somehow changed, i'm still confused about this momet) other kernel32 functions, due it's incompatibleness with mingw32 install.

What i did to fix this issue is a little bit illegal. I had previously installed cygwin with newer version of mingw32 and broken environment. I took libkernel32.a that had implementation of GetLogicalProcessorEx() and replaced old libkernel32.a with newer one. This seemed to work for me for the moment, but as i suspect it may lead to errors in future as i replaced only libkernel32.a and left other libs untouched.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.