views:

137

answers:

3

I'm running Xcode in OS X 10.6 on a Core 2 Duo. Here's a short program:

#include <stdio.h>

int main () {
    long a = 8589934592L;
    printf("a = %li\n", a);
    return 0;
}

When I compile this from the command line (gcc -pedantic) I get no errors or warning. When I compile this in Xcode in debug configuration, I get no errors or warnings. When I compile this in Xcode in release configuration, I get a warning: "Overflow in implicit constant conversion".

Longs should be 64-bit. And the program runs fine. So what's going on, and how do I get rid of this warning?

+2  A: 

Check sizeof(long) and sizeof(long long) to see if your assumption about long being a 64-bit type is correct.

I just tried your program out with Xcode 3.2.1 on Mac OS X 10.6.1 and didn't get that warning. I did manage to get the warning by setting the target configuration to "32-bit universal" instead of "Standard 32/64-bit universal". Make sure you're building for the right machine type!

Carl Norum
Sorry, should have explained: OS X uses the LP64 data model (see http://developer.apple.com/macosx/64bit.html ), in which longs are 64 bit.
Everett
I guess there is some Xcode project setting which indicates you're making 32-bit safe code.
Carl Norum
Thanks Carl --- yes, that was the problem. I'm new to Xcode, and I couldn't find that setting until after getting hints here.
Everett
A: 

The problem here is that longs are 32-bit, not 64. long long is 64 bit.

On a Mac Pro running 10.6

#include <stdio.h>
int main() {
    printf("%d.\n", sizeof(long));
    printf("%d.\n", sizeof(long long));

}

outputs

4.
8.
mbarnett
As Rüdiger points out, this depends on the build settings. If you select the project and Get Info, you can set the architecture to 32-bit Universal, 64-bit Intel, or the default 32/64-bit Universal. Depending on your choice, the program you include above will give different outputs...
Everett
+1  A: 

In the build settings, check "Architectures". If this is "Standard (32/64-bit universal)", then universal binaries with 32 bit and 64 bit versions will be built. The warning is for the 32 bit build.

The difference you're seeing is from "Build Active Architecture Only" being checked in Debug configuration, but not in Release configuration. So the 32 bit version is not built when you compile for Debug on 64 bit, and thus there's no warning.

Rüdiger Hanke
Ah! Thank you! That's exactly the issue.
Everett

related questions