I had a weird bug. I have been running and testing a C program on my Mac, and things have been fine. Tonight I tried to deploy it by copying the code to my server. The server is not a Mac, but runs Ubuntu. I found that I am using two different versions of gcc. So I compiled it with gcc just like on the Mac, but instead of a clean compile, it have me a bunch of warnings. warning: incompatible implicit declaration of built-in function ‘round’ ... (and from the linker:) undefined reference to `round' undefined reference to `floor' undefined reference to `log10' The output indicates a problem with the Math functions, though I had included math.h. After looking on the web, I found a work-around, which was to use -lm and -fno-builtin in the command line: gcc -fno-builtin -lm myprogram.c -o myprogram This compiles cleanly. But when I run the program, it does not give me the output I expect. This is complicated by the nature of the program; it is a cgi program, invoked by a HTTP POST from another program I'm working on under Google Web Toolkit. My first suspicion is that it was receiving corrupted input. The file that it creates looks nothing like what I expected, and did not match the file created on the Mac. So what's the problem? By faking the input to the program on both the Mac and the PC, I found that the input was being read correctly and matched under both versions. This surprised me. I thought that the likely candidate would be the POST mechanism. This is nice because it simplifies the debugging process. Here's how I determined that the input was being read correctly. First, I replaced the compiled program in the cgi-bin with a simple program. The simple program reads the input, gives some minimal output to satisfy the calling (POSTing) program, and appends the input it read to a file. Then I could copy this data from the file, and echo it to my program, like so. echo "data data data" | myprogram Doing this on both systems, along with some special debugging output added to my program, allowed me to check that the input data was not the issue. Instead, I found that the two programs behaved differently given the same input! This is very surprising considering that the source code is almost completely identical. The only changes are a few Boolean variables, such as a DEBUG flag that I set to true when I'm testing the program. So it seems to come down to the gcc command. Inspecting the debug output of the program on each machine, I found that some numbers generated internally that should have been exactly the same were different. For example, the PC reported the numbers 170, 0, 0, 30, while the Mac had 20, 0, 150, 30 (which is correct). Looking at the code, I found that the numbers that were different were produced by the round statement. Looking deeper, I found that the round function returned zero. I suppose it is not properly defined. The odd thing is that the Mac's version of gcc is the older one. I added another switch (-std=c99), and found that it worked on the PC fairly well. gcc -std=c99 -fno-builtin -lm myprogram.c -o myprogram The only problem is that it generates a new warning: In function ‘writeFile’: warning: implicit declaration of function ‘close’ As you may have guessed, function "writeFile" writes a file. To fix this, I changed "close(filehandle)" to "fclose(filehandle)". Then the program compiled cleanly. Now the program works well on both machines. -- Michael Weeks, copyright 2009