[CMAKE] CMAKE_SIZEOF_VOID_P and project

Recommended Posts

Alundra    2316

Hi,

I'm using cmake to generate my project and I have recently learned the existence of "CMAKE_SIZEOF_VOID_P".

Is "CMAKE_SIZEOF_VOID_P" a portable solution ?

To generate project name for x86 and x64 I need to use this trick :

project(Test)
if( CMAKE_SIZEOF_VOID_P EQUAL 8 )
project(Test_x64)
else()
project(Test_x86)
endif()


Is the only way to handle that correctly ?

Thanks

Edited by Alundra

Share on other sites
Hiwas    5807

It is only partially portable.  For Visual Studio it is fine since CMake only outputs 32 or 64 bit specific solutions.  Unfortunately though, since it is a project generation time test it won't work on items which allow switching between 32 and 64 bits on the fly, for instance Xcode automatically inserts both targets.  As such, you need to really use a compile time test.  A version I've been using which seems to be pretty consistent is:

#define __STDC_LIMIT_MACROS
#include <stdint.h>

#if UINTPTR_MAX == 0xffffffff
# define BUILDING_32BIT
#elif UINTPTR_MAX == 0xffffffffffffffff
# define BUILDING_64BIT
#else
# error Unknown architecture.
#endif



[note]

Obviously this only works on compilers with stdint, VC2010-2012 have it, 2008 didn't and you have to deal with it using specific compiler tricks.

[/note]

Edited by AllEightUp

Share on other sites
Hiwas    5807

Somehow I overlooked that you were just trying to change the name.  The basic reasoning against using the flag to control output is still true; it is done at generation time and doesn't translate to the build environments which deal with multiple targets automatically.