Is there a macro that checks if a given function exists?

advertisements

My application is using a library, and there are many versions of this library. I'm calling a function from this library. The name of the function is function_name (just an example) in one version of the library, but in other versions the same function has another name, another_function_name.

Is there a macro that checks if a given function exists?

Usage example:

#ifexist function_name
#define MYFUNCTION function_name
#else
#define MYFUNCTION another_function_name
#endif


Is there a macro that checks if a given function exists?

No. You are looking at the wrong end of the process of translating your c++ source files into an executable. Macro processing occurs very early on in the process. Resolving external references and building the executable is the very last step. The preprocessor doesn't know about libraries. It doesn't even know about functions.

At a minimum that library should be associated with some header files that you #include in your source code. You yourself should not have to declare the classes and functions that that library provides. A well-build library will also define preprocessor symbols that specify the version of the library. For example, The header some_library.h may define the symbols SOME_LIBRARY_MAJOR_VERSION and SOME_LIBRARY_MINOR_VERSION.

It is these preprocessor symbols that you should use to determine what function you should call. Suppose you know that you should be calling function_name with version 1.3 of the library and earlier, but another_function_name with version 1.4 and later. You can make a preprocessor symbol MYFUNCTION that is defined based on the library's version ID macros:

#if (SOME_LIBRARY_MAJOR_VERSION == 1) && (SOME_LIBRARY_MINOR_VERSION <= 3)
#define MYFUNCTION function_name
#else
#define MYFUNCTION another_function_name
#endif