共用方式為


函式內嵌問題

如果您使用函數內嵌,您必須:

  • 在包含的頭檔中實作內嵌函式。

  • 在頭檔中已開啟內嵌。

// LNK2019_function_inline.cpp
// compile with: /c
// post-build command: lib LNK2019_function_inline.obj
#include <stdio.h>
struct _load_config_used {
   void Test();
   void Test2() { printf("in Test2\n"); }
};

void _load_config_used::Test() { printf("in Test\n"); }

然後

// LNK2019_function_inline_2.cpp
// compile with: LNK2019_function_inline.lib
struct _load_config_used {
   void Test();
   void Test2();
};

int main() {
   _load_config_used x;
   x.Test();
   x.Test2();   // LNK2019
}

如果您使用 #pragma inline_depth 編譯程式指示詞,請確定您已設定 2 或更新的值。 值為零將會關閉內嵌。 也請確定您使用 /Ob1/Ob2 編譯程序選項。

在不同模組上混合內嵌和非內嵌編譯選項有時會造成問題。 如果C++連結庫是以開啟函式內嵌來建立的(/Ob1/Ob2),但描述函式的對應頭檔已關閉(沒有選項),您會收到錯誤LNK2001。 函式不會從頭檔內嵌到程式代碼中,但由於函式不在連結庫檔案中,因此沒有解析參考的位址。

同樣地,使用函式內嵌但定義.cpp檔案中的函式,而不是頭檔中的專案也會取得LNK2019。 頭檔包含在認為適當的地方,但函式只會在.cpp檔案通過編譯程式時內嵌;因此,連結器會在其他模組中使用時,將函式視為未解析的外部函式。

// LNK2019_FIP.h
struct testclass {
   void PublicStatMemFunc1(void);
};

然後

// LNK2019_FIP.cpp
// compile with: /c
#include "LNK2019_FIP.h"
inline void testclass::PublicStatMemFunc1(void) {}

然後

// LNK2019_FIP_2.cpp
// compile with: LNK2019_FIP.cpp
// LNK2019 expected
#include "LNK2019_FIP.h"
int main() {
   testclass testclsObject;

   // module cannot see the implementation of PublicStatMemFunc1
   testclsObject.PublicStatMemFunc1();
}

另請參閱

連結器工具錯誤 LNK2019