c++ - 继承自模板的 MSVC DLL 导出类导致 LNK2005 已定义错误

标签 c++ visual-studio templates dll lnk2005

我跟踪了一个大型项目中的一个错误 3 天,终于得到了一个最小可重现的示例。我想分享这个问题,并就 Visual Studio 的奇怪行为提出一些问题。

当您导出一个继承自实例化模板类的类时,例如

class __declspec(dllexport) classA : public Template<double>{}

MSVC 还会导出实例化的模板类Template<double>在 DLL 中。

如果消费者包括"template.h"然后在他的代码中实例化一个 Template<double> , qnd同时,链接到上面的DLL,他会得到两个Template<double>的定义,这会导致 LNK2005LNK1169 错误。 Microsoft DLL export and C++ templates 中讨论了此问题.

这是此问题的最小可重现示例(完整代码为 here ):

// ----------- Project AA ------------
// aa/CMakeLists.txt
cmake_minimum_required(VERSION 3.1)
project(AA)
add_library(AA SHARED classA.cpp)                          //(1)
set_target_properties(AA PROPERTIES COMPILE_FLAGS "-DBUILD_AA")

// aa/config.h
#pragma once

#ifdef _WIN32
#  ifdef BUILD_AA
#    define AA_API __declspec( dllexport )
#  else
#    define AA_API __declspec( dllimport )
#  endif
#else
#   define AA_API
#endif

// aa/template.h
#pragma once
template<class T>
class Template {
public:
    Template() {}
};

//  aa/classA.h
#pragma once
#include "config.h"
#include "template.h"
class AA_API classA : public Template<double> {         //(2)
public:
    int fun();
};

// aa/classA.cpp
#include "classA.h"
int classA::funA(){return 123;}

// ----------- Project Main ----------
//CMakeLists.txt
cmake_minimum_required(VERSION 3.1)
project(Main)
set (CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
set (CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
add_subdirectory(aa)
add_executable(Main main.cpp test.cpp)
target_link_libraries(Main PUBLIC AA)

// main.cpp
#include "aa/classA.h"
#include <iostream>
int main(){
    Template<double> a;                                //(3)
    //classA aa;                                       //(4)
    //std::cout << aa.funA() << std::endl;             //(5)
    return 0;
}

// test.cpp
#include "aa/template.h"
class classB {
    Template<double> t;
};
class classC : public Template<double> {};

void fun() {
    Template<double> b;                               //(6)
    //class classB;                                   //(7)
    //class classC;                                   //(8)
}

我在 VS2015 Debug 模式下编译代码,它给出了多重定义的错误

1>------ Build started: Project: AA, Configuration: Debug x64 ------
1>  classA.cpp
1>  AA.vcxproj -> D:\sandbox\build\aa\Debug\AA.dll
2>------ Build started: Project: Main, Configuration: Debug x64 ------
2>  main.cpp
2>AA.lib(AA.dll) : error LNK2005: "public: __cdecl Template<double>::Template<double>(void)" (??0?$Template@N@@QEAA@XZ) already defined in test.obj
2>D:\sandbox\build\Debug\Main.exe : fatal error LNK1169: one or more multiply defined symbols found
========== Build: 1 succeeded, 1 failed, 1 up-to-date, 0 skipped ==========

如果我进行其中之一以下更改,将不会出现错误:

  1. 更改为 Release模式
  2. 删除 AA_API(2)并在 (1) 处将链接模式更改为 STATIC
  3. 评论 (3)并取消注释 (4)(5)
  4. 评论 (6)并取消注释 (7)(8)
  5. 在 Linux 中通过 gcc 编译
  6. 在 VS 中更改项目参数:添加 /FORCE:MULTIPLE到项目 Main 的链接器

问题:

为什么更改 1、2、3、4 和 5 有效?我认为 3 和 4 可以工作太奇怪了。

如何在 Visual Studio 中正确解决这个问题?

最佳答案

我最近遇到了同样的问题。我最终设法修复了它,所以我会分享我的知识。

问题来源:

模板被多次实例化。那是因为您使用了隐式实例化。当你声明 class AA_API classA 时一次,当你声明 Template< double > a; 时在 main 中一次;在主要。

这意味着您将有多个模板定义。

  • 1,我不确定它为什么在 Release模式下工作(认为这是我对模板缺乏深入了解)
  • 2,3,4,当你摆脱任何隐式模板实例化时,你就摆脱了多重定义
  • 5,也许 gcc 以不同的方式进行实例化,或者某处有一个强制乘法标志......我不知道
  • 6,这并没有解决你的问题,它只是强制接受多个瞬间。

解决方案:

显式实例化。

// aa/template.h
#pragma once
template<class T>
class Template {
public:
    Template() {}
};
template class Template<double>;  // Put this line after your template class.

注意:如果模板类在不同的项目中,需要导出实例化。

我希望这能解决您的问题。它修复了我的问题。

关于c++ - 继承自模板的 MSVC DLL 导出类导致 LNK2005 已定义错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44960760/

相关文章:

时间:2019-03-08 标签:c++cgifilestreaming

c++ - 如何遍历 std::index_sequence

C++11 可变参数模板 + 继承

c++ - 在 C++ 中不重载 operator== 的结构成员相等性

c++ - 如何在 C++ 中绘制直方图和点图并保存到图片

c++ - 函数模板参数

c# - 您使用哪些(第三方)Visual Studio 2005/2008 调试可视化工具?

c++ - 引用复制是编译器优化吗,我可以避免吗?

c++ - 在 Windows 上编译 mongo db 客户端示例时出现链接器错误

c++ - 如何设置模板参数,灵活选择不同种类的函数