链接器工具警告 LNK4217

“filename_1.obj”中定义的符号“symbol”由函数“function”中的“filename_2.obj”导入

为符号指定了 __declspec(dllimport),即使该符号是在同一映像的目标文件中定义的。 删除 __declspec(dllimport) 修饰符以解决此警告。

备注

“symbol”是在映像中定义的符号名称。 “function”是导入符号的函数

使用 /clr 选项进行编译时,不会出现此警告。

如果尝试将两个模块链接在一起,也可能会出现 LNK4217,此时应使用第一个模块的导入库编译第二个模块。

// main.cpp
__declspec(dllimport) void func();

int main()
{
    func();
    return 0;
}

然后,

// tt.cpp
// compile with: /c
void func() {}

尝试按此处所示编译这两个模块将导致 LNK4217:

cl.exe /c main.cpp tt.cpp
link.exe main.obj tt.obj

要修复该错误,请在编译这两个文件后,将 tt.obj 传递给 lib.exe 以创建一个 .lib 文件,然后将 main.obj 与 tt.lib 链接,如下所示:

cl.exe /c main.cpp tt.cpp
lib.exe tt.obj /export:func /def
link.exe main.obj tt.lib

另请参阅

链接器工具警告 LNK4049
链接器工具警告 LNK4286
dllexport、dllimport