How to approach C/C++ linker errors

I want to learn to fix c/c++ linker problems in an IDE - agnostic way.
I’m starting the book Hands-On C++ Game Animation Programming
With the goal of Improving my C/C++ (I’m a novice) & Learning Open GL.
I’m using VS Code even though the book is using Visual Studio.
This is deliberate, because it’s not the syntax I need to learn,
It’s most often linker errors I hit when with C++ tutorials

I have reached the point where I expect to render a blank window, and I am getting many linker errors
I am compiling with cl.exe. Each error is one of LNK2019 or LNK2001

One example is

WinMain.obj : error LNK2019: unresolved external symbol __imp_ChoosePixelFormat referenced in function WinMain

WinMain.cpp has the following

    // define constants to reduce the ammount of code included with windows.h
    #define WIN32_LEAN_AND_MEAN
    #define WIN32_EXTRA_LEAN
    #include <windows.h>

So I am including <windows.h> but I guess an arbitrary subset of it?
VS Code finds the reference (F12) to ChoosePixelFormat, but it is only a signature



`C:\Program Files (x86)\Windows Kits\10\Include\10.0.17763.0\um\wingdi.h`

So I don’t know if the issue is

  • the signature in wingdi.h is not sufficient for compiling
  • cl.exe does not ‘see’ wingdi.h (maybe I need to edit a .vsode json file?)
  • some other C++ compiler/linker black magic I’m ignorant of

The situation above seems typical to all the linker errors I am getting.

Any help would be appreciated.
Hopefully someone has a linker error checklist they can share, when they hit linker errors…

This seems like a good article, perhaps it will help me.

Not sure about this case, but the LNK2019 sounds like you are missing a library (Maybe the Windows SDK) in your project. For me at least, it’s usually either a header file is not included or the referenced library (.lib or .dll) does not exist in the project. Looking up the error number online can often clue you into what is causing it. Dealing with someone else’s code on github, sometimes there will be comments about how to fix it if others encounter the same issue.

Thanks, Neil. It is indeed libraries not being included in the compiler command.

I guess, when you use Visual Studio and create a Window C++ project , a lot of this gets managed for you automagically, thus left out of the tutorial I’m following. But understanding compiling and linking is the reason I chose the less documented route of using VS Code.

I’m using VS Code’s task.json to specify the build command. By default tasks.json only passes the current file to the linker.

	"tasks": [{
		"type": "cppbuild",
		"label": "C/C++: cl.exe build active file",
		"command": "cl.exe",
		"args": [
			"${file}" // the current .cpp file being compiled

So the problem is to

  • identify the missing library files
  • pass them to the cl.exe compiler in addition to the one .cpp file already present.

There are linker errors for both windows gl function calls, I need to find what to include for both kinds.
For the windows fns I just search for the function, the docs tell me what .lib to include. For the GL calls, I searched my local project, finding them defined in a local glad.c file.

my new args[] list looks like this:

		"args": [

The big take away for me is: VS Code will autogenerate tasks.json to run your build command
But it will not include any libraries referenced in included headers. You have to add every file you are using to the args[] list in tasks.json so they can be linked and compiled.

and now I can compile! The program is getting an unhandled exception -but it’s compiling!
new bugs = progress!

1 Like

Awesome! Glad I could help. :slight_smile: