Handmade Hero»Forums»Code
Israel Fernández Angullo
16 posts
Visual Studio DLL Compiler Nightmare (Week 5)
Edited by Israel Fernández Angullo on Reason: grammar edit
Hello Everyone!

I'm struggling with week's 5 episodes.
I'm new at C programming and all the DLL, compile switches, and above all, Visual Studio configuration problems that I'm having are sinking my moral to the point that I'm close to surrender.

I'm working with Visual Studio as an IDE. I thought that if I was going to learn a new IDE better if it was one industry standard. Also is Casey's debbuger choice. So if i use another IDE I will need to learn 2 different softwares (plus C plus some C++).
My project was created as an empty project (not makefile) and so far I've been changing all the necessary in the "Solution Properties" and "Project Properties" and "File Properties" to make it work. I wasn't using the build.bat file but I managed to compile with all the switches, warning levels, etc.. same as Casey.
All was fine and dandy until day 21.

I watched and rewatched day 21,22,23, I've searched google and this forum. I've tried to make a new Solution with the makefile template but I can't figure out what the **** i'm supossed to do with the wizzard. I don't even understand the diference between a blank solution and a MakeFile solution.
So , after too many hours of dispair (+20) I beg you to help me. As I've said. I'm new with Visual Studio (and C) and I'm regretting the decision to use it at all (VS).

I think I'm missing something fundamental.
As I understand if I'm in a windows computer that has a C compiler I can run a .bat file with a proper order to the compiler (cl-) and compile wathever I want.
In this project I have to compile two different translation units. The platform layer (win32_handmade.cpp) and the actual game code (handmade.cpp). Each one of them includes their headers files.
The platform layer is the executable (.exe) thing with code that never will change (on the fly) and the game code is in DLL format so I can change things recompile and reload without closing the "main" program.
I need to make 1 solution with 2 projects in it that share the same folder. One for the .exe and one for the DLL. But I honestly can't figure out how to set-up Visual Studio.

Even after reading This post and this one and of course,the main one. There is A LOT of information in those posts but I can't figure out nothing coherent from them. I feel stupid.
I've tried to make a new makefile solution, also to run from command line, to run from 2 separate solutions, to run from 1 solution with two projects, to set up all the switches without the .bat to set up all the switches with a .bat to make a build_dll.bat as in the previous post and use the external tools.
Maybe is because I'm spanish and english is not my language and I'm missing something from previous episodes. But I don't undertand what/where is the problem with using only the build.bat as an external tool.

And all this pain is to make the "fun" part of the windows layer. (For Casey is the fun part of win32..to me is the awesome, but sadly not fun anymore, part)

And since this is an educational forum I better ask before giving up, (and ask for A LOT (I know))

So, please, can anybody help me to configure my VisualStudio "solution" to acomplish this? am I wrong about something?

I'm wishing/praying for a step-by-step guide for a Visual Studio set-up.
Something that I could follow to get VS to compile the two things (And run them). Some kind of tutorial that everybody can follow to put VS ready at the End of the win32 platform layer prototype (Day 25)
Or maybe is best to download Emacs or another IDE? Is it really that hard to compile this type of bi-thing with VS?
I really want to continue with my handmade_clone!
Thank you all!

PD. Sorry about my english.. I hope I made myself clear. And thanks again!
Piotr Madalinski
9 posts
Visual Studio DLL Compiler Nightmare (Week 5)
Edited by Piotr Madalinski on
I think sticking with just the build.bat and a plain old text editor of your choice (it can, but does not have to be emacs or vim - something simple, like nodepad++ will do fine as well) is a better approach.
Israel Fernández Angullo
16 posts
Visual Studio DLL Compiler Nightmare (Week 5)
Edited by Israel Fernández Angullo on Reason: Adding quote from Casey
Thanks!

Downloading notepad++.

I abandon VS as a tool for editing but frankly I can't understand how can it be this difficult to setup this kind of hierarchy in the project/solution/whatever.

Episode 1 - 29'22": "....So I've said screwit. No more build tools, here's a batch file."

