How to link to a static library using cl.exe (VS Code)

52 Views Asked by At

I'm quite unexperienced with VS Code and with cl.exe so I apologize for any obvious mistakes. But essentially, I'm trying to link to the User32.Lib library in the Windows Kits so that I can define the necessary functions and open a window using my code. I am having trouble finding out the correct syntax to use with cl.exe in order to link this library so that it compiles with my code and defines the functions I need to use. Without trying to resolve the problem, I get the following errors:

window.obj : error LNK2019: unresolved external symbol __imp__TranslateMessage@4 referenced in function "public: bool __thiscall Window::ProcessMessages(void)" (?ProcessMessages@Window@@QAE_NXZ)
window.obj : error LNK2019: unresolved external symbol __imp__DispatchMessageW@4 referenced in function "public: bool __thiscall Window::ProcessMessages(void)" (?ProcessMessages@Window@@QAE_NXZ)
window.obj : error LNK2019: unresolved external symbol __imp__PeekMessageW@20 referenced in function "public: bool __thiscall Window::ProcessMessages(void)" (?ProcessMessages@Window@@QAE_NXZ)
window.obj : error LNK2019: unresolved external symbol __imp__DefWindowProcW@16 referenced in function "long __stdcall WindowProc(struct HWND__ *,unsigned int,unsigned int,long)" (?WindowProc@@YGJPAUHWND__@@IIJ@Z)
window.obj : error LNK2019: unresolved external symbol __imp__PostQuitMessage@4 referenced in function "long __stdcall WindowProc(struct HWND__ *,unsigned int,unsigned int,long)" (?WindowProc@@YGJPAUHWND__@@IIJ@Z)
window.obj : error LNK2019: unresolved external symbol __imp__RegisterClassW@4 referenced in function "public: __thiscall Window::Window(void)" (??0Window@@QAE@XZ)
window.obj : error LNK2019: unresolved external symbol __imp__UnregisterClassW@8 referenced in function "public: __thiscall Window::~Window(void)" (??1Window@@QAE@XZ)
window.obj : error LNK2019: unresolved external symbol __imp__CreateWindowExW@48 referenced in function "public: __thiscall Window::Window(void)" (??0Window@@QAE@XZ)
window.obj : error LNK2019: unresolved external symbol __imp__DestroyWindow@4 referenced in function "long __stdcall WindowProc(struct HWND__ *,unsigned int,unsigned int,long)" (?WindowProc@@YGJPAUHWND__@@IIJ@Z)
window.obj : error LNK2019: unresolved external symbol __imp__ShowWindow@8 referenced in function "public: __thiscall Window::Window(void)" (??0Window@@QAE@XZ)
window.obj : error LNK2019: unresolved external symbol __imp__AdjustWindowRect@12 referenced in function "public: __thiscall Window::Window(void)" (??0Window@@QAE@XZ)
window.obj : error LNK2019: unresolved external symbol __imp__LoadCursorW@8 referenced in function "public: __thiscall Window::Window(void)" (??0Window@@QAE@XZ)
window.obj : error LNK2019: unresolved external symbol __imp__LoadIconW@8 referenced in function "public: __thiscall Window::Window(void)" (??0Window@@QAE@XZ)

And what I can tell, these errors come from the compiler having the header file included to establish the function but not the associated libraries to define them.

I was following this video (https://www.youtube.com/watch?v=Kx5CN-V6FvQ), though I am using VS Code instead of Virtual Studio. I believe I followed the video exactly, with the only deviation being that I added "#define UNICODE" to the top of my window.cpp file as without it, I get the error

'=': cannot convert from 'const wchar_t *' to 'LPCSTR'

From the line

const wchar_t* CLASS_NAME = L"This Window Class";

and

wndClass.lpszClassName = CLASS_NAME;

When I add in "#define UNICODE", I get the LNK2019 errors. And from my research I found that (to the best of my knowledge) this is caused by the libraries these functions are defined in not being included in the build. So my next objective was to figure out how to include the libraries. I first looked at the function DestroyWindow() and found in the documentation (https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-destroywindow) that it is included in the library "User32.lib". I found "User32.Lib" in the directory "C:\Program Files (x86)\Windows Kits\10\Lib\10.0.22621.0\um\x64". So to my tasks.json file I added the lines

"/I", "C:\\Program Files (x86)\\Windows Kits\\10\\Include\\10.0.22621.0\\um", 

to link to the include file with all the headers, and

"/link User32.Lib /LIBPATH:\"C:\\Program Files (x86)\\Windows Kits\\10\\Lib\\10.0.22621.0\\um\\x64\""

to link the library and its directory. I also added

"C:\\Program Files (x86)\\Windows Kits\\10\\Include\\10.0.22621.0\\um\\**",

in the "includePath" section in my c_cpp_properties.json file to tell that file where the header files were. Once I do all this, I get the error:

LINK : fatal error LNK1181: cannot open input file 'Files.obj'

Which from what I can tell is because there are spaces in my directory after /LIBPATH: when linking the library, and for some reason cl.exe takes the second word "Files" and assumes a .obj extension and tries to find that file. I'm not sure how to resolve this error, but I assume that even once I do there are more fundamental issues with my code that will keep this from compiling and generating a window. Would anyone be able to help me with what mistakes I'm making in my code?

tasks.json:

{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: cl.exe build active file",
            "command": "cl.exe",
            "args": [
                "/Zi",
                "/EHsc",
                "/nologo",
                "/I", "C:\\Program Files (x86)\\Windows Kits\\10\\Include\\10.0.22621.0\\um",              
                "/Fe${fileDirname}\\${fileBasenameNoExtension}.exe",
                "${workspaceFolder}/*.cpp",
                "/link User32.Lib /LIBPATH:\"C:\\Program Files (x86)\\Windows Kits\\10\\Lib\\10.0.22621.0\\um\\x64\""
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$msCompile"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "Task generated by Debugger."
        }
    ],
    "version": "2.0.0"
}

c_cpp_properties.json:

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "C:\\Program Files (x86)\\Windows Kits\\10\\Include\\10.0.22621.0\\um\\**",
                "${workspaceFolder}/**"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "windowsSdkVersion": "10.0.22621.0",
            "compilerPath": "cl.exe",
            "cStandard": "c17",
            "cppStandard": "c++17",
            "intelliSenseMode": "windows-msvc-x64"
        }
    ],
    "version": 4
}

Let me know if you need access to the code itself

0

There are 0 best solutions below