Compilar proyecto de 32 bits vs 64 bits usando CMake


¿Cómo puedo especificar que CMake debe usar un valor link_directories diferente dependiendo de si el objetivo es de 32 bits o 64 bits? Por ejemplo, los binarios de 32 bits necesitan enlazar con Boost de 32 bits, los binarios de 64 bits necesitan enlazar con Boost de 64 bits.

Author: yman, 2010-10-27

4 answers

Haces algo en este sentido

  if( CMAKE_SIZEOF_VOID_P EQUAL 8 )
    set( BOOST_LIBRARY "/boost/win64/lib" )
  else( CMAKE_SIZEOF_VOID_P EQUAL 8 )
    set( BOOST_LIBRARY "/boost/win32/lib" )
  endif( CMAKE_SIZEOF_VOID_P EQUAL 8 )
  set( CMAKE_EXE_LINKER_FLAGS ${BOOST_LIBRARY} )
 26
Author: Eugene Smith,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-10-27 02:42:09

Sé que es una pregunta bastante vieja. Pero todavía está en la parte superior cuando se busca con Google "cmake 32 64". Tengo una respuesta similar a la de user434507, pero un poco más legible en mi opinión (no me gusta la construcción if-else en cmake, se ve feo):

math(EXPR BITS "8*${CMAKE_SIZEOF_VOID_P}")
set(BOOST_LIBRARY "/boost/win${BITS}/lib")
set(CMAKE_EXE_LINKER_FLAGS ${BOOST_LIBRARY})

Esto apuntará BOOST_LIBRARY camino a / boost / win32/lib o / boost / win64/lib, dependiendo de tu arquitectura.

 11
Author: rominf,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-09-01 16:15:40

Para Boost específicamente, debe usar

FIND_LIBRARY(Boost 1.44 COMPONENTS ...)

Entonces la variable CMake Boost_LIBRARY_DIRS contendrá la ruta correcta de la biblioteca, la cual debe ser establecida usando LINK_DIRECTORIES, por ejemplo

LINK_DIRECTORIES(${Boost_LIBRARY_DIRS})

El caso más general se describe correctamente en la respuesta de user434507.

 4
Author: Martin,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-10-27 08:12:53

Basado en rominf me apareció la siguiente solución (para Windows). Instalo bibliotecas boost en: C:\Boost_32 y C:\Boost_64

En listas de CMakeLists.txt

math(EXPR BITS "8*${CMAKE_SIZEOF_VOID_P}")
set(BOOST_ROOT C:/Boost_${BITS})  

find_package(Boost 1.64.0 COMPONENTS ... )

INCLUDE_DIRECTORIES( ${Boost_INCLUDE_DIR}  )
LINK_DIRECTORIES(${Boost_LIBRARY_DIR})

Explicación:

  • CMAKE_SIZEOF_VOID_P es igual a 4 en la plataforma de 32 bits, y 8 en la plataforma de 64 bits.
  • La expresión 8*${CMAKE_SIZEOF_VOID_P} evaluará a 32 o 64, respectivamente.
  • C:/Boost_${BITS} se convierte en C:/Boost_32 o C:/Boost_64 automáticamente

Ventajas:

  • Usted no necesita condicionales (y en mi CMakeLists hay demasiados ya),
  • Es 90% cómo 'debería' incluir Boost con CMake.
 0
Author: Nick,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-09-01 15:32:50