-
Visual C++ Compiler Options
Posted on March 16th, 2010 1 commentIt is important to understand your tools. So when I went looking at the documentation for the compiler, I was surprised to see that it was missing a lot of information on the defaults the compiler uses.
I’ve also always been uncomfortable with the way the compilers get a lot of their state from environment variables. I think it is important that given a build script and the same code, it should always build the same binaries and debug output. This assumption can be broken by small differences in the configuration of the machine building the code.
In order to create a reproducible build, we have to start somewhere. Let’s start by looking at the options for the compiler. What are the default compiler settings if no options are given? Unfortunately there is no definitive documentation. Given the lack of documentation, the rational thing to do would be to explicitly specify all the options.
Default Options
I did some research and found the default options for the compiler supplied with Visual Studio 2008, are:
cl default options (Equivalent to cl Hello.c)
cl /W1 /Ze/ Zp8 /ZB64 /Gs4096 /Ot /FoHello.obj /Fdvc90.pdb /GS /Ob0 /MT /ZM Hello.c- /W1, Sets warning level to 1
- /Ze, Turns on microsoft extensions to C and C++
- /ZB64, Sets maximum integral types to 64bits (Undocumented)
- /Zp8, Sets struct or class packing to 8 bytes
- /Gs4096, Causes stack probe code to be generated for functions with locals greater than 4096KB
- /Ot, Optimise for time rather than space
- /FoHello.obj, Name for object file output
- /Fdvc90.pdb, Name for pdb file generated by compiler
- /GS, Generate stack frame cookies
- /Ob0, Disable inline expansion
- /MT, Link to multithreaded static crt library
- /ZM, Sets the compiler to not completely finish processing a compilation unit (cpp/c file) before starting to process the next (undocumented)
Thanks to Geoff Chappell for providing the information I needed to find the default parameters. He has studied the compiler and figured out many of the compiler’s undocumented parameters. Here’s the information I used from his site: Initial CL.EXE options and Compulsory Options.
-
Troubleshooting 101
Posted on January 24th, 2010 2 commentsIt doesn’t work…
Before you start: what are your primary motivations?
Very often the goals are antagonistic:
- Get it working again as quickly as possible.
- Diagnose the problem and find out exactly what went wrong.
Ideally you want to shut the whole factory down at the exact point of failure and then spend the next few days, futzing around with the system trying to figure out the problem.However I’ve noticed that this approach causes factory managers to turn funny colours and get all shouty. If you can live with that, sure, go right ahead.
You usually can only achieve one of these goals at a time. However with careful planning you can achieve both of these goals at the same time. This requires upfront time and effort designing your system to be robust in the face of failure and also leave enough breadcrumbs to find out what lead up to the failure. Convincing your company that this is important, I’ll leave as an exercise for the reader.
Some general troubleshooting rules of thumb:
- Test the simple things first
- Select the test that will provide you with the most information (eliminate as many other possibilities as possible)
Here are some small things to check:
- Is it plugged in?
- Is it turned on?
- Have you tried rebooting your computer?
- Be aware that rebooting the computer will most probably destroy the cause of the problem.
- You may have to reboot the computer for expediency, if lives are at risk or dollars will be lost.
- Can you easily reproduce the problem? Once you have reproduced the problem:
- Is it a hardware or a software problem?
- Try it on a different computer, if it still persists, it’s probably software, otherwise it’s important to eliminate the hardware cause as soon as possible.
- Troubleshooting the hardware
- If you have intermittent crashes or blue screens check your memory and power supply.
- A flaky power supply can cause very hard to pin down errors, so it’s best to eliminate it as soon as possible.
- If it persists, start swapping out cards methodically
- Finally replace the motherboard and cpu (with the same model)
- If you have intermittent crashes or blue screens check your memory and power supply.
- If it is software, then whose software is causing the problem? Is it your program, the operating system, drivers, libraries or the compiler? (It is almost always your program
)
- If it was working and now hasn’t, you need to find out what changed.
- Has the user installed a new version of your software?
- Is there a new service pack or hotfix that could be interfering with the your software?
- Is the user running some crapware or spyware or even legitimate software that is interfering with yours?
- Has the user changed their work process and are now hitting different parts of your application?
- If it was working and now hasn’t, you need to find out what changed.
- Once you have ascertained it is your software and you can reproduce the problem reliably, you now need to find it in your software and fix it.
- Apply the standard methods to find the bug in your source and fix it
Oh well I could write more about this, but I’m tired and I’m going to bed.
Good night.
-
Update: qt and git-svn
Posted on May 11th, 2009 No commentsQT
I’m currently learning the ins and outs of the QT gui toolkit and I got to say that it is quite neat. It’s available for free at http://www.qtsoftware.com/. It seems to be dogged by a fair amount of controversy. Do some googling about KDE and QT and their licences. It’s been released under the LGPL which is good news for small commercial developers, since their commercial licences are quite expensive. There is much debate about it’s signals and slots system which uses a custom preprocessor and is only type checked at runtime.
Git-svn
I have also been playing with git-svn. Git is my favourite version control system. You can grab it from http://git-scm.com/. The windows version that I use day-to-day is available at: http://code.google.com/p/msysgit/ it’s got a gui installer for those allergic to the cli and it’s maturing quite fast.
Git-svn is a git command that allows you to contribute to a subversion based project using git. The first thing it does is download the entire subversion repository, including all branches and tags. This can take quite a while, but once you have it, browsing the projects history is a dream. I tested it out on some of the smaller Sourceforge projects and it seems quite foolproof.
For the larger projects, I would recommend you find a download of a preconverted git repository and work from there. With a bit of googling it should be no problem.
Happy Programming
-
What character set does C++ use?
Posted on May 4th, 2009 No commentsI believe the answer to this question, is that it depends on the implementation. The standard only states that char is wide enough to contain the “basic execution character set” and that wchar_t is wide enough to contain any character from all the character sets in all locales on an implementation.
-
TCHAR in new C++ programs
Posted on May 3rd, 2009 1 commentWheres the beginning C++ 4 article?
I have been debating with myself whether I should use TCHAR in my C++ examples in this blog.
When you generate a new C++ console project in Visual Studio 2008, it automatically includes
TCHAR and friends. I think this would be a bit confusing for the beginner who is trying to learn
C++ for the first time. The approach that I think I’m going to take, is to delete all the generated
files and let the beginner build his first program up from scratch in a new file using only standard
types.First a review of the standard C++ char types
A null terminated string is an array of chars with a null (0) character at the end
to signal the end of the string. It is usually referred to with a char* pointer to the
first letter in the string. Alternatively it can be embedded in a std::string or std::vector
which is safer. -
C++ Types 3
Posted on May 2nd, 2009 No commentsInteger Types
Unsigned integer types
These types only vary in the amount of bits they use to represent numbers. The numbers are encoded in the bits as binary. C++03 mandates a set of unsigned integer types and a minimum number of bits and range for each type. The actual number of bits is implementation specific as long as it follows the following rules
- (for the number of bits of each) unsigned char <= unsigned short int <= unsigned int <= unsigned long int.
- char (and thus unsigned char) has enough bits to store the basic character set.
- int has the “natural” bit size of the architecture (Usually 16, 32 or 64 bits)
Type
C++03 minimum bits
C++03 Minimum range
VS2008 bits
VS2008 Range
unsigned char
8
0-255
8
0-255
unsigned short int
16
0-65535
16
0-65535
unsigned int
16
0-65535
32
0-4294967295
unsigned long int
32
0-4294967295
32
0-4294967295
It is interesting to note that the following condition is legal in a C++ implementation:
- The number of bits in the following types can all be equal: unsigned char = unsigned short int = unsigned int = unsigned long int (It would have to be at least 32 bits to satisfy the standard)
- For example the following is legal:
Type
Bits
Range
unsigned char
60
0-1152921504606846976
unsigned short int
60
0-1152921504606846976
unsigned int
60
0-1152921504606846976
unsigned long int
60
0-1152921504606846976
Signed integer types
Signed integer types can be encoded in three different ways, sign magnitude, 1’s complement and 2’s complement. The C++03 standard allows an implementation to use any of these representations. However most implementations use 2’s complement. 2’s complement is really easy to understand, it’s just like the normal unsigned binary representation, except the highest digit represents the negation of the power of 2 of it’s position. For example 1011 means 1*(-8) + 0*4 + 1*2 + 1 = -8 + 2 + 1 = -5.
The signed types follow the same rules as the unsigned types (see above) and the ranges are as follows:
Type
C++03 minimum bits
C++03 Minimum range
VS2008 bits
VS2008 Range
signed char
8
-127 – 127
8
-128 – 127
short int
16
-32767 – 32767
16
-32768 – 32767
int
16
-32767 – 32767
32
-2147483648 – 2147483647
long int
32
-2147483647 – 2147483647
32
-2147483648 – 2147483647
chars
There are some special rules for chars. In many ways char is the “base” type of a C++ system. It’s the smallest addressable set of bits when programming in C or C++. Other types’ sizes are always measured in terms of chars, sizeof(T) returns the number of chars needed to store T.
- char, signed char and unsigned char types must:
- All have the same alignment requirements and have the same number of bits.
- Large enough to represent the basic character set.
- char can either be signed or unsigned, it depends on the implementation.
- VS2008 uses signed chars by default.
-
C++ Types 2
Posted on May 1st, 2009 1 commentEncoding Numbers
To start of with we will want to store numbers. Let’s assume we want to store unsigned integers. Believe it or not there are many ways of encoding unsigned integers into bits.
The simplest method I can imagine is the following, we could encode the number by the number of sequential 1 bits, e.g. 1 is 1, 11 is 2, 111 is 3, 1111 is 4 etc. Use 0 as a marker for the end of the number and we have 10 is 1, 1110 is 3 and 1111111110 is 9. You could represent any number using this scheme. The major disadvantage of this method is that the space used to represent the number is the same as the magnitude of the number. This means it will take 1 Megabit to represent the number 1048576. To resolve this we need to use a positional notation to represent a number.