My bad for not paying attention.
Mārtiņš Možeiko
2559 posts / 2 projects
Visual Studio DLL Compiler Nightmare (Week 5)
Edited by Mārtiņš Možeiko on
Setting up VS project is not much more difficult than configuring some editor from scratch to adjust to your needs.

I'm assuming you have handmade source in C:\handmade folder.

Then to setup makefile based project with MSVS, do the following:
1) File -> New -> Project...
2) Select Templates -> Visual C++ -> General -> Makefile Project
3) Set Name = code and Location = C:\handmade (VS will create C:\handmade\code\code.sln/vcxproj files), make sure "Create directory for the solution" checkbox is NOT checked, then click OK
4) Press Next
5) Press Next (no need to fill fields, because we'll need to adjust them later anyway).
6) Press Finish
7) solution with project will be created, you can remove all solution folders and readme.txt, they are not needed.
10) select Project -> HandmadeHero Properties ... menu item
12) change Configuration combobox to All configurations
11) change Platform combobox to x64
12) change Configuration Type (under Project Defaults) to Makefile
13) press Apply
14) select Configuration Properties -> Debugging
15) set Command = c:\build\win32_handmade.exe
16) set Working Directory = c:\handmade\data
17) select Configuration Properties -> NMake
18) set values like this:
a) Build command line = c:\handmade\code\build.bat
c) Rebuild command line = c:\handmade\code\build.bat
b) Clean command line: del c:\build\*.dll c:\build\*.exe
d) Output = c:\build\win32_handmade.exe
19) press OK to close dialog.
20) change platform to x64 (in toolbar), because x86 is not supported
21) to see the files in Solution Explorer, select Project -> Show All Files menu item.
22) press Build and/or run to run :)
Remember you need to uncomment asset builder compilation in build.bat and execute test_asset_builder.exe in c:\handmade\data directory to generate test assets (it will create test1/2/3/fonts.hha files).

To setup native Visual Studio project, do the following:
1) File -> New -> Project ...
2) Select Templates -> Other Project Types -> Visual Studio Solutions -> Blank Solution
3) Set Name = code and Location = C:\handmade, Click OK

4) Click right mouse button in Solution Explorer on "Solution 'code'" item and select Add -> New Project ...
5) Select Visual C++ -> Win32 -> Win32 Project
6) Set Name = win32_handmade, Location should have value C:\handmade\code, Click OK
7) Press Next
8) Choose:
a) Application Type = Windows application
b) check Additional Options -> Empty project
c) uncheck Additional Options -> Security Development Lifecycle checks
9) Press Finish
10) Select Project -> Add Existing Item ... menu
11) Browse to C:\handmade\code and add win32_handmade.cpp file, click Add
12) select Project -> win32_handmade Properties ... menu item
13) change Platform combobox to x64
14) select Configuration combobox to Release and set following values:
a) Output Directory -> c:\build\
b) Character Set -> <inherit from parent or project defaults>
c) Press Apply, then choose Debugging section on the left
d) Command -> c:\build\win32_handmade.exe
e) Working Directory -> c:\handmade\data
f) Press Apply, then choose C/C++
g) Treat Warnings As Errors -> Yes
h) Press Apply, then choose C/C++ -> Preprocessor
i) Preprocessor Definitions -> HANDMADE_STREAMING=0;HANDMADE_INTERNAL=1;HANDMADE_SLOW=1;HANDMADE_WIN32=1
j) Press Apply, then choose C/C++ -> Code Generation
k) Enable C++ Exceptions -> No
l) Runtime Library -> Multi-threaded (/MT)
m) Security Check -> Disable Security Check (/GS-)
n) Floating Point Model -> Fast (/fp:fast)
o) Press Apply, then choose C/C++ -> Language
p) Enable Run-Time Type Information -> No (/GR-)
q) Press Apply, then choose Linker -> Input
r) Additional Dependencies -> user32.lib;gdi32.lib;winmm.lib;opengl32.lib
s) Press Apply
15) select Configuration combobox to Debug and set following values:
a) Output Directory -> c:\build\
b) Character Set -> <inherit from parent or project defaults>
c) Press Apply, then choose Debugging section on the left
d) Command -> c:\build\win32_handmade.exe
e) Working Directory -> c:\handmade\data
f) Press Apply, then choose C/C++
g) Debug Information Format -> Program Database (/Zi)
h) Treat Warnings As Errors -> Yes
i) Press Apply, then choose C/C++ -> Optimization
j) Enable Intrinsic Functions -> Yes
k) Press Apply, then choose C/C++ -> Preprocessor
l) Preprocessor Definitions -> HANDMADE_STREAMING=0;HANDMADE_INTERNAL=1;HANDMADE_SLOW=1;HANDMADE_WIN32=1
m) Press Apply, then choose C/C++ -> Code Generation
n) Enable Minimal Rebuild -> No (/Gm-)
o) Enable C++ Exceptions -> No
p) Runtime Library -> Multi-threaded Debug (/MTd)
q) Floating Point Model -> Fast (/fp:fast)
r) Press Apply, then choose C/C++ -> Language
s) Enable Run-Time Type Information -> No (/GR-)
t) Press Apply, then choose Linker -> General
y) Enable Incremental Linking -> No (/INCREMENTAL:NO)
v) Press Apply, then choose Linker -> Input
w) Additional Dependencies -> user32.lib;gdi32.lib;winmm.lib;opengl32.lib
x) Press OK to close dialog

