c/c++中的__attribute__((weak))使用

main.cpp

#include <stdio.h>
#include "g_test.h"

extern bool testFunc();

bool  __attribute__((weak))testFunc()
{
    warnf("this is weak api\n");
    return false;
}

int main()
{
    testFunc();
    return 0;
}

 g_test.h

#ifndef  __G_TEST_H_
#define __G_TEST_H_


bool testFunc();

#endif

 g_test.cpp

#include "g_test.h"
#include <stdio.h>

bool testFunc()
{
    printf("this  is strong api\n");
    return false;
}

输出结果是调用到g_test.cpp中的testFunc函数中

如果g_test.cpp中没有实现,则调用的__attribute__((weak))的函数

 

posted @ 2020-09-02 15:26  唯一诺  阅读(1535)  评论(0编辑  收藏  举报