I have shared library written in C++ which provides some APIs calls for different applications written in C++ too, now I want to use this library inside C programs. The original library contained data-types that are only valid for C++ like std::string and std::vector as follow:
typedef u_int32_t ApplicationID;
typedef std::string IPAddress;
typedef std::vector<int> SDLIST;
These data-types are being used as input parameters for the APIs:
register_client(IPAddress ip);
export(ApplicationID id, SDLIST *list);
But in C, we don't have string nor vector and these two data-types should be modified as follow:
typedef char* IPAddress;
typedef int* SDLIST;
I tried to do the following changes in my code:
typedef u_int32_t ApplicationID;
enter code here
#ifdef __cplusplus
typedef std::string IPAddress;
typedef std::vector<int> SDLIST;
#else
typedef char* IPAddress;
typedef int* SDLIST;
#endif
#ifdef __cplusplus
extern "C" {
#endif
register_client(IPAddress ip);
export(ApplicationID id, SDLIST *list);
#ifdef __cplusplus
}
#endif
My questions are:
Is this a correct way to build a library that can be used in both C & C++?
My shared library use Boost Interprocess library which is a wrapper for the standard POSIX shared memory calls. Whenever I try to link this shared library to any application, I should include the
lrt
again in the application. So my question is it possible to link the shared library statically to thelrt
library without having the need to include thelrt
in all applications that use my shared library?