16) Click right mouse button in Solution Explorer on "Solution 'code'" item and select Add -> New Project ...
17) Select Visual C++ -> Win32 -> Win32 Project
18) Set Name = handmade, Location should have value C:\handmade\code, Click OK
19) Press Next
20) Choose:
a) Application Type = DLL
b) check Additional Options -> Empty project
c) uncheck Additional Options -> Security Development Lifecycle checks
21) Press Finish
22) Select Project -> Add Existing Item ... menu
23) Browse to C:\handmade\code and add handmade.cpp file, click Add
24) select Project -> handmade Properties ... menu item
25) change Platform combobox to x64
26) repeat steps 14 and 15, except you don't need to set anything under "Debugging" section on the left.
27) additionally for each debug and release configuration you need to set following value:
Linker -> Command Line = -EXPORT:GameGetSoundSamples -EXPORT:GameUpdateAndRender -EXPORT:DEBUGGameFrameEnd

28) Click right mouse button in Solution Explorer on "Solution 'code'" item and select Add -> New Project ...
29) Select Visual C++ -> Win32 -> Win32 Project
30) Set Name = test_asset_builder, Location should have value C:\handmade\code, Click OK
31) Press Next
32) Choose:
a) Application Type = Console application
b) check Additional Options -> Empty project
c) uncheck Additional Options -> Security Development Lifecycle checks
33) Press Finish
34) Select Project -> Add Existing Item ... menu
35) Browse to C:\handmade\code and add test_asset_builder.cpp file, click Add
36) select Project -> test_asset_builder Properties ... menu item
37) change Platform combobox to x64
38) repeat steps 14 and 15, except set Command under "Debugging" section to c:\build\test_asset_builder.exe

39) Click right mouse button in Solution Explorer on "Solution 'code'" item and select Add -> New Project ...
40) Select Visual C++ -> Win32 -> Win32 Project
41) Set Name = simple_preprocessor, Location should have value C:\handmade\code, Click OK
42) Press Next
43) Choose:
a) Application Type = Console application
b) check Additional Options -> Empty project
c) uncheck Additional Options -> Security Development Lifecycle checks
44) Press Finish
45) Select Project -> Add Existing Item ... menu
46) Browse to C:\handmade\code and add simple_preprocessor.cpp file, click Add
47) select Project -> simple_preprocessor Properties ... menu item
48) change Platform combobox to x64
49) repeat steps 14 and 15, except set Command under "Debugging" section to c:\build\simple_preprocessor.exe, and Working Directory to c:\handmade\code

50) select win32_handmade project, and choose Project -> Project Dependencies menu item
51) check the checkbox next to simple_preprocessor, click OK to close dialog
52) select win32_handmade project, and choose Project -> Properties ... menu item
53) select Build Events -> Pre-Build Event
54) set Command Line to cd c:\handmade\code && c:\build\simple_preprocessor.exe >c:\handmade\code\handmade_generated.h

