Automatically Generate C++ File from Header

Automatically Generate C Code From Header

UML modeling tools are capable of generating default implementation in the language of choice. Generally there is also a support for importing source code (including C headers). You can try to import your headers and generate source code from them. I personally have experience with Enterprise Architect and it supports both of these operations.

Automatically generate header file for custom C library

There is no need for specific headers for static and dynamic libraries. You have to provide only one header list.h which can be used for both purposes. It contains a list of all functions and structures declarations you use in list.c and which should be usable by other programs, too.

To create and use either type of libraries, you have to change the parameters for the compiler respectively the linker. In CMake this is done by passing add_library the key word STATIC or SHARED.

How to automatically generate C header file using CMake?

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 :)

How to generate the C and C++ header file for a C++ library/class?

I think this link answered my question about the C header:
Can I use shared library created in C++ in a C program?

For the C++ header hiding private/protected, I discovered the PIMPL idiom that seems perfect. Thanks Caleth.



Related Topics



Leave a reply



Submit