Asaf
Sleep Deprived
- Joined
- Mar 22, 2010
- Messages
- 1,326
- Overview
- How to prepare the compilation environment
- How to compile
- Working with Visual Studio
- Before Debugging
- Debugging with Visual Studio
Overview
This tutorial is intended to be a simple step-by-step guide to compiling the DLL.
This tutorial does not teach C++, or how to edit the Civ4 C++ source code. For these you might try one of these C++ tutorials and Xienwolf's An Idiots Guide to Editing the DLL.
My tutorial covers the 'Setting up the SDK' section of Xienwolf's tutorial.
I tried bringing the required installations to a minimum, so I packed parts of the previously installed components in archives which only need to be extracted, to prevent problems with 64-bit OS's. I also included only some of the parts of the original components, since most of it was not needed.
All archives are in .7z format, which can be extracted using 7-zip (which is free) or WinRAR (which is not).
The makefile+fastdep utility credit goes to DannyDaemonic (Downloaded from here),
with some modifications to the makefile which I added.
I put in spoiler tags extra information which is not necessary for the process, but might help you understand it better.
Spoiler :
A short introduction (feel free to skip)
Unlike Python source files, which are interpreted at runtime (and therefore don't need to go through any kind of processing beforehand), C++ source files need to be converted to machine language (commands which are comprised of 1's and 0's). This process is called compilation.
The final product of the compilation process (and some other processes which we'll not go into) of Civ4's C++ code is a DLL file (Dynamically Linked Library). The game loads this file and uses the code which was generated in the compilation.
How to prepare the compilation environment
This tutorial will help you prepare the environment to convert the C++ source code files to a DLL which the game can use.
Let's get to it!
Step 1: Download and install Microsoft Visual C++ 2008 Express (thanks to embryodead for uploading it). You might need to restart your system afterwards.
This is the only component you actually need to install.
Admittedly, you don't have to use it (there are alternatives), but it makes life somewhat easier, so I suggest you do.
Spoiler :
Microsoft Visual Studio is an IDE (Integrated Development Environment) which allows you to edit, compile and debug your code.
Visual C++ is the component of Visual Studio for handling C++ code.
The current version of VS is 2010, but there were rumors that the 2010 Express edition lacks somewhat in the field of debugging, so I used 2008.
If you want to use VS2010, here's an explanation from Sareln on what you have to change:
I've got it working on VS2010 express.
Primary switch to throw is (once you have a project):
Project -> Properties (Alt + F7) -> Configuration Properties -> Configuration Type: Makefile
Also Configuration Properties -> Nmake -> Make sure your command line build all / rebuild all etc. make sense.
The Express edition is the free version of Visual Studio. Apparently you need to register it (for free) to use it more than 30 days.
And thanks to embryodead for the following tip:
Also, VC2008 Express will ask you to register with Microsoft. If for some reason you don't want to, temporarily change the system's date to 2009 when launching the program.
Note: The previous download is an online installer (it's an installer which downloads the actual application installer). If you want an offline installer, check out the spoiler below.
Spoiler :
According to the instructions in this link, you can download this file from microsoft and extract it to a folder.
Then you need to go to the command line in this folder and run:
Code:
msiexec /i vs_setup.msi vsextui=1 addlocal=all reboot=reallysuppress
And thanks to civjo for testing it and for bringing up this issue.
Step 2: Download the packed Microsoft Visual C++ Toolkit 2003, and extract it where you see fit.
Remember the path of the extracted folder, since you'll need it later on. The archive size is ~5MB, and the extracted folder is ~32MB.
Spoiler :
This toolkit contains several tools and files which are needed to compile the source code. Specifically it contains the compiler and linker utilities.
In addition it contains header files (e.g. the STL header files) and libraries (e.g. the basic C++ runtime libraries) which match the version in which the game was originally compiled, and that is to make the DLL compatible with the game's executable (that's why we use an old version of this toolkit).
Step 3: Download the packed Windows Platform SDK and extract it where you see fit.
Again, you'll need this path later on. The archive size is ~10MB, and the extracted folder is ~110MB.
Spoiler :
This archive contains some of the files of the Windows Platform SDK (version 6). Specifically it contains the resource compiler (to embed the version information), and all the include and lib files for x86 architecture (which is what the DLL is compiled to).
This SDK contains the basic Windows environment modules, which (almost) every program which compiles to Windows environment needs.
It also contains many unneeded things, but it seemed like a bad idea to try to filter them by functionality.
Step 4: Download the project and makefile archive, and extract it in the folder in which you want to keep your mod's source files.
Spoiler :
Extracting it will create the CvGameCoreDLL folder, in which you'll have the following files:
CvGameCoreDLL.sln - The 'solution' file for your C++ project. This file is the file you double-click to open the Visual C++ environment (or just open it from inside the VC++). The solution points to the project file (each solution can contains one or more projects. This solution contains just one).
CvGameCoreDLL.vcproj - The project file for you C++ project. This project holds a list of files that you can see in the file tree (solution explorer) in your Visual C++, and holds the parameters to send to the Makefile.
bin folder - Holds the fastdep utility which creates the dependency structure between source and header files.
Makefile - This file contains the actual instructions of what to build, and how to build it.
Step 5: Edit the Makefile (extracted to CvGameCoreDLL folder).
You can open this file in any text editor (Visual Studio will also do), and update the following paths:
Code:
#### Paths ####
TOOLKIT=C:\Dev\Microsoft Visual C++ Toolkit 2003
PSDK=C:\Dev\WindowsSDK
CIVINSTALL=D:\games\Civilization IV\Beyond the Sword
TOOLKIT is the path where you extracted Microsoft Visual C++ Toolkit 2003 (step 2).
PSDK is the path you extracted Windows Platform SDK (Step 3).
CIVINSTALL is your BTS installation path.
Checkout the spoiler tag to enable auto-copying the DLL to where it should be:
Spoiler :
You can also edit the following line to copy the finished DLL directly to you assets folder after it is compiled successfully (To uncomment delete the '#' at the beginning of the line with YOURMOD=):
Code:
## Uncomment to have newly compiled dlls copied to your mod's Assets directory
#YOURMOD=$(CIVINSTALL)\Mods\MyMod
Step 6: Add the source files.
The source files are the files which contain the C++ code. These are the files with the .cpp, .h and .inl extensions.
I've prepared an archive of the BTS 3.19 source files, which you can extract (it also contains the CvGameCoreDLL folder). The source files should be in the same folder as the Makefile, .vcproj and .sln files.
Alternatively, you can take these files from your BTS install folder (in the CvGameCoreDLL folder): You should copy all the .cpp, .h, .inl, .rc files into CvGameCoreDLL (you'll only have 1 .rc file).
CvTextScreens.cpp is not used, so you can skip copying it.
Another change you need to make after you copy the files from the BTS folder is in the file CvGameTextMgr.cpp: comment out (add // to the beginning of the line) line 2085 (inside the function createTestFontString, the first line in the function which starts with szString.append), otherwise you might have problems compiling.
A 3rd option is that you don't want to base your code on BTS 3.19, but on another mod. Simply take the C++ source code files of this mod and copy them into the CvGameCoreDLL folder.
Note that some mods only publish the source files which they have changed, so you should probably first copy the BTS files to the CvGameCoreDLL folder, and override it with the mod's files.
Important: If you base your mod on another mod which has its own C++ source files - use the 3rd option.
And that's it. Everything should (hopefully) work now.
How to compile
Open Visual C++ 2008 which you've installed in step 1. In it, open (File->Open->Project/Solution) the CvGameCoreDLL.sln file inside the CvGameCoreDLL folder.
On the top of the window (below the menu) you will see a drop-down box with either 'Debug' or 'Release' in it. These are the two configurations in which you can compile.
Release is the configuration which you will use when you release your mod (hence the name). You can of course use it before that. This is the more optimized configuration: It'll run faster, and the final DLL file will be much smaller (same as the DLL which is released with the game).
Debug is the configuration which allows you to perform debugging on your code. When debugging, you have several advantages:
- If the game crashes, you'll see the line in the code in which it crashed (and all the functions that called it).
- You can place 'breakpoints' in certain points in the code, so when this code is about to run, the game will pause and you will gain control over the run of the game. You can then watch all the variables' values, run the program line-by-line and even make on-the-fly changes to the values.
However, the debug DLL is both slower and much larger than the Release one, so only use it while developing your mod.
After you've selected a configuration, Simply go to the 'Build' menu and choose 'Build Solution' (or press F7). This will start the compilation process. If there are modified source files, this will save them first.
If everything's OK, it will produce a CvGameCoreDLL.dll file in either the Debug or Release subfolder, and if you added it in the makefile - it will also be copied directly to your mod's folder.
You should make sure to place CvGameCoreDLL.dll in your <BTS installation>/Mods/<Mod Name>/Assets folder, so the game will load it.
If it's not there, there won't be any error, since the game will simply load the default DLL, and things might not work later on in the game.
That's it. Let me know if it works, or if this guide needs corrections or improvements.