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))的函數