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
C++ Check If Statement Can Be Evaluated Constexpr
Best Way to Start a Thread as a Member of a C++ Class
C++ Frontend Only Compiler (Convert C++ to C)
How to Access a Global Variable Within a Local Scope
Why Don't I Need to Specify "Typename" Before a Dependent Type in C++20
How to Detect Possible/Potential Stack Overflow Problems in a C/C++ Program
What Is Allowed in a Constexpr Function
How to Do a C++ Style Compile-Time Assertion to Determine MAChine's Endianness
How to Use Cmake_Export_Compile_Commands
Why Define Operator + or += Outside a Class, and How to Do It Properly
What Data Structure, Exactly, Are Deques in C++
How to Use C++ Modules in Clang
Initialize Std::Array with a Range (Pair of Iterators)
Constructor Conditionally Marked Explicit
Implementation of Operators for Enum Class