gpt4 book ai didi

c++ - 链接到具有内联函数的静态库时出现问题

转载 作者:搜寻专家 更新时间:2023-10-31 00:47:57 26 4
gpt4 key购买 nike

我有一个静态库(除其他外)实现了一个小函数,它只从常量字符串表中返回一些字符串。此函数不会在库内的任何地方调用,但仍被声明为 inline。为清楚起见,它看起来像这样:

namespace flow
{
inline const char* GetName( BYTE methodType );
}

和实现:

const char* flow::GetName( BYTE methodType )
{
if ( methodType < 5 )
return cszNameTable[ methodType ];

return NULL;
}

在另一个项目中,我链接到这个库。我有正确的 .h 文件,并且我的代码中有 using namespace flow;。问题是,我收到链接器错误:

error LNK2001: unresolved external symbol "char const * __cdecl flow::GetName(unsigned char)" (?GetName@flow@@YAPBDE@Z)

现在我可以通过从静态库的函数声明中删除“inline”关键字来轻松解决这个问题。所以这是我的问题:

1) 为什么会出现这个错误?如何在不修改静态库源代码(不删除 inline 关键字)的情况下修复它?

2)不是在库本身内部调用的静态库函数中使用inline关键字有什么好处?当从另一个项目链接到库时,inline 关键字是否有任何影响(我想是的,但我不确定)?

最佳答案

1) Why does this error appear? How can i fix it without modifying the static library source code (without removing the inline keyword)?

声明函数为内联毫无意义。无论如何,您都必须在 header 中定义它们:

namespace flow
{
inline const char* GetName( BYTE methodType )
{
if ( methodType < 5 )
return cszNameTable[ methodType ];

return NULL;
}
}

2) What is the benefit of using the inline keyword in a static library function that is not called inside the library itself? Does the inline keyword have any effect when linking against the library from another project (i guess it does, but i'm not sure)?

inline 的效果是您可以并且必须在 header 中定义函数,因为实现 内联 函数必须在调用该函数的地方可见

关于c++ - 链接到具有内联函数的静态库时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3203365/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com