【发布时间】:2017-12-11 03:51:24
【问题描述】:
我在一个大型项目中跟踪了 3 天的错误,终于得到了一个最小的可重现示例。我想分享这个问题,并就 Visual Studio 的奇怪行为提出一些问题。
当您导出从实例化模板类继承的类时,例如
class __declspec(dllexport) classA : public Template<double>{}
MSVC 还将在 DLL 中导出实例化的模板类 Template<double>。
如果消费者在他的代码中包含"template.h"然后实例化一个Template<double>,同时qnd,链接到上面的DLL,他会得到Template<double>的两个定义,导致LNK2005 和 LNK1169 错误。这个问题在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 调试模式下编译代码,它给出了多重定义的错误
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 ==========
如果我做了 ONE OF 以下更改,则不会出现错误:
- 更改为发布模式
- 删除
AA_API(2)并将链接模式更改为 STATIC(1) - 评论
(3)和取消评论(4)和(5) - 评论
(6)和取消评论(7)和(8) - 通过 gcc 在 Linux 中编译
- 在 VS 中更改项目参数:将
/FORCE:MULTIPLE添加到项目 Main 的链接器
问题:
为什么更改 1、2、3、4 和 5 有效?我觉得 3 和 4 可以工作真是太奇怪了。
如何在 Visual Studio 中正确解决这个问题?
【问题讨论】:
-
这个问题有没有报告给微软?看起来是一个非常可怕的问题,没有好的解决方案。
标签: c++ visual-studio templates dll lnk2005