Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
476 views
in Technique[技术] by (71.8m points)

include - C - Libraries with the same function and variable names

I want to include two libraries which i built from the source code.

To go more into detail: These libraries are in general the same, except for some parameters which are different and set by a

#define Mode *X*

X describes the mode and describes which parameter set to use. E.g.

For x = 2 i want to use

#define N 3
#define Q 4

etc

In one library my x is for example 2 and in the other 3 which makes N 4 and Q 6 e.g.

Also these libraries contain the same functions which operate little different depending mode which is set.

I Need to include both of them and keep the options open to use both parameter sets.

Any ideas how to implement this correctly and clean? The whole thing only depends on the mode and to rename all containing functions and variables is unfortunately no option. But i have access to the source code and can make some changes.

I tried to built the libraries and include them in my main program but obviously i have a lot of naming collisions and the first definition which is found by the linker is set to be correct.

Another problem is that all the header files start with e.g.

 #ifndef CONFIG
 #define CONFIG

Which is standard and ok but I have all files two times so only the first header which is included by the linker sees that it isnt already defined and for the second its already defined so none of the code below is executed by the compiler.

Has someone a good idea how to solve this elegant?

I heard something about namespaces but i am not sure if this is even available for C and the right way to go.

BR

question from:https://stackoverflow.com/questions/65927679/c-libraries-with-the-same-function-and-variable-names

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You'll probably have to resort to loading the external libraries at run time.

If you're on Linux, you can use dlopen to open a library at runtime, then use dlsym to extract each variable and function you would want to use.

You could create a struct that would contain pointers to the variables and functions for an instance of the library, then populate a copy of the struct for each variation of the library. From there, you would use the function pointer from the appropriate struct instance to call a particular library function.

For example, suppose you have the following library files:

x2.c:

#include <stdio.h>

void foo()
{
    printf("in x2 foo
");
}

x3.c

#include <stdio.h>

void foo()
{
    printf("in x3 foo
");
}

And these are compiled into libx2.so and libx3.so respectively. Then your main source could do this:

#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>

struct lib {
    void (*foo)(void);
};

int main(void)
{
    struct lib lib2;
    struct lib lib3;

    void *l2 = dlopen("./libx2.so", RTLD_NOW);
    if (!l2) {
        printf("dlopen x2 failed: %s
", dlerror());
        exit(1);
    }
    void *l3 = dlopen("./libx3.so", RTLD_NOW);
    if (!l3) {
        printf("dlopen x3 failed: %s
", dlerror());
        exit(1);
    }

    lib2.foo = dlsym(l2, "foo");
    lib3.foo = dlsym(l3, "foo");

    lib2.foo();
    lib3.foo();

    dlclose(l2);
    dlclose(l3);
    return 0;
}

Output:

in x2 foo
in x3 foo

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...