Difference between revisions of "Free Compiler Setup"
(→To Do) |
|||
Line 143: | Line 143: | ||
== To Do == | == To Do == | ||
− | *Check if this compiles vessels with panels. | + | *Check if this compiles vessels with panels. |
{{HasPrecis}} | {{HasPrecis}} | ||
[[Category:Tutorials]][[Category:Addon tutorials]] | [[Category:Tutorials]][[Category:Addon tutorials]] |
Revision as of 02:29, 7 December 2006
This page documents the travails and heartache necessary to get the MSVC++ 2005 Express Edition to compile Orbiter projects.
Requirements
Get the Compiler
Go to the Microsoft download center and get the compiler.
Tell the program you are agreeing to its pernicious license agreement, but do it with your fingers crossed.
Get the Graphical IDE, but not MSDN or SQL stuff.
Get Orbiter and the SDK
Get Orbiter and the OrbiterSDK from Orbitersim.com and unpack it.
For the latest version of Orbiter, 2006-P1, you need the following files:
- Orbiter Base (060929)
- Orbiter SDK (060929)
For the older Orbiter 06 version, you need the following files:
- Orbiter Base (060504)
- Orbiter SDK (060504)
For the older Orbiter 05 version, you need at least 4 files:
- Orbiter Base (050116)
- Orbiter SDK (050116)
- Orbiter Base Patch (050216)
- Orbiter SDK Patch (050216)
Unpack all these files into the same folder, in order. A good choice is C:\Orbiter. This document assumes you use this path, from now on.
When installing the patches, it will if it is ok to replace a file. Say yes to all.
Get the Windows SDK
You need the SDK to get windows.h and its associated files.
Go to here to get the Windows Platform SDK. It says it is the Windows 2003 Server SDK but it includes what we want. (Note that you can now download the SDK rather than running the installer directly from your Windows machine.) Run the installer and uncheck everything but what is shown below.
Setting up a project
VC++ 2003 files are not perfectly compatible with VC++ 2005, and the non-free version is not perfectly compatible with the free version. There are several changes that you need to make to the project to get it to compile. Fortunately, they are small, and the same for all projects.
We will modify the ShuttlePB project to work with the VC++2005. Start VC++, and open the project file OrbiterSDK\samples\ShuttlePB\ShuttlePB.dsw.
Configuration
At the top, immediately under the menu bar, change the Configuration to Release.
Include path
Right click on the bold ShuttlePB in the left-hand tree. Select "Properties". Add the SDK include path for both the Platform SDK and the Orbiter SDK to the project include path. The Platform SDK installs itself by default to C:\Program Files\Microsoft Platform SDK so add the path C:\Program Files\Microsoft Platform SDK\Include. Orbiter can be installed anywhere, but if you unpacked it to C:\Orbiter, the correct path is C:\Orbiter\Orbitersdk\include. Or, you can have it be a relative path, like ..\..\include (This number of ..\ is correct for ShuttlePB, check for the right number for anyplace else)
Library path
Add the SDK library path for both the Platform SDK and the Orbiter SDK to the linker library path. The Platform SDK path default is C:\Program Files\Microsoft Platform SDK\lib. The Orbiter SDK default is C:\Orbiter\Orbitersdk\lib
C++ language change
There are a few important changes between code for Visual C++ 2005 and earlier versions. Fortunately, only small modifications to the code are required to compensate for them.
First, load OrbiterAPI.h and change the reference to <fstream.h> to <fstream> so it will find the renamed header file correctly.
If you were to try to build the project after making that change, it would still fail on line 43 of ShuttlePB.cpp.
MSVC++ 2005 fixes a long-standing Visual C++ bug relating to the scope of variables defined in for() loops, and some code has previously relied on that bug to compile: worse than that, correct C++ code following the standard which defined a variable of the same name in two for() loops would not compile! In this case, simply change the function as shown:
// Calculate lift coefficient [Cl] as a function of aoa (angle of attack) over -Pi ... Pi // Implemented here as a piecewise linear function double LiftCoeff (double aoa) { const int nlift = 9; static const double AOA[nlift] = {-180*RAD,-60*RAD,-30*RAD,-1*RAD,15*RAD,20*RAD,25*RAD,60*RAD,180*RAD}; static const double CL[nlift] = { 0, 0, -0.1, 0, 0.2, 0.25, 0.2, 0, 0}; static const double SCL[nlift] = {(CL[1]-CL[0])/(AOA[1]-AOA[0]), (CL[2]-CL[1])/(AOA[2]-AOA[1]), (CL[3]-CL[2])/(AOA[3]-AOA[2]), (CL[4]-CL[3])/(AOA[4]-AOA[3]), (CL[5]-CL[4])/(AOA[5]-AOA[4]), (CL[6]-CL[5])/(AOA[6]-AOA[5]), (CL[7]-CL[6])/(AOA[7]-AOA[6]), (CL[8]-CL[7])/(AOA[8]-AOA[7])};for (int i = 0; i < nlift-1 && AOA[i+1] < aoa; i++);int i; for (i = 0; i < nlift-1 && AOA[i+1] < aoa; i++); return CL[i] + (aoa-AOA[i])*SCL[i]; }
The exact problem here is that in older versions, the variable i in the one-line for statement for(int i=0... incorrectly has scope over its entire block, in this case the entire function. In VC++2005, i correctly has scope only over the for statement in which it is defined. Other projects may break in the compile phase for similar reasons.
Note that to allow backwards compatibility it's best to always define a for() loop variable outside the loop in this manner, even when no code tries to access the variable outside the loop. Otherwise, if, for example, you include two for() loops in the same function which both define int i as their loop variable, it will compile in VC++2005 but fail to compile in older versions.
There is an option in the project compiler settings window which changes this particular standard, but the code change makes the code clearer and more readable, and, more importantly, it's now correct C++ code according to the C++ standard.
Adding and Removing libraries
Compiling a vessel requires the Windows SDK library's kernel32.lib,user32.lib and gdi32.lib. These are not linked by default, so you have to add them to the "Additional Dependencies" line.
If ODBC library is specified you may safely remove it from the link options on the "Additional Dependencies" line. Also, by default, the project tries to link against the library msvcirt.lib which is not included in the free VC++2005 download, or the SDK download. We can easily get rid of this also. Add this library to the "Ignore specific library" line.
Edit the project properties, and make the changes as shown below:
Compiling and adding resource files
VC++ Express doesn't support the compilation of resource files (.res) from source files (such as bitmaps .bmp). However, there is a command line resource compiler (rc.exe) which is installed when you install VC++ Express. The default location for this program is C:\Program Files\Microsoft Visual Studio 8\VC\Bin\rc.exe.
rc.exe takes as its input a resource description file (.rc) and emits a resource file (.res). Take the example of two bitmaps called panel.bmp and panelLight.bmp. In order to compile these resources create a .rc file which is effectively a text file describing the resources to compile. To compile the above example you would a text file with the following text and save it as a .rc file (in our case panels.rc).
1 BITMAP panel.bmp 2 BITMAP panelLight.bmp
then execute the following command:
rc /r panels.rc
The result is a new file (panels.res). In VC++ you can select to add an existing item and add the .res file to the solution. On next compilation the resource will be embedded in the resulting dll.
Note: In order to call rc from the directory where your files are you will need to add the location of rc to your path.
Compiling the addon
Use the menu option Build/Build ShuttlePB to compile. It should now compile cleanly, no errors or warnings. The DLL will be in C:\Orbiter\Modules\ShuttlePB.dll. If you are making an MFD or plugin, you will want those to compile into the C:\Orbiter\Modules\Plugin folder. If you go look up this module, it should have the current time, and be accompanied by a .exp and a .lib file, both of which have the current date on them. Load any scenario which uses ShuttlePB and fly it normally.
Sharing the addon
When addons are compiled under VC++ 2005 (VC 8.0) the resulting DLL implements a new component dependency model not present in any previous version of VC++. This model uses an embedded XML file to declare it's dependency to which VC library it uses. For DLL's compiled in VC++ 2005, this means the embedded XML points to MSVCR80.DLL and MSVCP80D.DLL. These two files are not included with "off-the-shelf" copies of Windows. Users of your addon must either have installed the Microsoft .NET 2.0 Framework or the Microsoft Visual C++ 2005 Redistributable Package.
The .NET 2.0 Framework Redistributable can be downloaded here for x86 (32-bit) and here for x64 (64-bit).
If the user does not wish to install the full .NET 2.0 Framework, they can still use your addon by installing the Microsoft Visual C++ 2005 Redistributable Package which can be downloaded here for x86 (32-bit) and here for x64 (64-bit).
Reloading the project
Once the project has been saved VC++2005 will have created a number of new files in the original directory. To reopen the project at a later date you should open the .sln file, as opposed to the original .dsw file. If you open the project from the .sln file all of the changes you made in the previous section will be intact as opposed to going through the process of conversion again.
To Do
- Check if this compiles vessels with panels.
Precis
This article has a precis and appears in the Random addon or Random article section on the Main Page. The precis can be found at Free Compiler Setup/precis and is displayed below.
Free Compiler Setup. Many Orbiter add-ons come with source code which can be compiled using the free Visual Studio. However, there are a few things that need to be done to make Orbiter projects compile in it. This tutorial goes through the steps necessary to configure Visual Studio for use with Orbiter. (More...)
|