如何在不同的静态库中拥有函数的声明和定义

How to have the declaration and the definition of a function in different static libraries

本文关键字:拥有 函数 声明 定义 静态      更新时间:2023-10-16

我想让这件事发生。在一个静态库中声明函数,并在另一个静态库中声明函数的定义。下面是一个示例:

在利巴·

// Foo.h
class Foo
{
public:
   void Bar() const;
};

在LibB.a

// FooPlatformSpecific.h
inline void Foo::Bar() const
{
}

然后在 LibC.dylib 中

// Myfile.cpp
#include "Liba/Foo.h"
void useTheFunction()
{
   Foo foo;
   foo.Bar();
}

我需要使 Bar 内联一些评论,这样它就不会生成重复的符号。现在我的问题是它没有链接它给了我一个未定义的符号。

一种解决方案是使 Bar 定义不内联,而是在 LibB.a 的 cpp 中,但这会阻碍编译器的内联可能性。

有没有办法解决这个问题?

这是行不通的,因为声明驻留在标头中,而定义驻留在库中。所以它们已经分开了,如果你在 .h 中声明并在 .cpp 中实现.