0

I'm trying to compile the following code

#include <iostream>

#include <windows.h>
#include <objbase.h>

int main (int argc, char** argv) {
    HRESULT hr;

    hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
    if (SUCCEEDED(hr)) {
        std::cout << "Initialized" << std::endl;
    } else {
        std::cout << "Failed" << std::endl;
    }

    CoUninitialize();
    return 0;
}

but

g++ -o test -L"<dir>" -lOle32 <file>.cpp
# <dir> contains Ole32.Lib

always tells me that __imp_CoInitializeEx and __imp_CoUninitialize are undefined and -print-file-name=Ole32.Lib just return Ole32.Lib. If g++ doesn't find Ole32.Lib, maybe

g++ -c -o test.o <file>.cpp
ld -L"<dir>" -lOle32 -o test test.o

works. Now g++/ld actually finds CoInitializeEx and CoUninitialize, but the standard library seems to be missing and adding -static-libstdc++ or -lstdc++ or -llibstdc++ doesn't help either. So what am I missing? Why is g++ unable to find CoInitializeEx and CoUninitialize?

EDIT: I can definitely say that there is nothing wrong with my code, my header files and my library files, because I can compile the code using Visual Studios compiler:

cl /c /EHsc ^
   /I"<...>\Microsoft Visual Studio 14.0\VC\include" ^
   /I"<...>\Windows Kits\10\Include\<version>\ucrt" ^
   /I"<...>\Windows Kits\10\Include\<version>\shared" ^
   /I"<...>\Windows Kits\10\Include\<version>\um" ^
   /Fotest.obj ^
   main.cpp

link /nologo /machine:x64 /subsystem:console ^
     /libpath:"<...>\Microsoft Visual Studio 14.0\VC\lib\amd64" ^
     /libpath:"<...>\Windows Kits\10\Lib\<version>\ucrt\x64" ^
     /libpath:"<...>\Windows Kits\10\Lib\<version>\um\x64" ^
     /out:test.exe ^
     test.obj Ole32.Lib
Cubi73
  • 1,891
  • 3
  • 31
  • 52
  • 1
    Possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – Mike Kinghan Apr 15 '19 at 19:28
  • ...and the relevant answer is: [Your linkage consumes libraries before the object files that refer to them](https://stackoverflow.com/a/43305704/1362568) – Mike Kinghan Apr 15 '19 at 19:29
  • @MikeKinghan Wow, this really was the reason. I could have sworn I already tried the answer you provided... Now it works, thank you :)) – Cubi73 Apr 15 '19 at 19:37

0 Answers0