我已經開始了將我的程式想法變為現實的程序。我的問題是我似乎無法讓 api 作業和運行。
我正在使用 Visual Studio 代碼和 mingw x64 編譯器。
我只是在運行 Microsoft 提供的關于如何構建 direct2d 程式的示例代碼,
參考
以及管理應用程式狀態的基視窗類,
參考文獻 2
唯一的區別是我變得懶惰并復制并粘貼了 basewidow 類,而不是像他們對 basewin.h 所做的那樣包含它
問題:
當我運行示例代碼時出現此錯誤,
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\uub17\AppData\Local\Temp\ccti919g.o:myprogram.cpp:(.text$_Z17D2D1CreateFactory17D2D1_FACTORY_TYPERK5_GUIDPPv[_Z17D2D1CreateFactory17D2D1_FACTORY_TYPERK5_GUIDPPv] 0x2b): undefined reference to `D2D1CreateFactory'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../lib/libmingw32.a(lib64_libmingw32_a-crt0_c.o): in function `main':
C:/M/mingw-w64-crt-git/src/mingw-w64/mingw-w64-crt/crt/crt0_c.c:18: undefined reference to `WinMain'
collect2.exe: error: ld returned 1 exit status
我做了一些搜索,發現api分為Ansi和Unicode,所以我嘗試了api入口點“winMain”的Unicode版本而不是“wwinMain”并將“pwstr”更改為“pstr”,當我運行時然后我在終端中得到這個輸出,
In instantiation of 'BOOL BaseWindow<DERIVED_TYPE>::Create(PCWSTR, DWORD, DWORD, int, int, int, int, HWND, HMENU) [with DERIVED_TYPE = MainWindow; BOOL = int; PCWSTR = const wchar_t*; DWORD = long unsigned int; HWND = HWND__*; HMENU = HMENU__*]':
myprogram.cpp:202:20: required from here
myprogram.cpp:54:37: error: cannot convert 'PCWSTR' {aka 'const wchar_t*'} to 'LPCSTR' {aka 'const char*'} in assignment
54 | wc.lpszClassName = ClassName();
| ~~~~~~~~~^~
| |
| PCWSTR {aka const wchar_t*}
myprogram.cpp:59:33: error: cannot convert 'PCWSTR' {aka 'const wchar_t*'} to 'LPCSTR' {aka 'const char*'}
59 | dwExStyle, ClassName(), lpWindowName, dwStyle, x, y,
| ~~~~~~~~~^~
| |
| PCWSTR {aka const wchar_t*}
In file included from C:/msys64/mingw64/include/windows.h:72,
from myprogram.cpp:1:
C:/msys64/mingw64/include/winuser.h:2203:65: note: initializing argument 2 of 'HWND__* CreateWindowExA(DWORD, LPCSTR, LPCSTR, DWORD, int, int, int, int, HWND, HMENU, HINSTANCE, LPVOID)'
2203 | WINUSERAPI HWND WINAPI CreateWindowExA(DWORD dwExStyle,LPCSTR lpClassName,LPCSTR lpWindowName,DWORD dwStyle,int X,int Y,int nWidth,int nHeight,HWND hWndParent,HMENU hMenu,HINSTANCE hInstance,LPVOID lpParam);
I did more searching and saw somewhere that my mingw crt compiler doesnt support unicode or wide characters or somethind and must use WinMain, so i kept WinMain as the entry point and defined UNICODE and _UNICODE which is already defined in the cpp properties json folder, and i get this in the terminal
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\uub17\AppData\Local\Temp\ccT85reo.o:myprogram.cpp:(.text$_Z17D2D1CreateFactory17D2D1_FACTORY_TYPERK5_GUIDPPv[_Z17D2D1CreateFactory17D2D1_FACTORY_TYPERK5_GUIDPPv] 0x2b): undefined reference to `D2D1CreateFactory'
collect2.exe: error: ld returned 1 exit status
I dont know much but i went as far as changing everything that deals with strings to either ansi and unicode versions to remove the need for conversion but generally i get the same result.
I do not understand how code from the microsoft site can fail and how a compiler that is made for win32 doesnt support what the os supports.
i literally cant run sample code lol, please educate me
generally everything i tried was changing to the versions of the functions that are ansi, trying to use Unicode anyway although appaerently it is incompattible with my compiler by using different macros and defines provided by microsoft, and checking the vscode json and build settings, which are
c_cpp_properties.json
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"compilerPath": "C:\\msys64\\mingw64\\bin\\gcc.exe",
"cStandard": "gnu17",
"cppStandard": "gnu 17",
"intelliSenseMode": "windows-gcc-x64"
}
],
"version": 4
}
tasks.json
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C : g .exe build active file",
"command": "C:\\msys64\\mingw64\\bin\\g .exe",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "compiler: C:\\msys64\\mingw64\\bin\\g .exe"
}
]
}
launch.json
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "C:\\msys64\\mingw64\\bin\\gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "Set Disassembly Flavor to Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C : g .exe build active file"
}
]
}
uj5u.com熱心網友回復:
D2D1CreateFactory找不到,因為您沒有按照https://docs.microsoft.com/en-us/windows/win32/api/d2d1/nf-d2d1-d2d1createfactoryD2d1.lib中的指定鏈接。將引數添加到命令列。也許您還需要使用指令來指定目錄。-ld2d1-L
可以使用MSVC 編譯器pragma指令(這可能不適用于mingw)。
#pragma comment(lib, "d2d1")
已編輯
"-ld2d1",之前添加的"-o",。
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-ld2d1",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/448719.html
標籤:windows visual-studio-code winapi character-encoding linker
上一篇:在遠程服務上復制檔案的功能
下一篇:如何正確重新分配calloc?
