I have the job to create a shared library which should be useable as a replacement for several (older) versions of an other shared library.
Now the problem:
I have to combine:
Library a:
const char *mixer_ctl_get_enum_string(struct mixer_ctl *ctl, unsigned int enum_id);
const char *mixer_ctl_get_name(struct mixer_ctl *ctl);
Library b:
int mixer_ctl_get_enum_string(struct mixer_ctl *ctl, unsigned int enum_id, char *string, unsigned int size);
int mixer_ctl_get_name(struct mixer_ctl *ctl, char *name, unsigned int size);
I found out how to handle several amounts of input-params, but now they also have different return-types. I found examples for this in C++, but not for C.
How can I do this?
If C would work like Java, I would just implement both and everything is fine, but in C?
Thanks for your help & kind regards!
There is no easy or general solution.
In C++ you could package up functions into classes, and function names only have to be unique in the class. C doesn’t have this.
In C++, the return type and types of arguments count as part of a function name (so
void foo(int)andvoid foo(float)are actually different functions and the compiler knows which one to call). C doesn’t have this.In C there is a single, global namespace, and the types do not count as part of the function name. As others have noted, the standard C function names are different for different return types:
sqrt()returnsdoublebutsqrtf()returnsfloat.There are functions in C that can take a varying number of arguments; a classic example is
printf(). But these are tricky to write, and not a general solution to your problem. In the case ofprintf()there is a “format string” argument, and theprintf()function just has to trust that the format string correctly matches up with the arguments toprintf(). (Well, sinceprintf()is common, some compilers actually check the format string against the arguments, but your library functions don’t have this advantage!)I’ve done a lot of work in C, and the single global namespace is one of the most annoying limitations of C. Is there any chance that you can actually use C++ for this project? If you use the basic features of C++, you can treat it as “C with classes” and just take advantage of namespaces and function overloading.
Otherwise I think your best bet is to use a refactor tool, or a really good search-and-replace feature in a text editor, to change the function names to be globally unique. An obvious way to do this is to change every function to have the library name as a prefix:
Then you would have to refactor or search-and-replace the programs using the old libraries, but since the libraries are mutually contradictory you should have an easy time getting things working again.