我正在尝试编译以下代码
#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;
}
但是
g++ -o test -L"<dir>" -lOle32 <file>.cpp
# <dir> contains Ole32.Lib
总是告诉我__imp_CoInitializeEx
和__imp_CoUninitialize
是未定义的,而-print-file-name=Ole32.Lib
仅返回Ole32.Lib
。如果g ++找不到Ole32.Lib,也许
g++ -c -o test.o <file>.cpp
ld -L"<dir>" -lOle32 -o test test.o
有效。现在,g ++ / ld实际上找到了CoInitializeEx和CoUninitialize,但是标准库似乎丢失了,添加-static-libstdc++
或-lstdc++
或-llibstdc++
也无济于事。那我想念什么呢?为什么g ++无法找到CoInitializeEx和CoUninitialize?
编辑:我可以肯定地说我的代码,头文件和库文件没有问题,因为我可以使用Visual Studios编译器来编译代码:
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