The Allegro Wiki is migrating to github at https://github.com/liballeg/allegro_wiki/wiki

Difference between revisions of "Visual C++ Express 2005"

From Allegro Wiki
Jump to: navigation, search
m (switched to local version of VC80 CRT)
(New category)
Line 286: Line 286:
 
This article was written by [[Matthew Leverton]]. '''If you have anything to add to the article, please first use the discussion tab.''' If it is valuable information, it will be added to the body of the article.
 
This article was written by [[Matthew Leverton]]. '''If you have anything to add to the article, please first use the discussion tab.''' If it is valuable information, it will be added to the body of the article.
  
[[Category:Article]]
+
[[Category:Articles]]
 
[[Category:IDE Configuration]]
 
[[Category:IDE Configuration]]

Revision as of 18:26, July 15, 2007

Background

Visual C++ Express is an excellent, free, fully optimizing compiler and IDE from Microsoft. It doesn't contain all of the features that the retail versions have, but it is more than sufficient for creating Allegro applications. You must be running Windows 2000 or later to use it. Your computer should have at least 256MB of RAM and about 2GB of free disk space for a complete install of Visual C++ Express, MSDN Library, and the Platform SDK.

Installing the Software

While it is possible to build Allegro with VC++ Express, it isn't a very pleasant task because VC++ Express doesn't come with the necessary Windows and DirectX libraries. Therefore, we will just make use of packaged binaries available from Allegro.cc.

First thing you need to do is download and install VC++ Express:

Next, you will want to install the Platform SDK. It technically is optional if you don't static link, but it is recommended. Follow the instructions from here:

Once that is installed, download the MSVC 8.0 binary package from Allegro.cc:

You'll need to locate your Visual Studio/VC folder. By default it will be in your C:\Program Files folder. Once you find that, you'll need to copy the files from the above mentioned binary package into their proper locations:

  • The contents of the zip file's lib folder must go into your VC/Lib folder.
  • The contents of the zip file's include folder must go into your VC/include folder.
  • You should also copy the three DLLs from the zip file's bin folder into your C:\Windows\System32 folder.
  • You should also copy alld42.pdb from the zip file's lib folder into your C:\Windows\System32 folder.

Setting up a Project

We'll begin by setting up a project with debugging enabled. This is the configuration you should use when working on your project.

  1. Open up Visual C++ Express 2005 from the Start menu.
  2. Select File / New Project...
  3. Select Visual C++ / General / Empty Project
  4. Type in the name of your project.
  5. Select the parent folder for your project. (VC++ will automatically create a sub folder for your project.)
  6. Uncheck the option Create directory for solution.
  7. Select Project / Add New Item...
  8. Select Code / C++ File, enter main.cpp for the name, and press Add.

At this point, you have a blank C++ project. There are a couple configuration settings we need to change to get Allegro to work (make sure you have the Debug Configuration selected):

  1. Select Project / Properties....
  2. Under Configuration Properties / C/C++ / General, change Debug Information Format to Program Database for Edit & Continue (/ZI).
  3. Under Configuration Properties / C/C++ / Optimization, change Optimization to Disabled.
  4. Under Configuration Properties / C/C++ / Code Generation, change Runtime Library to Multi-threaded Debug DLL (/MDd).
  5. Under Configuration Properties / Linker / Input, add alld.lib to Additional Dependencies.
  6. Under Configuration Properties / Linker / Debugging, change Generate Debug Info to Yes (/DEBUG).
  7. Press OK.

That's it! Now we can write our first Allegro application in Visual C++ Express. Copy and paste this code to your main.cpp file:

<highlightSyntax language="cpp">#include <allegro.h>

int main(void) {

  allegro_init();
  allegro_message("Hello World!");
  return 0;

} END_OF_MAIN()

</highlightSyntax>

Now press F7 to build the project. There should be no errors. Press CTRL-F5 to run it. If all goes as expected, you should see a message box with the text Hello World.

Important: With VC 2005, a few things have changed that complicate releasing a binary to non-developers. Be sure to read the Deployment section when you get ready to release your project to other people.

Using the Debugger

One of the nicest features of Visual C++ Express is its debugger. To use it with Allegro, make sure you are running your project in a Windowed mode. It does not work well with a full screen application.

To use the debugger, first make sure you are using a Debug configuration. The active configuration is listed to the right of the green arrow on the toolbar. To begin a debugging session, just press F5. Your application will run as normal as long as nothing crashes. A benefit to using the debug mode is that VC will automatically look for bad pointers and tell you when you try to use them.

You can set breakpoints by placing the cursor on a line and pressing F9. When the program reaches that line of the code, it will pause the execute and let you investigate things. You can then view and adjust the values of variables. Once you are finished, you can press F5 to continue again. Try adding a breakpoint on the allegro_message line and see what happens when you Debug (F5).

Advanced Options

As of now, we just have the Debugging version of Allegro configured. While this is the version you should use to develop your project, you'll also want to at least configure a Release (optimized) version for distribution.

Visual C++ Express lets you set up a variety of different build versions. With Allegro, the following can be set up:

  • Release
  • Debug (we set this one up already)
  • Profile
  • Static Release
  • Static Release + Static Runtime (See Deployment section)
  • Static Debug
  • Static Profile

The Static versions are different from the normal ones in that no Allegro DLL is needed. Instead, all the relevant Allegro code is included in the executable. This makes the file a bit larger, but easier to distribute.

The Release version is the one you should use when distributing your project. The Debug version is the one you should use when developing your project. The Profile version is the one you should use when trying to determine where the bottlenecks are. (Note that the Profile version isn't altogether very useful, because it just gives you more detailed information on Allegro's code as opposed to your own.)

The two that are the most useful are listed in boldface. You'll most likely use one of these two when deploying your application. You'll probably never need any of the other configurations.

