Pregunta

Estoy buscando una manera de generar automáticamente un archivo de encabezado. Este archivo es la interfaz pública de una biblioteca y quiero " llenar " algunas estructuras y cosas antes de la compilación.

Por ejemplo, en el encabezado privado tengo una estructura con campos útiles:

typedef struct mystuff_attr_t {
  int                      _detachstate;
  mystuff_scope_t          _scope;
  cpu_set_t                _cpuset;
  size_t                   _stacksize;
  void*                    _stackaddr;
} mystuff_attr_t;

Y me gustaría tener esta estructura en el encabezado público sin los campos pero con el mismo tamaño (actualmente hecho manualmente) de esta manera:

typedef struct mystuff_attr_t {
  char _opaque[ 20 ]; 
} mystuff_attr_t;

Me gustaría que CMake generara esto automáticamente al crear el sistema de compilación para evitar estructuras de tamaño incorrecto en la interfaz pública cuando cambio la estructura en el encabezado privado.

¿Fue útil?

Solución

De hecho, CMake le permite generar archivos (usando configure_file (file.h.in file.h) ) y también verificar un tamaño de letra (usando check_type_size (" type " ; header.h) ) por lo que es fácil combinar esos dos para tener un encabezado público adecuado. Aquí está el código que uso en CMakeList.txt:

# Where to search for types :
set (CMAKE_EXTRA_INCLUDE_FILES ${CMAKE_CURRENT_SOURCE_DIR}/private_type.h)

# Type1 :
check_type_size ("type1_t" MY_SIZEOF_TYPE1_T)

# Generate public header :
configure_file (${CMAKE_CURRENT_SOURCE_DIR}/pub_type.h.in ${CMAKE_CURRENT_BINARY_DIR}/pub_type.h)

# Need to set this back to nothing :
set (CMAKE_EXTRA_INCLUDE_FILES)

Y en el encabezado público pub_type.h.in:

#define MY_SIZEOF_TYPE1_T ${MY_SIZEOF_TYPE1_T}

Esto funciona bastante bien :)

Otros consejos

La herramienta Makeheaders ( manual ).

Escribiría un exe que crea el encabezado.

por ejemplo:

#include <stdio.h>

#define PUBLIC(TYPE) \
printf( "typedef struct %s { char _opaque[ %d ]; } %s;\n", #TYPE, sizeof(TYPE), #TYPE )

int main()
  {
  // start header stuff

  PUBLIC(mystuff_attr_t);

  // end header stuff
  }
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top