带有模板的 C++ 共享库:未定义符号错误

C++ Shared Library with Templates: Undefined symbols error(带有模板的 C++ 共享库:未定义符号错误)
本文介绍了带有模板的 C++ 共享库:未定义符号错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在尝试使用模板类链接到共享库,但它给了我未定义符号"错误.我已将问题浓缩为大约 20 行代码.

I'm trying to link to a shared library with a template class, but it is giving me "undefined symbols" errors. I've condensed the problem to about 20 lines of code.

shared.h

template <class Type> class myclass {
  Type x;
public:
  myclass() { x=0; }
  void setx(Type y);
  Type  getx();
};

shared.cpp

#include "shared.h"
template <class Type> void myclass<Type>::setx(Type y) { x = y; }
template <class Type> Type myclass<Type>::getx() { return x; }

main.cpp

#include <iostream>
#include "shared.h"
using namespace std;

int main(int argc, char *argv[]) {
   myclass<int> m;
   cout << m.getx() << endl;
   m.setx(10);
   cout << m.getx() << endl;
   return 0;
}

这是我编译库的方式:

g++ -fPIC -c shared.cpp -o shared.o
g++ -dynamiclib -Wl,-dylib_install_name -Wl,libshared.dylib -o libshared.dylib shared.o

和主程序:

g++ -c main.cpp
g++ -o main  main.o -L. -lshared

只得到以下错误:

Undefined symbols:
"myclass<int>::getx()", referenced from:
  _main in main.o
  _main in main.o
"myclass<int>::setx(int)", referenced from:
  _main in main.o

如果我删除 shared.h/cpp 中的模板"内容,并将它们替换为int",则一切正常.此外,如果我只是将模板类代码直接复制并粘贴到 main.cpp 中,并且不链接到共享库,则一切正常.

If I remove the 'template' stuff in shared.h/cpp, and replace them with just 'int', everything works fine. Also, if I just copy&paste the template class code right into main.cpp, and don't link to the shared library, everything works as well.

如何让这样的模板类通过共享库工作?

How can I get a template class like this to work through a shared library?

我使用的是 MacOS 10.5 和 GCC 4.0.1.

I'm using MacOS 10.5 with GCC 4.0.1.

推荐答案

除了其他答案之外,您还可以显式实例化模板类.这仅在您事先知道模板参数可能采用的类型时才有用.您使用库中的所有这些类型实例化模板.

In addition to the other answers, you can explicitly instantiate template classes. This is only useful if you know beforehand what types the template parameters may assume. You instantiate the template with all these types in the library.

为了编译您的示例,只需将以下内容添加到 shared.cpp 的末尾:

For your example to compile, just add the following to the end of shared.cpp:

// Instantiate myclass for the supported template type parameters
template class myclass<int>;
template class myclass<long>;

这会使用 Type=int 实例化模板并将实例化的代码放在共享库中.根据需要为所有类型添加尽可能多的显式实例.

This instantiates the template with Type=int and places the instantiated code in the shared library. Add as many explicit instantiations as you need, for all the types you need.

同样,如果您希望能够使用任意类型参数实例化模板,那么您必须将定义添加到头文件中,以便编译器知道模板的源代码在其他编译单元中实例化时.

Again, if you want to be able to instantiate the template with any arbitrary Type parameter, then you must add the definitions to the header file, so that the compiler knows the source code of the template when instantiating it in other compilation units.

这篇关于带有模板的 C++ 共享库:未定义符号错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

How do compilers treat variable length arrays(编译器如何处理变长数组)
Deduce template argument from std::function call signature(从 std::function 调用签名推导出模板参数)
check if member exists using enable_if(使用 enable_if 检查成员是否存在)
Standard Library Containers with additional optional template parameters?(具有附加可选模板参数的标准库容器?)
Uses of a C++ Arithmetic Promotion Header(C++ 算术提升标头的使用)
Parameter pack must be at the end of the parameter list... When and why?(参数包必须位于参数列表的末尾...何时以及为什么?)