gpt4 book ai didi

c++ - 内联和 dlimport/dllexport

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:38:18 28 4
gpt4 key购买 nike

现在我分析一些不是我写的旧代码。在 header 中有许多这样的声明:

SVPDSDKDLLEXPORT inline C3vec mult(C3vec src, D3DXMATRIX &m);

SVPDSDKDLLEXPORT定义为_declspec(dllexport),如果在SVPDSDK中使用; as _declspec(dllimport),如果它用于任何使用 SVPDSDK.dll 的项目。在这里内联对我来说似乎很奇怪,因为标题中没有定义,它在 .cpp 文件中,但是 SVPDSDK 的编译和链接以及使用各自 DLL 的所有项目都可以毫无问题地执行。我假设,它只是被忽略并且函数被导出,就好像它没有被内联一样。

我发现了这个讨论: C++ : inline functions with dllimport/dllexport?

看起来我应该从所有此类声明中删除“内联”,不要混合内联和导出/导入。但后来我在 MSDN 中找到了这个主题: http://msdn.microsoft.com/en-us/library/xa0d9ste

有些地方没看懂。

You can define as inline a function with the dllexport attribute. In this case, the function is always instantiated and exported, whether or not any module in the program references the function. The function is presumed to be imported by another program.

首先,“函数总是被实例化”是什么意思?我只找到了有关 C++ 中模板函数实例化的主题,没有找到任何其他实例化。它是否仅与模板连接?

其次,“功能始终是导出的”。我完全不明白。在某些情况下是否可能不导出带有 declspec(_dllexport) 的函数?在什么情况下?

现在关于导入:

You can also define as inline a function declared with the dllimport attribute. In this case, the function can be expanded (subject to /Ob specifications), but never instantiated. In particular, if the address of an inline imported function is taken, the address of the function residing in the DLL is returned. This behavior is the same as taking the address of a non-inline imported function.

同样,我不明白,在这种情况下实例化是什么意思。

在写这个问题并从 MSDN 分析主题时,我得出一个结论,同时导出/导入和内联的函数仅在其项目本身(在我的案例中是 SVPDSDK)中内联,并且是非内联在所有导入项目中。它没有在 MSDN 主题中明确声明。如果我不在任何使用它的项目中导入它,并且我在它的头文件中没有定义,它将是一个普通的内联函数,所以我会得到一个链接错误。然后在我看来,混合内联和导出/导入是可以接受的,认为它与上面提到的 stackoverflow 讨论中的答案相矛盾。我说得对吗?
而且我仍然不理解有关内联函数实例化的所有这些词。

很抱歉,我将一些问题合并到一个主题中,但我不知道如何将其分成不同的主题,因为它们由同一问题和相同 Material union 在一起。但是,如果有人能为我澄清这个问题,我将不胜感激。

最佳答案

事实上,inline 是优化器的一种提示。编译器仍然可以生成一个带有主体的真实函数,将参数压入堆栈等。这不会破坏任何逻辑。如果您的“内联”函数有超过 10000 行代码,它肯定会这样做。微软甚至有特殊的 __forceinline 关键字。猜猜为什么要引入它。

函数总是被实例化和导出...

这里的措辞可能并不完美。实例化在这里意味着将生成一个主体和一个入口点。这与模板实例化无关。整个段落意味着__declspecinline更重要。

对于dllimport,他们基本上写成dllimport阻止在当前二进制模块中生成这个内联函数的主体,而内联扩展仍然是可能的。

关于c++ - 内联和 dlimport/dllexport,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11927173/

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