How to replace a symbol in a static library with the weak attribute

Suppose I have code like this:

lib.h:

void func();

      

lib.cpp:

void __attribute__((weak)) func()
{
    printf("original");
}

      

and this code will be compiled to libA.a

.

clang++ lib.cpp -o libA.a

      

Then in my test unit, I want to override this function func

, for example:

test.cpp:

#include "lib.h"
void func()
{
    printf("override");
}

      

and

clang++ test.cpp -lA

      

But in my result, the conclusion is always original

.

What is the correct way to have a weak

static lib function and then override it externally?

+3


source to share





All Articles