If you want to add other files to project, then you need to make sure you exclude *.cpp files from build - right click on file, select properties, set Excluded From Build = Yes.

Now you can compile and run program.
Make sure the win32_handmade project is selected as start up project (right mouse button on project in Solution Explorer and choose "Set as StartUp Project")
First choose the configuration you want (Release or Debug) and select x64 platform in the toolbar. Then select Debug -> Start [Without] Debugging menu item.
Everything should run fine and you can also use debugger.
Make sure you run at least once test_asset_builder project (set it as start up and choose Debug -> Start Without Debugging menu item). This will generate test assets in hha format under c:\handmade\data folder.

It is possible to make setting up all these projects faster (steps 14, 15, 26, 38, 49) - first create all projects, don't set up their properties. Then hold ctrl and select all projects at once in Solution Explorer. Then you'll be able to adjust project properties for all project simultaneously. After setting up common properties, you'll still need to adjust options that are different for each project.

Hope this helps!
Israel Fernández Angullo
16 posts
Visual Studio DLL Compiler Nightmare (Week 5)
Thank you!!!!!

This guide is incredible! And exactly what I was looking for!!
I think it should be linked in the resources section.

But man.. I followed along both ways. Excluding the test_asset and simple_prepocessor parts since I'm still at day 25. The makeFile builds ok but has an error running (can't find the .exe file) and the blank solution has 1 build success and 1 build error (the DLL). I'm pretty sure is me screwing up following your steps. But I've exhausted my patience in this labyrinth.

Come on. How can it be so complicated (long, tedious, meticulous and obscure) to say to a sofware developed for developing software:
Build "that.dll" and "that.exe", run "that.exe", rebuild and reload "that.dll".
I still feel I must be missing something. Also, the amount of files and folders that VS generates is insane. Is a wall of unnecesary complexity for me at this point. I like knowing what every file does in the project and Visual Studio kills that part. And, above all, Casey with his huge experience goes the other way.

I really, really thank you for your effort writting the guide and I'm sure lots of people will find it super usefull. But I think I will follow Casey's holy words.
Seriously, your guide should be a sticky in the resources area.
Mārtiņš Možeiko
2559 posts / 2 projects
Visual Studio DLL Compiler Nightmare (Week 5)
Can you check that when you build makefile project it really creates c:\build\win32_handmade.exe file? Basically delete it manually, and then press build in MSVC.

Then check steps 15 and 18d. Those steps sets up path to exe for MSVC IDE and debugger.

As for build error for MSVC native dll project, nobody can help you if you don't show us the error message...
Israel Fernández Angullo
16 posts
Visual Studio DLL Compiler Nightmare (Week 5)
Ei! I wasn't asking for more help with my new problems following your guide. I explained the error in the makefile project only to make a point.
The point is: I have the step by step guide that I was asking for and I still can't make it work. I'm idiot. But more than a hundred steps to setup a project seems insane to me.

I will try to make work the makefile project with your suggestions because I feel I have to use all the help that you provide. (and maybe learn something). But I'm not a programmer. This is a hobby for me. I'm amazed that if I want to make my own thing in Visual Studio using this kind of code architecture (.exe & .dll) (I don't know if I'm using the right word here) I will have to do more than a hundred steps just TO SET UP the software (the native project) and end up with a bunch of files and folders. I'm stuck in week's 5 episodes for a long time now. I want to learn the things Casey explains but I'm wrestling with the options that I have to set up.

I'm sure that I'm amazed due to my ignorance in the subject and there is a reason why is so complicated. I make an analogy to explain how I (a no programmer) see this compilation/builds: To me the compilation process is like the export process when I'm in some video editing tool. I have my project, with all my files. I click the export button. It opens a window where I put all the settings for my exported file. (All the settings have presets preloaded and I can also make my own presets. I can chose codecs and other stuff without worrying (too much) about platform compatibility. I click export and my new file is created. In all the softwares that I tried the process is exactly the same.

