Question

When I create a static library, can the typedef structures cause name clashes?

Given the example header file below, I understand that myFun() will be an external symbol and could clash with any other library with a myFun() function. To avoid this, the best thing to do is give myFun() a longer and more specific name.

// myFile.h

typedef struct
{
    int myVar;
} myStruct;

void myFun(myStruct * input);

Is myStruct an external symbol that could cause naming clashes when linking with another library?

And why does it not get listed when I look at the .a static library file with nm myLib.a?

Was it helpful?

Solution

No, names are only visible outside a library (or source file) through linkage, and in C, types do not have linkage. C11 §6.2.2/2:

In the set of translation units and libraries that constitutes an entire program, each declaration of a particular identifier with external linkage denotes the same object or function.

Two struct definitions in different translation units (usually from inclusion of a common header) must match up to a certain degree of similarity to achieve compatibility with each other, but conceptually each translation unit has its own distinct set of data-types.

In C++, you would in theory have cause for concern, because class types, and typedef names declared along with a class type, do have linkage. However, in practice the linker would not see them.

So, there's really nothing to worry about.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top