Home > Blockchain >  Sharing static linkage across translation units
Sharing static linkage across translation units

Time:10-20

I am implementing a C project, to be compiled inside/along with a bigger C project. I have a big .c file (no headers), that I made recent additions into. And the purpose of these additions are logically separable from the big C file. I want to split it to two .c files, for better maintenance. However I face a complication.

Although logically and preferably separable, these theoretical separated units(.c files), must share some of the static functions and declarations made inside the original file. When tried to create a header file to contain the common dependencies (which is the common procedure), I struggled because of the following reasons (and what I tried).

  • The bigger project that my code is being buried into, has some of the same named definitions as my code, which is why they were static declarations originally. It works this way, but when I try to split the c files and create a header file, I had to remove the static keyword in some of them (to not get the error undefined reference), but this resulted in multiple declaration errors because of collision with the bigger project (expected but frustrating).
  • I could not change the naming of such functions and structs because they are strict to work with the outer project.
  • I am confused about what happens if the declaration in the header file is static, but the definition in the c file is not. Could not determine if this causes collision (theoretically it should?).
  • I did not want to repeat the static common dependencies in the the separated files, it just involves making copies of code, and contradicts my purpose of making maintenance simpler.

I guess what it comes down to is, can I create such a header file or files, that the linkage (exported symbols) are only visible to the c files that imported that header, and not from the other translation units. And it is required that these units that I am created can see each others definitions (because of the header file only they included).

Even when I ask it, it seems improbable, however I may be approaching the problem from a wrong viewpoint, which I would also like to learn about. Is what I am trying to do is impossible, and I should keep it as a single file?

If useful, I am developing a module for the Nginx web server.

CodePudding user response:

This is one of the rare cases where it makes sense to include a .c file.

First, you have your two .c files, eg. module1.c and module2.c. Then make a third file called mylibrary.c which contains just the following:

#include "module1.c"
#include "module2.c"

And compile just mylibrary.c. While this means you loose the ability to reduce compile time by compiling modules separately, you still have the logical separation of functions in different .c files while only exporting the functions you want.

For static functions that need to be shared between the submodules, make separate headers for those functions to include in each submodule.

For example:

module1_priv.h:

static void mod1_internal1(void);
void mod1_external1(void);

module2_priv.h:

static void mod2_internal1(void);
void mod2_external1(void);

module1.c:

#include "module1_priv.h"
#include "module2_priv.h"

static void mod1_internal1(void)
{
    // do something
}

void mod1_internal2(void)
{
    // do something
}

module2.c:

#include "module1_priv.h"
#include "module2_priv.h"

static void mod2_internal1(void)
{
    // do something
}

void mod2_internal2(void)
{
    // do something
}
  • Related