带模板的C ++共享库:未定义的符号错误(C++ Shared Library with Templates: Undefined symbols error)

编程入门 行业动态 更新时间:2024-10-28 02:31:47
带模板的C ++共享库:未定义的符号错误(C++ Shared Library with Templates: Undefined symbols error)

我试图用模板类链接到共享库,但它给了我“未定义的符号”错误。 我将问题简化为约20行代码。

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 ,并且不链接到共享库,则一切正常。

我如何获得这样的模板类来处理共享库?

我正在使用MacOS 10.5和GCC 4.0.1。

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; }

This is how I compile the library:

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

And the main program:

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

Only to get the following errors:

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

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?

I'm using MacOS 10.5 with GCC 4.0.1.

最满意答案

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

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

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

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

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

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.

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>;

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.

更多推荐

本文发布于:2023-08-07 08:39:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1463662.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:符号   模板   错误   未定义   Shared

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!