如何使用CMake自动生成C头文件?

时间:2022-10-26 02:05:20

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 :

例如,在private header中,我有一个包含有用字段的结构:

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.

我希望CMake在创建构建系统时自动生成这个结构,以便在更改私有头中的结构时避免公共接口中的不良大小结构。

3 个解决方案

#1


9  

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 :

事实上,CMake允许您生成文件(使用configure re_file (file.h)。也可以检查类型大小(使用check_type_size(“type”header.h)),这样就可以很容易地将这两种类型组合在一起,得到一个合适的公共标题。这是我在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 :

在公共标题pub_type.h中。:

#define MY_SIZEOF_TYPE1_T ${MY_SIZEOF_TYPE1_T}

This works pretty good :)

这个效果很好:)

#2


3  

The Makeheaders tool (manual).

Makeheaders工具(手动)。

#3


2  

I would write an exe that creates the header.

我会写一个exe来创建标题。

for example:

例如:

#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
  }

#1


9  

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 :

事实上,CMake允许您生成文件(使用configure re_file (file.h)。也可以检查类型大小(使用check_type_size(“type”header.h)),这样就可以很容易地将这两种类型组合在一起,得到一个合适的公共标题。这是我在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 :

在公共标题pub_type.h中。:

#define MY_SIZEOF_TYPE1_T ${MY_SIZEOF_TYPE1_T}

This works pretty good :)

这个效果很好:)

#2


3  

The Makeheaders tool (manual).

Makeheaders工具(手动)。

#3


2  

I would write an exe that creates the header.

我会写一个exe来创建标题。

for example:

例如:

#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
  }