GCC 链接器:静态 A 在链接其他共享库时看不到静态 B

GCC linker: staticA can't see staticB while linking other shared library

本文关键字:静态 链接 看不到 共享 GCC 其他      更新时间:2023-10-16

GCC 4.4.3,Ubuntu。

3个项目:

  • A(静态库)生成正常
  • B(静态库)构建正常
  • X(共享库或控制台应用程序)链接器错误

为什么这种情况不让链接X?

X calls B;
B calls A; // WHY???? linker error in X: B has undefined reference to stuff in A

这个案例有效:

X calls A; // this fact allows B access A
X calls B;
B calls A; // Now X linked just fine

完整代码:

//////////////////////////////
// StaticAAA.cpp
void FunctionAAA()
{
}
//////////////////////////////
// StaticBBB.cpp
void FunctionAAA();
void FunctionBBB()
{
    FunctionAAA();
}
//////////////////////////////
// App.cpp
void FunctionAAA();
void FunctionBBB();
int main()
{
#ifdef WHY_LINKER_ERROR_FIXED
    FunctionAAA();
#endif
    FunctionBBB();
    return 0;
}

gcc命令行上的库的顺序很重要-您需要按依赖关系顺序列出库。如果您有任何循环依赖关系,那么您可能需要列出至少一个库两次才能满足此要求。

在你的特殊情况下,你可能想要这样的东西:

$ g++ ... X.o -lB -lA ...