JavaScript EditorFree JavaScript Editor     Ajax Editor 

Main Page
  Previous Section Next Section

Using the C/C++ Compiler

I have received more than 17,000 e-mails in the past three years from people who don't know how to use the C/C++ compiler. I don't want another e-mail about compiler problems unless blood is squirting from the screen and the computer is speaking in tongues! Every single problem was a result of a rookie compiler user. You simply can't expect to use a piece of software as complex as a C/C++ compiler without reading the manual—right, Jules? So please do so before trying to compile programs from this book.

Anyway, here's the deal about compiling: I used MS VC++ 5.0 and 6.0 for this book, so everything works perfectly with those compilers. I'd also imagine that VC++ 4.0 would work, but don't hold me to that (I heard there are some problems with DirectX and VC++ 4.0). If you have a Borland or Watcom compiler, they should work also, but you may have to do a bit of work to get the right setup to compile. My suggestion is to save yourself the headache and get a copy of VC++. Student and Standard editions are usually less than $100.

The bottom line is that the MS compiler is the best for Windows/DirectX programs, and it just makes everything work better. I have used Borland and Watcom for other things, but for Windows applications, I don't know many professional game programmers who don't use MS VC++. It's just the right tool for the right job. (Note to Bill Gates: My Bank of Cayman account number is 412-0300686-21. Thanks.)

Here are some hints for setting up the MS VC++ compilers. Other compilers are similar:

Application Type— DirectX programs are Windows programs. More accurately, they're Win32 .EXE applications. Hence, set your compiler to Win32 .EXE applications for all DirectX programs. If I tell you that you're making a console application, set the compiler for console applications. Also, I suggest that you make a single workspace and compile all your programs in it.

Search Directories— To compile DirectX programs, the compiler needs two things: the .LIB files and the .H files. Now, listen up: Set both search paths in the compiler/linker options to search the DirectX SDK .LIB directory and .H directory so the compiler can find the files during compilation. However, this isn't enough! You must also make absolutely sure the DirectX paths are first in the search tree. The reason for placing them first is that VC++ comes with an old version of DirectX, and you will end up linking DirectX 3.0 files if you aren't careful. Furthermore, make sure that you manually include the DirectX .LIB files in your projects. I want to see DDRAW.LIB, DSOUND.LIB, DINPUT.LIB, DINPUT8.LIB, and so on in the project! This is very important!!!

Error-Level Setting— Make sure that you turn the error level on your compiler to something reasonable, like level 1 or 2. Don't turn it off, but don't put it on full-blast. The code in this book that I wrote is professional-level C/C++, and the compiler will think that there are a lot of things that I didn't want to do that I intended to do. Hence, turn the warning level down.

Typecast Errors— If the compiler gives you a typecast error on a line of code (VC++ 6.0 users beware), just cast it! I have more than 3,000 e-mails from people who don't know what a typecast is. If you don't, look it up in a C/C++ book. But I will tell you that some of my code may still be missing an explicit typecast here or there. VC++ 6.0 might become belligerent. If you get one of these errors, look at what the compiler expects and just put it in front of the rvalue in the expression to fix it.

Optimization Settings— You aren't making full release products yet, so don't set the compiler to a crazy optimization level. Just set it for standard optimizations that prefer speed over size.

Threading ModelsIn this book 99% of the examples are single-threaded, so use the single-threaded libraries. If you don't know what that means, read a compiler book. However, when I do use multithreaded libraries, I will tell you. For example, to compile the examples on multithreading from Chapter 11, "Algorithms, Data Structures, Memory Management, and Multithreading," you must switch to the multithreaded libraries.

Code GenerationThis controls what kind of code the compiler generates. Set it for Pentium. I haven't seen a 486 in a long time, so don't worry about being compatible.

Struct Alignment— This controls the "filling" between structures. PentiumX processors like things that are in multiples of 32 bytes, so set the alignment as high as possible. It will make the code a bit bigger, but also a lot faster.

Finally, when you're compiling programs, make sure that you include all the source files referenced in the main program. For example, if you see me include T3DLIB1.H, chances are there's a T3DLIB1.CPP that needs to be in the project—get it?

      Previous Section Next Section

    JavaScript EditorAjax Editor     JavaScript Editor