DLL仅导出某些模板化函数

时间:2022-04-02 23:29:40

Hey all, so this seems to be a rather strange issue to me. I have a very simple templatized container class which is part of a DLL. The entire class is defined in a header file, to allow automatic template generation. Now another part of the DLL actually requests a certain template type to be generated, so the code should exist within the DLL. However, when using the object from another executable, the constructor/destructor, plus multiple other functions work, however 2 functions are not found by the linker. Following are is the code for these two functions, as well as for a working function.

嘿所有,所以这对我来说似乎是一个相当奇怪的问题。我有一个非常简单的模板化容器类,它是DLL的一部分。整个类在头文件中定义,以允许自动生成模板。现在DLL的另一部分实际上请求生成某个模板类型,因此代码应该存在于DLL中。但是,当使用来自另一个可执行文件的对象时,构造函数/析构函数以及多个其他函数都可以工作,但链接器找不到2个函数。以下是这两个函数的代码,以及一个工作函数。

const T** getData() const
{
    return m_data;
}

int getNumRows() const
{
    return m_nRows;
}

int getNumCols() const
{
    return m_nCols;
}

So the getNumRows() and getNumCols() functions are not found by the linker, however the getData() function is. Is this a common problem, do the functions need to have a templatized parameter in order to be generated?

因此链接器找不到getNumRows()和getNumCols()函数,但getData()函数是。这是一个常见问题,函数是否需要有一个模板化参数才能生成?

@1 800 INFORMATION

@ 1 800信息

I have exported this from the DLL via a standard macro:

我已经通过标准宏从DLL导出了这个:

#ifdef ACORE_EXPORTS
#define ACORE_API __declspec(dllexport)
#else
#define ACORE_API __declspec(dllimport)
#endif

And at the class definition:

并在课堂定义:

template < class T >
class ACORE_API matrix

2 个解决方案

#1


The compiler will only generate member functions that are actually called.

编译器只生成实际调用的成员函数。

For instance:

template <class T>
class MyClass
{public:
    int function1()
    {
        return 0;
    }
    int function2()
    {
        T t;
        t->DoSomething();
        return 0;
    }
};

and then later

然后是

MyClass<int> m;
m.function1();

compiles, because MyClass::function2() was never compiled.

编译,因为从未编译过MyClass :: function2()。

You can force the instantiation of the entire class by doing this:

您可以通过执行以下操作强制实例化整个类:

template class MyClass<int>;

In that case, every method in the class will be instantiated. In this example, you get a compiler error.

在这种情况下,将实例化类中的每个方法。在此示例中,您将收到编译器错误。

#2


Are you actually exporting the functions from the library? You would mention the names in the .def file, or use the dllexport and dllimport directives in order to accomplish this.

你实际上是从库中导出函数吗?您可以在.def文件中提及名称,或使用dllexport和dllimport指令来完成此操作。

#1


The compiler will only generate member functions that are actually called.

编译器只生成实际调用的成员函数。

For instance:

template <class T>
class MyClass
{public:
    int function1()
    {
        return 0;
    }
    int function2()
    {
        T t;
        t->DoSomething();
        return 0;
    }
};

and then later

然后是

MyClass<int> m;
m.function1();

compiles, because MyClass::function2() was never compiled.

编译,因为从未编译过MyClass :: function2()。

You can force the instantiation of the entire class by doing this:

您可以通过执行以下操作强制实例化整个类:

template class MyClass<int>;

In that case, every method in the class will be instantiated. In this example, you get a compiler error.

在这种情况下,将实例化类中的每个方法。在此示例中,您将收到编译器错误。

#2


Are you actually exporting the functions from the library? You would mention the names in the .def file, or use the dllexport and dllimport directives in order to accomplish this.

你实际上是从库中导出函数吗?您可以在.def文件中提及名称,或使用dllexport和dllimport指令来完成此操作。