[CMake] Detecting 32/64 bit systems

Michael Wild themiwi at gmail.com
Mon Feb 14 04:16:57 EST 2011


But there's one important pitfall: fat binaries. E.g. on Mac OS X if
CMAKE_OSX_ARCHITECTURES is set to multiple architectures (especially, if
they're mixed 32/64 bit) you can't rely on the value of
CMAKE_SIZEOF_VOID_P. You can't do anything about this stuff in your
CMake code, because you are compiling both for 32 _AND_ 64 bit at the
same time. In order to solve this problem, you'll want to look at the
documentation of the CheckTypeSize module.

I usually do something like this:

include(CheckTypeSize)
check_type_size("void*" SIZEOF_VOID_P BUILTIN_TYPES_ONLY)
configure_file(config.h.in "${CMAKE_CURRENT_BINARY_DIR}/config.h" @ONLY)

Where config.h.in contains (at least):

/* Set to sizeof(void*) */
@SIZEOF_VOID_P_CODE@

The variable SIZEOF_VOID_P_CODE defined by the check_type_size() macro
contains preprocessor code defining the symbol SIZEOF_VOID_P to the
appropriate number depending on the current architecture.

HTH

Michael

On 02/14/2011 09:57 AM, Yngve Inntjore Levinsen wrote:
> I have done the same thing myself, using size of INT instead of P which is of course following the exact same logic. 
> After attempting several strategies I found this one to be the safest way to detect 32/64bit systems, so I recommend using SIZEOF as well.
> 
> Cheers,
> Yngve
> 
> On Sunday 13 February 2011 08:00:41 PM Ryan Pavlik wrote:
>> Yep CMAKE_SIZEOF_VOID_P will be 4 if you've chosen a generator like "Visual
>> Studio 9.0 2008", and 8 if you've chosen "Visual Studio 9.0 2008 Win64"
>>
>> Ryan
>>
>> On Sat, Feb 12, 2011 at 11:41 AM, Dominik Szczerba <dominik at itis.ethz.ch>wrote:
>>
>>> Quite right.
>>>
>>> So you claim that on Windows CMAKE_SIZEOF_VOID will tell me the
>>> *target* build system, not the physical one, while it will mach
>>> CMAKE_SYSTEM_PROCESSOR if the both are the same?
>>>
>>> Many thanks for the clarification.
>>>
>>> Dominik
>>>
>>> On Sat, Feb 12, 2011 at 6:34 PM, Ryan Pavlik <rpavlik at iastate.edu> wrote:
>>>> CMAKE_SIZEOF_VOID_P will tell you the difference between target
>>> bit-widths:
>>>> 8 (for 64 bit) vs 4 (for 32 bit) - remember that just because you're
>>> running
>>>> on a 64-bit system doesn't mean you're compiling a 64-bit binary.
>>>> Ryan
>>>>
>>>> On Sat, Feb 12, 2011 at 11:29 AM, Dominik Szczerba <dominik at itis.ethz.ch
>>>>
>>>> wrote:
>>>>>
>>>>> OK, got it experimentally. You have
>>>>>
>>>>> CMAKE_SYSTEM_PROCESSOR
>>>>>
>>>>> But wait, is the description below correct?
>>>>>
>>>>> http://www.cmake.org/Wiki/CMake_Useful_Variables
>>>>>
>>>>> Opposed to the explanations there, which I saw, but took as not what I
>>>>> need, on my systems it amounts to i686 and x86_64, exactly what I
>>>>> need.
>>>>>
>>>>> Dominik
>>>>>
>>>>> On Sat, Feb 12, 2011 at 6:19 PM, Dominik Szczerba <dominik at itis.ethz.ch
>>>>
>>>>> wrote:
>>>>>> Hi,
>>>>>>
>>>>>> I can't really find a clear solution in google: how would one go about
>>>>>> detecting if the system is 32 or 64 bit? There are UNIX, WIN32 etc.
>>>>>> flags, but nothing really for the architecture.
>>>>>>
>>>>>> Thanks a lot,
>>>>>> Dominik
>>>>>>



More information about the CMake mailing list