0

I am working on an application that requires interfacing with Gdi32 and JsonCpp libraries. To keep things simple, I put relevant relevant code-segments in 2 separate files and compiled them individually as binaries. Both applications compiled are run ok.

I used the following commands to compile:

JsonCpp:

g++ -I/mingw64/include/json -L/mingw64/bin -llibjsoncpp-24 my_json.cpp -o my_json.exe -O3

Gdi32:

g++ my_app.cpp -o my_app.exe -lGDI32 -O3

Later I combined the code from those 2 files into a single file and tried to compile with the following command:

g++ -I/mingw64/include/json -L/mingw64/bin -llibjsoncpp-24 -lGDI32 combined.cpp -o combined.exe -O3

This results in the following compiler/linker error:

C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\msys64\tmp\ccOLYQ4n.o:combined.cpp:(.text+0x458): undefined reference to `__imp_GetStockObject' collect2.exe: error: ld returned 1 exit status

Could anyone please tell me how to fix this issue?

I am on Window 8 PC, using msys2 (mingw64) g++ ver 10.2.0.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
  • It appears combined.cpp calls GetStockObject (or something close to), but you are not providing the code for it in combined.cpp, or are not linking in the library where GetStockObject resides, or are not linking things together in the proper order. – Eljay Aug 13 '21 at 12:11
  • The gdi related code is there. It compiles ok in the separate file. Compilation fails with the combined code file. – Pen Guin Aug 13 '21 at 12:22
  • The parameters passed to `g++` are processed in the order given. So I repeat *"... or are not linking things together in the proper order."* – Eljay Aug 13 '21 at 14:21
  • Indeed, wrong order of compiler parameters was the culprit. I was able to resolve this issue by rearranging the parameters passed to compiler: g++ combined.cpp -o combined.exe -lGDI32 -I/mingw64/include/json -L/mingw64/bin -llibjsoncpp-24 -O3 – Pen Guin Aug 13 '21 at 16:29

0 Answers0