In programming each IDE has a different process. (Why?) I can use a .bat file to setup the compiler, or I can set-up hundreds of different properties spread along lots of different interfaces. And It seems that there are even build tools to add more stuff to the mix. As I see it the compiler is like the encoder in video. Converts your project into a file that the computer can understand and use if the computer has the appropiate "codecs". ¿Why is so different? I know that with a video editor I only can make a video. And with VS I can make anything , even a video editor. So there are more things that I would need to adjust in my exported file. But the way to make this adjustments should be similar and user friendly, no? For example. Is it posible to share "export presets"? If not, Why not?
I imagine that a video file has only data in it, and a .exe has "instructions" but ... in form of data..no? I mean. You also need a "player" (windows) for the .exe to be undesrtood by the CPU, same as the video.

Can you see how lost I am? ;)
Maybe it deserves another topic but, can someone point out a place where we can read about this things? I mean, to understand better the work of the compiler/linker and how it relates with the text editor (if it relates at all)? In a general sense! For people without a computer science degree. (Or is necessary previous backgroud? If it is...which is the background required?

I played with Unity and C# and so far the main advantage that I see is how all this nightmare is hidden to you. I just want to export my file!!

Also, another question. Casey's technique to make live changes to the code is so rare?
It seems like a easy solution to me. (more than writting a scripting language/compiler to acomplish the same at lesser speed). Where is the template?

Thanks again for the help!
Mārtiņš Možeiko
2559 posts / 2 projects
Visual Studio DLL Compiler Nightmare (Week 5)
Oh, OK :)

Well, I wouldn't call this a hundred step guide, because I went into a lot of detail. I could say for some of points like "create a project" and don't explain which menu button to press or not, because sometimes it is very obvious.

Also these all steps are for handmade setup till recent day. Initially on first days you need to set up only one or two project which reduces amount of steps significantly.

And I'll repeat what I said in the beginning, setting up MSVC IDE is not more complex that adjusting advanced editor (emacs, 4coder, whatever) to your needs. Customizing these editors will still be many-step guide.

It's ok to not know how to do things. It's not ok to not want to understand things. That's how you learn. Just take it slow and in time you'll can learn everything. As for where to find this information, depends on exactly what information. For compilers/linkers this is good high-level article:
* http://www.tenouk.com/ModuleW.html
* and keep watching Handmade Hero videos, I think Casey explained this couple of times
I can also recommend th
is book on topic of linkers: http://www.amazon.com/Linkers-Kau...eering-Programming/dp/1558604960/

For example. Is it posible to share "export presets"?
Yes, it is. In MSVS is called property sheets. Basically you set up compiler/linker/other options for sheet, and then apply this property sheet to multiple projects. That way you won't need to configure each project individually. I just didn't want to put this in my steps, as this introduces another layer of complexity.

Also, another question. Casey's technique to make live changes to the code is so rare?
Yes, it is rare. Because it requires you to architect your whole system around it. Otherwise it won't work. So if you don't think about this in the beginning, then introducing reloading later is huge pain. Casey did know he wants live code reloading and he is using memory management in way that allows to use live code changing in very easy way.
511 posts
Visual Studio DLL Compiler Nightmare (Week 5)
Caseys live update technique used in handmade hero has some limitations

It depends on the main loop being in the core executable and you cannot edit it (the platform code)

any memory used by the dll is passed into the entry point of the dll each time it is called. This means no globals of any kind.

Changing memory layout (adding a field to a struct) during execution is nearly impossible. unless you write a conversion function that gets called the first time (and only then) the entry point gets called.
Israel Fernández Angullo
16 posts
Visual Studio DLL Compiler Nightmare (Week 5)
Edited by Israel Fernández Angullo on Reason: grammar edit
mmozeiko

It's ok to not know how to do things. It's not ok to not want to understand things.


Totally agree. Is why I'm here.
I expressed myself badly. I meant that I will abandon VS as an editor.. not the whole interest in programming! But I assure you is exhausting..and... sit in front of the computer doing the set up of a software is not programming at all (I think/hope/feel).
I'm starting to feel like I'm in a bad puzzle in an old school adventure. Trying everything with everything to see if something works. With the fear of having missed some pixel in another screen. Or already be in a dead end without realizing it.

