¿Cómo agrego un enlazador o un indicador de compilación en un archivo CMake?


Estoy usando el compilador arm-linux-androideabi-g++. Cuando intento compilar un simple "¡Hola, Mundo!"programa que compila bien. Cuando lo pruebo agregando un simple manejo de excepciones en ese código, también funciona (después de agregar -fexceptions.. Supongo que está desactivado por defecto).

Esto es para un dispositivo Android, y solo quiero usar CMake, no ndk-build.

Por ejemplo - first.cpp

#include <iostream>

using namespace std;

int main()
{
   try{
   }
   catch(...)
   {
   }
   return 0;
}

./arm-linux-androideadi-g++ -o first-test first.cpp -fexceptions

Funciona sin problemas...

El problema ... Estoy tratando de compilar el archivo con un archivo CMake.

Quiero agregar el -fexceptions como una bandera. Lo intenté con

set (CMAKE_EXE_LINKER_FLAGS -fexceptions ) or set (CMAKE_EXE_LINKER_FLAGS "fexceptions" )

Y

set ( CMAKE_C_FLAGS "fexceptions")

Todavía muestra un error.

 155
Author: Peter Mortensen, 2012-08-02

5 answers

Supongamos que desea agregar esas banderas (mejor declararlas en una constante):

SET(GCC_COVERAGE_COMPILE_FLAGS "-fprofile-arcs -ftest-coverage")
SET(GCC_COVERAGE_LINK_FLAGS    "-lgcov")

Hay varias maneras de añadirlos:

  1. El más fácil (no limpio, pero fácil y conveniente, y solo funciona para compile flags, C & C++ a la vez):

    add_definitions(${GCC_COVERAGE_COMPILE_FLAGS})
    
  2. Añadiendo a las variables CMake correspondientes:

    SET(CMAKE_CXX_FLAGS  "${CMAKE_CXX_FLAGS} ${GCC_COVERAGE_COMPILE_FLAGS}")
    SET(CMAKE_EXE_LINKER_FLAGS  "${CMAKE_EXE_LINKER_FLAGS} ${GCC_COVERAGE_LINK_FLAGS}")
    
  3. Usando las propiedades de destino, cf. doc CMake compile flag target property y necesita conocer el destino nombre.

    get_target_property(TEMP ${THE_TARGET} COMPILE_FLAGS)
    if(TEMP STREQUAL "TEMP-NOTFOUND")
      SET(TEMP "") # Set to empty string
    else()
      SET(TEMP "${TEMP} ") # A space to cleanly separate from existing content
    endif()
    # Append our values
    SET(TEMP "${TEMP}${GCC_COVERAGE_COMPILE_FLAGS}" )
    set_target_properties(${THE_TARGET} PROPERTIES COMPILE_FLAGS ${TEMP} )
    

Ahora mismo uso el método 2.

 184
Author: Offirmo,
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
2018-08-22 20:24:03

En las versiones más recientes de CMake también puede establecer indicadores de compilador y enlazador para un solo destino utilizando target_compile_options y target_link_libraries respectivamente (sí, este último también establece opciones de enlazador):

target_compile_options(first-test PRIVATE -fexceptions)

La ventaja de este método es que puede controlar la propagación de opciones a otros destinos que dependen de este a través de PUBLIC y PRIVATE.

 98
Author: vitaut,
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
2015-12-08 14:31:07

Intenta establecer la variable CMAKE_CXX_FLAGS en lugar de CMAKE_C_FLAGS:

set (CMAKE_CXX_FLAGS "-fexceptions")

La variable CMAKE_C_FLAGS solo afecta al compilador de C, pero está compilando código de C++.

Agregar la bandera a CMAKE_EXE_LINKER_FLAGS es redundante.

 29
Author: sakra,
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
2012-08-02 19:27:56

También puede agregar banderas enlazadoras a un destino específico usando la propiedad LINK_FLAGS:

set_property(TARGET ${target} APPEND_STRING PROPERTY LINK_FLAGS " ${flag}")

Si desea propagar este cambio a otros destinos, puede crear un destino ficticio para vincularlo.

 0
Author: kaveish,
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
2018-04-11 14:29:07

Checkout las macros ucm_add_flagsy ucm_add_linker_flagsde ucm (mi conjunto de macros CMake útiles) - se ocupan de agregar banderas de compilador/enlazador.

 0
Author: onqtam,
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
2018-08-22 20:25:51