How to compile Libtcod and C++, mirrored from Ysgard.net

Source: http://www.ysgard.net/2011/06/visual-studio-2010-c-express-and-libtcod/

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.

  1. 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.
  2. 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.
  3. Open Visual Studio Express.  Create an Empty project for your new libtcod application.
  4. Right click on your project in the Solution Explorer and select Properties.
  5. Go to Configuration Properties -> VC++ Directories.
  6. Click on Include Directories, and from the little drop-down box on the right, select <Edit…>
  7. 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).
  8. Do the same for SDL (mine was C:\Sigil\VS\libs\SDL-1.2.14\include).
  9. Click OK.
  10. Now do the same for the Library Directories, specifying the folders for your libtcod and SDL lib directories.
  11. 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.
  12. 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!

8 thoughts on “How to compile Libtcod and C++, mirrored from Ysgard.net”

  1. fuck man, I just uninstalled VS Express 2013 because I couldn’t figure this shit out. And now I find this looking for a solution to a problem with using mingw to compile libtcod on windows 8. geesh, give me the run around. THREE DAYS! Three days I been trying my heart out to get a this library set up so I can get going.

    1. error LNK1104: cannot open SDL.lib
      SDL.lib is in
      G:\Microsoft Visual Studio\mylibs\SDL-devel-1.2.15-VC\SDL-1.2.15\lib\x86

      but there is also one in an x64 folder as well, i feel that I am so close T.T

    2. Seriously, I feel your pain. I’d been smashing my face into my desk for over a week trying to figure this out. No exaggeration.

  2. http://lazyfoo.net/SDL_tutorials/lesson01/windows/msvsnet0508e/index.php

    “For certain versions of SDL, there will be a x86 folder and a x64 folder inside of the lib folder from the archive. The x86 folder contains the 32bit *.lib files and the x64 bit folder contains the 64bit versions of the library files. If you’re compiling a 32bit program, set the new library directory to the x86 folder and if you’re compiling a 64bit version set the new library directory to the x64 folder. By default Visual Studio compiles in 32bit so if you’re not sure how you’re compiling, try the 32bit libraries first. What matters here is not whether you have 32/64bit windows, but what type of program you’re compiling.” -above website

    to fix the above error I just changed your 10th step to point more specifically to the x86 folder instead of the folder that has both the x86 and x64 folders in it

  3. WOOT! okay so I moved zlib1.dll, terminal.png, SDL.dll, and libtcod-VS.dll to
    G:\Microsoft Visual Studio\Projects\TacticsHack\TacticsHack
    where all my source files are and it WORKS! After 3 days of novice troubleshooting and learning C++ I can finally dive in! Thanx Tankor! Now I’ve got to copy paste all these steps for others wanting to using VisStudio to build with libtcod.

    I’m following jice’s tutorial at:
    http://codeumbra.eu/complete-roguelike-tutorial-using-c-and-libtcod-part-1-setting-up
    And I’m doing three to four chapters a day at:
    http://www.learncpp.com/
    And I’m having so much fun now!
    You know who’s going in the credits with special thanks and has two thumbs? Give yourself two thumbs up and say “This guy.” =)

  4. Shit man, thanks for this, I was losing my mind trying to get this to work, now I just need to hunt for the SDL.lib which should be easy enough.

    This was so much easier to do in Linux.

    Thanks again.

Leave a Reply to sebastien tides Cancel reply

Your email address will not be published.