Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to automatically generate C header file using CMake?

Tags:

c

header

cmake

I'm looking for a way to automatically generate a header file. This file is the public interface of a library and i want to "fill" some structures and stuff before compilation.

For example, in the private header I have a structure with useful fields :

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

And I would like to have this structure in the public header without the fields but with the same size (currently done manually) this way :

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

I would like to have this automatically generated by CMake when creating the build system in order to avoid bad size struct in public interface when I change the struct in private header.

like image 289
claf Avatar asked Dec 06 '22 06:12

claf


2 Answers

In fact, CMake allow you to generate files (using configure_file (file.h.in file.h) ) and also to check a type size (using check_type_size ("type" header.h)) so it's easy to combine those two to have a proper public header. Here is the piece of code I use in 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)

And in the public header pub_type.h.in :

#define MY_SIZEOF_TYPE1_T ${MY_SIZEOF_TYPE1_T}

This works pretty good :)

like image 91
claf Avatar answered Dec 15 '22 01:12

claf


The Makeheaders tool (manual).

like image 42
Ollie Saunders Avatar answered Dec 15 '22 00:12

Ollie Saunders