handmade.network » Forums » cl.exe build problem
kasius
Marko
2 posts
#13002 cl.exe build problem
2 months, 2 weeks ago

Hello guys,

Sorry for referencing beginning of the project, but I just lost a few days try to build an simple c++ program which includes windows.h and windows functions like: GetMessage, TranslateMessage, DispatchMessage, from command prompt using cl and it won't work.

I did everything like Casey on video, but I get errors. And if I create a project in Visual Studio and put the same code in source program builds and runs just fine.

What I am missing?

Simple code from MSDN site:

#ifndef UNICODE
#define UNICODE
#endif

#include <windows.h>

LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);

int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR pCmdLine, int nCmdShow)
{
// Register the window class.
const wchar_t CLASS_NAME[] = L"Sample Window Class";

WNDCLASS wc = {};

wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.lpszClassName = CLASS_NAME;

RegisterClass(&wc);

// Create the window.

HWND hwnd = CreateWindowEx(
0, // Optional window styles.
CLASS_NAME, // Window class
L"Learn to Program Windows", // Window text
WS_OVERLAPPEDWINDOW, // Window style

// Size and position
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,

NULL, // Parent window
NULL, // Menu
hInstance, // Instance handle
NULL // Additional application data
);

if (hwnd == NULL)
{
return 0;
}

ShowWindow(hwnd, nCmdShow);

// Run the message loop.

MSG msg = {};
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}

return 0;
}

LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_DESTROY:
PostQuitMessage(0);
return 0;

case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);

FillRect(hdc, &ps.rcPaint, (HBRUSH)(COLOR_WINDOW + 1));

EndPaint(hwnd, &ps);
}
return 0;

}
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}


MY Errors:

W:\handmade\code>build
Microsoft (R) C/C++ Optimizing Compiler Version 19.00.24215.1 for x86
Copyright (C) Microsoft Corporation. All rights reserved.

win32_handmade.cpp
Microsoft (R) Incremental Linker Version 14.00.24215.1
Copyright (C) Microsoft Corporation. All rights reserved.

/out:win32_handmade.exe
/debug
win32_handmade.obj
win32_handmade.obj : error LNK2019: unresolved external symbol __imp__GetMessage
[email protected] referenced in function [email protected]
win32_handmade.obj : error LNK2019: unresolved external symbol __imp__TranslateM
[email protected] referenced in function [email protected]
win32_handmade.obj : error LNK2019: unresolved external symbol __imp__DispatchMe
[email protected] referenced in function [email protected]
win32_handmade.obj : error LNK2019: unresolved external symbol __imp__DefWindowP
[email protected] referenced in function "long __stdcall WindowProc(struct HWND__ *,unsign
ed int,unsigned int,long)" ([email protected]@[email protected]@[email protected])
win32_handmade.obj : error LNK2019: unresolved external symbol __imp__PostQuitMe
[email protected] referenced in function "long __stdcall WindowProc(struct HWND__ *,unsign
ed int,unsigned int,long)" ([email protected]@[email protected]@[email protected])
win32_handmade.obj : error LNK2019: unresolved external symbol __imp__RegisterCl
[email protected] referenced in function [email protected]
win32_handmade.obj : error LNK2019: unresolved external symbol __imp__CreateWind
[email protected] referenced in function [email protected]
win32_handmade.obj : error LNK2019: unresolved external symbol __imp__ShowWindow
@8 referenced in function [email protected]
win32_handmade.obj : error LNK2019: unresolved external symbol __imp__BeginPaint
@8 referenced in function "long __stdcall WindowProc(struct HWND__ *,unsigned in
t,unsigned int,long)" ([email protected]@[email protected]@[email protected])
win32_handmade.obj : error LNK2019: unresolved external symbol [email protected]
referenced in function "long __stdcall WindowProc(struct HWND__ *,unsigned int,
unsigned int,long)" ([email protected]@[email protected]@[email protected])
win32_handmade.obj : error LNK2019: unresolved external symbol [email protected]
2 referenced in function "long __stdcall WindowProc(struct HWND__ *,unsigned int
,unsigned int,long)" ([email protected]@[email protected]@[email protected])
win32_handmade.exe : fatal error LNK1120: 11 unresolved externals

mmozeiko
Mārtiņš Možeiko
1502 posts
1 project
#13003 cl.exe build problem
2 months, 2 weeks ago Edited by Mārtiņš Možeiko on Sept. 2, 2017, 9:51 p.m.

"unresolved external symbol" error means that linker doesn't know where to find these functions (GetMessage, CreateWindowExW, etc..)

To fix it, you need to pass import libraries to linker arguments. Import libraries tell linker that function X can be found in dll Y.

To figure out what libraries you need to use, you need to check MSDN. For eaxmple, lets take GetMessage function. Open the link and check what it says at bottom table in "Library" row. It says "user32.lib" - that means you need to pass "user32.lib" in linker arguments. Add it there and try to compile again. If it complains about more functions, do the same process again - look up function in MSDN, figure out which library it is in, and pass the library in linker arguments.

You can see Casey explaining this at the end of day 1: https://hero.handmade.network/episode/code/day001#3720

Btw, we have dedicated forum section for discussion or asking questions about HandmadeHero code here: https://hero.handmade.network/forums/code-discussion
kasius
Marko
2 posts
#13004 cl.exe build problem
2 months, 2 weeks ago

I really don't know how I skipped this part. Thank you very much!!!!

All the best!