I’m creating some static libraries (MyLib, OtherLib) that contain C or Objective-C code, and I’d like each module to be able to access the name of the static library.
I could do a
const char* mylibname = "MyLib";
and use it everywhere. However, I want a more convenient method, where each file in every library can access its library through constant currentlibname. So when a file is in MyLib, currentlibname´s value is “MyLib”, when it is in OtherLib, its value is “OtherLib”.
Any ideas on the most convenient solution? Ideally all the magic should be in one place, without the need to adjust every file besides including something that declares mylibname.
Regards,
Jochen
It obviously depends upon the operating system, the compiler, the builder.
If you’ve got dynamic libraries on Linux, you could retrieve at runtime some information, perhaps thru
/proc/self/mapsand maybedladdr(and perhaps even return address builtins).My suggestion would be to define a preprocessor symbol during the building of each library, perhaps with something like
CFLAGS += -DTHIS_LIBRARY_NAME=MyLibin yourMakefileand have inmylib.hsomething using it. For C I might suggest to put inmylib.hsomething likeBTW, you could even put the above code in a common single header file included by your libraries main header file.
But I am a bit surprised you ask that. A better way might be to give an API for the version of your library, see what gtk does about feature tests.