Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Figure out function parameter count at compile time

I have a C library (with C headers) which exists in two different versions.

One of them has a function that looks like this:

int test(char * a, char * b, char * c, bool d, int e); 

And the other version looks like this:

int test(char * a, char * b, char * c, bool d) 

(for which e is not given as function parameter but it's hard-coded in the function itself).

The library or its headers do not define / include any way to check for the library version so I can't just use an #if or #ifdef to check for a version number.

Is there any way I can write a C program that can be compiled with both versions of this library, depending on which one is installed when the program is compiled? That way contributors that want to compile my program are free to use either version of the library and the tool would be able to be compiled with either.

So, to clarify, I'm looking for something like this (or similar):

#if HAS_ARGUMENT_COUNT(test, 5)     test("a", "b", "c", true, 20); #elif HAS_ARGUMENT_COUNT(test, 4)     test("a", "b", "c", true); #else     #error "wrong argument count" #endif 

Is there any way to do that in C? I was unable to figure out a way.

The library would be libogc ( https://github.com/devkitPro/libogc ) which changed its definition of if_config a while ago, and I'd like to make my program work with both the old and the new version. I was unable to find any version identifier in the library. At the moment I'm using a modified version of GCC 8.3.

like image 681
Florian Bach Avatar asked Nov 18 '20 07:11

Florian Bach


People also ask

How many parameter and function will take?

The main function can be defined with no parameters or with two parameters (for passing command-line arguments to a program when it begins executing). The two parameters are referred to here as argc and argv, though any names can be used because they are local to the function in which they are declared.

Which of the following function returns count of parameters provided to function?

func_get_args( ) returns an array of all parameters provided to the function, func_num_args( ) returns the number of parameters provided to the function, and func_get_arg( ) returns a specific argument from the parameters.

What is compile time parameter?

Compile time parameters are parameters utilized during the compilation of a template. These parameters can only be used via the SparkleFormation library. It is for this reason that compile time parameters are generally discouraged from use.

How many parameters a function should have?

Our functions should have the minimum number of arguments possible, if it have less than four argument, nice. Put a limit on the argument's number is not the ideal, we should strive to keep them as minimal as we can.


1 Answers

This should be done at the configure stage, using an Autoconf (or CMake, or whatever) test step -- basically, attempting to compile a small program which uses the five-parameter signature, and seeing if it compiles successfully -- to determine which version of the library is in use. That can be used to set a preprocessor macro which you can use in an #if block in your code.

like image 170
Sneftel Avatar answered Oct 12 '22 15:10

Sneftel