[CMake] CMAKE_SIZEOF_VOID_P question

Thomas Nilefalk thomas at junovagen.se
Fri Apr 14 11:58:04 EDT 2017


I'm developing a library on and for a multitude of platforms and would 
like to provide both 32 and 64-bit versions for all platforms this is 
relevant for.

I'm currently on Ubuntu 64bit with CMake 3.5.2 and use the "-m32" 
compiler switch to do the 32-bit compile.

I'm trying to understand why GNUInstallDirs are not setting 
CMAKE_INSTALL_LIBDIR to what I think it should. While doing that I 
discovered that CMAKE_SIZEOF_VOID_P is not set as I thought it would 
either.

According to the documentation CMAKE_SIZEOF_VOID_P it is determined by 
running the compiler. However it seems that it is not using pertinent 
flags from the configuration, in particular CMAKE_C_FLAGS and 
CMAKE_CXX_FLAGS.

Having a CMakeLists.txt containing:

> cmake_minimum_required(VERSION 2.8.5)
> project(X C)
> try_run(run_result_var compile_result_var /tmp /tmp/size.c)
> message(STATUS "Sizeof=" ${run_result_var})
> message(STATUS "CMAKE_SIZEOF_VOID_P=" ${CMAKE_SIZEOF_VOID_P})

and size.c with:

> int main(int argc, char **argv) {
>     return sizeof(void *);
> }

With no flags I get:

> -- Sizeof=8
> -- CMAKE_SIZEOF_VOID_P=8

And with CMAKE_C_FLAGS set to "-m32" I get:

> -- Sizeof=4
> -- CMAKE_SIZEOF_VOID_P=8

If this is by design, then I think the documentation is unclear, if not 
plain wrong:

> This is set to the size of a pointer on the target machine, and is 
> determined by a try compile.
> If a 64-bit size is found, then the library search path is modified 
> to look for 64-bit libraries first.

Or am I missing something?

/Thomas



More information about the CMake mailing list