So I was playing with the Doryen Library, aka libtcod, the other day. What a fantastic little library. I had just finished a small demo for displaying how influence maps works (more for my own education, as I’m a pretty inexperienced programmer and doing is how I learn) and was pretty pleased with the result. I’m using Visual Studio Express because I use Windows and happen to like it a bit more than Code::Blocks.
Okay, so I don’t need that debug window anymore. Go into the linker properties, specify the subsystem as Windows app, compile…. and BAM!
MSVCRTD.lib(crtexew.obj) : error LNK2019: unresolved external symbol _WinMain@16 referenced in function ___tmainCRTStartup C:\Sigil\VS\InfluenceMap\Debug\InfluenceMap.exe : fatal error LNK1120: 1 unresolved externals
Hmm, okay what’s going on here? Check the libtcod documentation… no, there’s nothing here about this. Why is this happening?
The answer to this lies in the way the program is run. Before, I was compiling the program as a console app. For this, the standard C++ entry point, main, was sufficient.
int main(int argc, char** argv)
But the moment I specified the program as a Windows application, the rules changed. To use the Windows api, I need a Windows entry point, which is defined like this:
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
But the samples I’ve seen don’t define this entry point. They define a standard main(). So what am I missing? As it turns out, I’m missing SDL!
libtcod is an SDL library, and SDL has its own way of mapping main() to WinMain(). But in order to make this happen, you also need to link in SDL and pull in the SDL.h header.
Here’s how to set up a Visual Studio project with both libtcod and SDL. You can then write and test your application using the console window, and when the time is right, switch to a native windows app without any issues!
Here’s the exact procedure I followed.
- Download and extract the libtcod library for Visual Studio to wherever you place your libraries. In my case, I extracted it to C:\Sigil\VS\libs, which resulted in the folder C:\Sigil\VS\libs\libtcod-1.5.1.
- Download and extract the SDL development libraries for Visual Studio. You want the latest one available, I used SDL-devel-1.2.14-VC8.zip. I extracted it to C:\Sigil\VS\libs\SDL-1.2.14.
- Open Visual Studio Express. Create an Empty project for your new libtcod application.
- Right click on your project in the Solution Explorer and select Properties.
- Go to Configuration Properties -> VC++ Directories.
- Click on Include Directories, and from the little drop-down box on the right, select <Edit…>
- Click on the little folder icon to add a new folder to your include directories, specify the directory for your libtcod library (for me, C:\Sigil\VS\libs\libtcod-1.5.1\include).
- Do the same for SDL (mine was C:\Sigil\VS\libs\SDL-1.2.14\include).
- Click OK.
- Now do the same for the Library Directories, specifying the folders for your libtcod and SDL lib directories.
- Now go to Configuration Properties -> Linker -> Input. Click on Additional Dependencies, click on the drop-down box, select <Edit…>. Add the following dependencies, one per line: libtcod-VS.lib, SDL.lib, SDLmain.lib. Click on OK.
- There’s one final step left. You need to pull in the main SDL header file. At the top of your main file, add #include <SDL.h>
You’re now set! Go ahead and develop your application as normal. Whenever you’re ready (or just want to test it) you can set your linker to produce a native Windows application. To do this, you need to go into the Properties windows again, and select Configuration Properties -> Linker -> System. In the right-hand pane, click on SubSystem and select Windows (/SUBSYSTEM:WINDOWS) from the drop-down menu.
Build your application again, and you should now be enjoying some console-free Windows goodness!