我在win7中使用VS2008,在CentOS 18中使用g ++ 4.7。當我使用動態共享庫時,問題只出現在Windows上。當我將它轉換爲靜態庫時,程序鏈接正常。Visual Studio dll導出類和函數模板實例的問題
我知道共享庫模板函數/類應該在頭文件中定義,或者應該通過編譯單元提供模板類型(參數)的模板實例化。我選擇了後面的選項。我以前做過,我通過
Why can templates only be implemented in the header file?
C++ Shared Library with Templates: Undefined symbols error
去,但我不能,爲什麼在Windows儘快弄清楚,我轉換對DLL它未能解決的符號庫: 錯誤LNK2019:無法解析的外部符號「void __cdecl HelpingRegistration(double)」(?? $ HelpingRegistration @ N @@ YAXN @ Z)在函數_main中引用
在Windows中,它在靜態庫中工作正常。 在Linux中,動態庫和共享庫都可以工作。
//Static library
//Library header
#ifndef _TEMPLATED_STATIC_LIB_
#define _TEMPLATED_STATIC_LIB_
#include <iostream>
#include <string>
#include "Export.h"
template<typename T>
class EXPORT TemplatedStaticLib
{
public:
TemplatedStaticLib(){};
~TemplatedStaticLib(){};
void print(T t);
};
template<typename T>
EXPORT void HelpingRegistration(T);
#endif
//庫的.cpp
#include "TemplatedStaticLib.h"
#include <typeinfo>
template<typename T>
void TemplatedStaticLib<T>::print(T t)
{
std::cout << "Templated Print: "<< t<< " type:: " << typeid(t).name() << std::endl;
}
//Class Template explicit instantiation
template class TemplatedStaticLib<double>;
template class TemplatedStaticLib<std::string>;
template<typename T>
void HelpingRegistration(T t)
{
std::cout << "Function Templated Print: " << t << " type: " << typeid(t).name() << std::endl;
//return t;
}
//function template explicit instantiation
template void HelpingRegistration<>(double);
template void HelpingRegistration<>(std::string);
//視窗符號出口
//.h
#ifndef STATIC_LIB_EXPORT
#define STATIC_LIB_EXPORT
#if !defined WIN32
#define EXPORT
#elif defined LIB_EXPORTS
#define EXPORT __declspec(dllexport)
#else
#define EXPORT __declspec(dllimport)
#endif
//STATIC_LIB_EXPORT
#endif
//庫用戶的.cpp
#include <TemplatedStaticLib/TemplatedStaticLib.h>
#include<string>
int main(int argc, char* argv[])
{
double aDouble = 3.9;
TemplatedStaticLib<double> double_test;
double_test.print(aDouble);
std::string aString = "James";
TemplatedStaticLib<std::string> string_test;
string_test.print(aString);
HelpingRegistration(aDouble);
HelpingRegistration(aString);
return 0;
}
有這個文學成千上萬的重複。您不能像平常一樣將模板化的類分割爲頭文件和源文件,它們都必須位於頭文件中。 –
[爲什麼模板只能在頭文件中實現?](http://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file) – juanchopanza
@Joachim Pileborg它肯定可以按照問題http://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file中提供的答案所示完成,我也是這樣做的,只有在Windows DLL它導致問題請認真閱讀完整的問題第一 – jazaman