To get started, select Project / Properties... from the menu. On the top left, you should see a drop down called Configuration. Two versions are already present: Debug and Release. If you want to add Profile or Static configurations, see the next sub-section. If you don't, you can skip it.

Adding new Configurations

  1. Press Configuration Manager....
  2. Under Active Solution Configuration, select <New...>
  3. Give it a name (from one of the above six configurations)
  4. Press OK.

You'll need to repeat steps 1-3 for every configuration you want to add. Note that both the Release and Debug configurations are already created (although not configured) by default. So you only need to add configurations if you want to work with Static or Profiling versions.

Working with Configurations

Once you've added all the configurations that you want to use, you need to set them up. To do so, first select the configuration from the drop down box on the top left. After you do that, you can alter the settings specific to that configuration. The following table is a listing of all the settings that need to be changed for each configuration to get Allegro to work.

Configuration Key Value
Release C/C++ / Code Generation / Runtime Library Multi-threaded DLL (/MD)
^ Linker / Input / Additional Dependencies alleg.lib
Debug C/C++ / General / Debug Information Format Program Database for Edit & Continue (/ZI)
^ C/C++ / Optimization / Optimization Disabled
^ C/C++ / Code Generation / Runtime Library Multi-threaded Debug DLL (/MDd)
^ Linker / Input / Additional Dependencies alld.lib
^ Linker / Debugging / Generate Debug Info Yes (/DEBUG)
Profile C/C++ / Code Generation / Runtime Library Multi-threaded DLL (/MD)
^ Linker / Input / Additional Dependencies allp.lib
Static Release C/C++ / Preprocessor / Preprocessor Definitions ALLEGRO_STATICLINK
^ C/C++ / Code Generation / Runtime Library Multi-threaded DLL (/MD)
^ Linker / Input / Additional Dependencies alleg_s.lib kernel32.lib user32.lib gdi32.lib comdlg32.lib ole32.lib dinput.lib ddraw.lib dxguid.lib winmm.lib dsound.lib
^ Linker / Command Line / Additional Options /LTCG


Static Release, Static Runtime C/C++ / Preprocessor / Preprocessor Definitions ALLEGRO_STATICLINK
^ C/C++ / Code Generation / Runtime Library (/MT)
^ Linker / Input / Additional Dependencies alleg_s_crt.lib kernel32.lib user32.lib gdi32.lib comdlg32.lib ole32.lib dinput.lib ddraw.lib dxguid.lib winmm.lib dsound.lib
^ Linker / Command Line / Additional Options /LTCG


Static Debug C/C++ / General / Debug Information Format Program Database for Edit & Continue (/ZI)
^ C/C++ / Optimization / Optimization Disabled
^ C/C++ / Preprocessor / Preprocessor Definitions ALLEGRO_STATICLINK
^ C/C++ / Code Generation / Runtime Library Multi-threaded Debug DLL (/MDd)
^ Linker / Input / Additional Dependencies alld_s.lib kernel32.lib user32.lib gdi32.lib comdlg32.lib ole32.lib dinput.lib ddraw.lib dxguid.lib winmm.lib dsound.lib
^ Linker / Debugging / Generate Debug Info Yes (/DEBUG)
Static Profile C/C++ / Preprocessor / Preprocessor Definitions ALLEGRO_STATICLINK
^ C/C++ / Code Generation / Runtime Library Multi-threaded DLL (/MD)
^ Linker / Input / Additional Dependencies allp_s.lib kernel32.lib user32.lib gdi32.lib comdlg32.lib ole32.lib dinput.lib ddraw.lib dxguid.lib winmm.lib dsound.lib

(If anyone wants to make a Project Wizard/Template to eliminate the need to hand edit configuration settings, it would be much appreciated! Post a link to it if you ever make one...)

Deployment

Microsoft introduced a new way to handle DLLs with Visual C++ 2005 (in both Express and retail versions) that is very confusing to people expecting the traditional behavior. It's beyond the scope of this article to explain why they did and how it all works, but let it suffice to say that you can no longer just drop a DLL in the same folder as an executable and expect everything to work in Windows XP or later versions.

You'll soon find out that not many people have the VC 8 runtime DLLs that are needed to run your programs. So you'll need to distribute them with your project, the proper way. First off, make sure you are building a Release build (as opposed to Debug). Then you need to use one of the following two methods:

Using the Microsoft.VC80.CRT folder

You can include an assembly that includes the DLLs in a specific way. The following should work with your project:

You'll need to place the contents of that file directly into your program's folder. This means you need to create a structure like:

  • c:\mygame
    • alleg42.dll
    • game.exe
    • Microsoft.VC80.CRT
      • Microsoft.VC80.CRT.manifest
      • msvcm80.dll
      • msvcp80.dll
      • msvcr80.dll

You must use the original names as in the Zip file. Of course, there is one caveat. Windows 98/2000 users will need to manually copy the msvcr80.dll folder out of the Microsoft.VC80.CRT subfolder and back into the same folder as the executable.

Using the Static C Runtime

Alternatively, you can static link everything into the executable. This means that the Allegro DLL and the C runtime will be stored inside the executable, eliminating the need for any external assemblies or DLLs. In order to do this, you need to create the Static Release, Static Runtime profile as detailed in the above configuration table. You'll end up with a single executable that will run on any Windows system, regardless if they have the Allegro or VC8 DLLs.

You might run into linking problems if you use additional third party libraries. This will most likely be due to the library expecting to use the Multi-threaded DLL (/MD) version of the C runtime. In order to fix that, you'll need to recompile that library using the /MT switch instead.

Notes

This article was written by Matthew Leverton. If you have anything to add to the article, please first use the discussion tab. If it is valuable information, it will be added to the body of the article.