So, I will make the makefile work .."por mis huevos".
But for the sake of my learning I also will make it work without VS. Mainly to have less files that I don't understand (at this point). The hook for me to this series is the "from scratch".
I've downloaded notepad++ and sublime. Both look simple enought for me. And there's a guide for sublime too in the resources area.

About the makefile:
I think my problem is with the hierarchy of files in the folder.
The checkbox in the NewProject screen: Create directory for the solution. Has to be checked?
If I set the project name: code and the location: c:\handmade there's no HandmadeHero properties.. I assume Is the "code" project properties. Am I wrong?

Thanks!
Mārtiņš Možeiko
2559 posts / 2 projects
Visual Studio DLL Compiler Nightmare (Week 5)
Edited by Mārtiņš Možeiko on
The checkbox in the NewProject screen: Create directory for the solution. Has to be checked?
Checkbox needs to be not checked. For me it is by default in unchecked state.
After creating project save it, and check that you have c:\handmade\code\code.sln and c:\handmade\code\code.vcxproj files. If you have, then project is created in correct folder.

If set the project name: code and the location: c:\handmade in step there's no HandmadeHero properties.. I assume Is the code project properties. Am I wrong?
You mean under Project menu? I'm not sure when it happens, but sometimes it has text simply "Properties", sometimes "<project name> Properties". It's the same thing. The shortcut for that is Alt+F7. Or you can simply press right mouse button on project in Soltion Explorer and choose Properties there (last menu item).
At least this is how it is on VS2015. If you are using older VS, then maybe it is just a bit different.
Israel Fernández Angullo
16 posts
Visual Studio DLL Compiler Nightmare (Week 5)
Edited by Israel Fernández Angullo on Reason: adding Visual Studio version
Yeeess!!!
Thaanks!

I was missing a pixel! The problem was the checkbox.
But there is still something strange. I have to remove the switch (-WX) treat warning as errors. With it it doesn't compile with these warnings
I'm in Visual Studio 2015.

Code C2220 warning as error. no file 'object' generated win32_handmade.cpp
Code C4456 PlayCursor declaration hides previous local declaration. win32_handmade.cpp
Code C4456 WriteCursor declaration hides previous local declaration. win32_handmade.cpp
MSB3073 c:\handmade\build.bat exit with code -1. Microsoft.MakeFile.Targets
(text errors may be different because I have mine in Spanish)

Why is that?

What is the file Microsoft.MakeFile.Targets and what is MSBuild?
Clicking in the error message it throws me
here... no idea..

Finally I can continue. And most important. I know now how to do it with my thing. Thank you very much mmozeiko!
Mārtiņš Možeiko
2559 posts / 2 projects
Visual Studio DLL Compiler Nightmare (Week 5)
Edited by Mārtiņš Možeiko on
That's because Casey is using VS2013. Microsoft improved VS2015 compiler and added more warnings to detect more potential errors. So if you are compiling with VS2015 expect to see warnings. Usually they are trivial - like this one you are getting, simply rename variable in inner scope, and you'll be fine.
Israel Fernández Angullo
16 posts
Visual Studio DLL Compiler Nightmare (Week 5)
Ok! Thanks!
Israel Fernández Angullo
16 posts
Visual Studio DLL Compiler Nightmare (Week 5)
It's been a while but I write this here for completeness.


How to build with visual studio like a Casey.

1,
Create an external tool: Tools->ExternalTools->Add.
Name the new tool as you want (mine is RunBuildBat)
In the command line add the path to your build.bat. (mine is x:\mihandmade\code\build.bat)

REMEMBER THE POSITION OF YOUR NEW TOOL IN THE LIST OF EXTERNAL TOOLS (lame design).

2,
Create a new shortcut: Tools->Options->Keyboard.
Search for Tools.ExternalCommandX (X = the position of your external tool in the list)
Assign the new shortcut (mine is Alt+m ;) )

That's all!