Linux 使用模板使用g++-std=c++11创建静态库

Linux 使用模板使用g++-std=c++11创建静态库,linux,c++11,static-libraries,Linux,C++11,Static Libraries,当使用c++11创建静态库时,我认为它在链接过程中失败了 < >我可以使用普通C++中的信息创建一个静态库并链接到它,但是如果我尝试遵循C++ 11的特性,则在链接过程中失败。 test.cpp: 包括 包括 包括libtestlib.h 使用名称空间std; int main{ itest n1={1,2,3,4}, {5,6,7,8}, {9,0,1,2}}; cout因为您似乎支持有限数量的类型上的操作,所以对这个问题的经典答案不一定是最好的 通过为每个实现明确导出一个符号,您可以两全其美

当使用c++11创建静态库时,我认为它在链接过程中失败了

< >我可以使用普通C++中的信息创建一个静态库并链接到它,但是如果我尝试遵循C++ 11的特性,则在链接过程中失败。 test.cpp:

包括 包括 包括libtestlib.h 使用名称空间std; int main{ itest n1={1,2,3,4}, {5,6,7,8}, {9,0,1,2}};
cout因为您似乎支持有限数量的类型上的操作,所以对这个问题的经典答案不一定是最好的

通过为每个实现明确导出一个符号,您可以两全其美,但要将实现委托给库中的模板:

libtestlib.h:

#ifndef testlib
#define testlib

#include <vector>
using namespace std;

typedef vector<vector<double>> dtest;
typedef vector<vector<int>>    itest;

void test_print(dtest&);
void test_print(itest&);
libtestlib.cpp:

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

namespace {
  template <typename testtype>
  void test_print_impl(testtype &t)
  {
    int m=t.size();
    int n=t[0].size();
    for(int i=0; i<m; i++) {
      for(int j=0; j<n; j++)
        cout << t[i][j] << " ";
      cout << endl;
    }
    cout << endl;
  }
}

void test_print(dtest& val) {
  test_print_impl(val);
}

void test_print(itest& val) {
  test_print_impl(val);
}

请注意,对于这样一个小函数,可能不值得这么做,只需在头中内联模板代码就可以了。在什么情况下,函数的复杂性及其依赖性的范围保证了这是一个判断调用。

因为您似乎支持有限类型的操作,经典的只是在标题中回答这个问题并不一定是最好的答案

通过为每个实现明确导出一个符号,您可以两全其美,但要将实现委托给库中的模板:

libtestlib.h:

#ifndef testlib
#define testlib

#include <vector>
using namespace std;

typedef vector<vector<double>> dtest;
typedef vector<vector<int>>    itest;

void test_print(dtest&);
void test_print(itest&);
libtestlib.cpp:

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

namespace {
  template <typename testtype>
  void test_print_impl(testtype &t)
  {
    int m=t.size();
    int n=t[0].size();
    for(int i=0; i<m; i++) {
      for(int j=0; j<n; j++)
        cout << t[i][j] << " ";
      cout << endl;
    }
    cout << endl;
  }
}

void test_print(dtest& val) {
  test_print_impl(val);
}

void test_print(itest& val) {
  test_print_impl(val);
}

请注意,对于这样一个小函数,可能不值得这么做,只需在头中内联模板代码就可以了。函数的复杂性及其依赖性的范围在什么程度上保证了这是一个判断调用。

将问题注释中要求的示例作为代码查找d注释中的内容非常难看。组合libtestlib.h和libtestlib.cpp

#ifndef testlib
#define testlib

#include <vector>
#include <iostream>
using namespace std;

typedef vector<vector<double>> dtest;
typedef vector<vector<int>>    itest;

template <typename testtype>
void test_print(testtype &t)
{
  int m=t.size();
  int n=t[0].size();
  for(int i=0; i<m; i++) {
    for(int j=0; j<n; j++)
      cout << t[i][j] << " ";
    cout << endl;
  }
  cout << endl;
}
#endif

将注释中请求的示例放在这里,因为注释中的代码看起来非常难看。组合libtestlib.h和libtestlib.cpp

#ifndef testlib
#define testlib

#include <vector>
#include <iostream>
using namespace std;

typedef vector<vector<double>> dtest;
typedef vector<vector<int>>    itest;

template <typename testtype>
void test_print(testtype &t)
{
  int m=t.size();
  int n=t[0].size();
  for(int i=0; i<m; i++) {
    for(int j=0; j<n; j++)
      cout << t[i][j] << " ";
    cout << endl;
  }
  cout << endl;
}
#endif

你的库基本上是空的,因为这是一个模板函数,它不会有任何实例化。它应该在libtestlib.h头中或包含在其中。一旦你这样做了,你就可以静态编译它。感谢你的回复@estabro。你能举一个例子说明你的意思吗?给出了一个很好的解释。以你的c为例在上面的ode中,将libtestlib.cpp中的内容移动到libtestlib.h中并编译它。g++-std=c++11-static-o test_it test.cpp您的库本质上是空的,因为它是一个模板函数,不会有任何实例化。它应该位于libtestlib.h头中或包含在其中。一旦这样做,您就可以静态编译它。感谢您的回复ponse@estabroo.你能举个例子说明你的意思吗?给出了一个很好的解释。举个例子,把libtestlib.cpp中的内容移到libtestlib.h中,然后编译它。g++-std=c++11-static-o test\u it test.cpptho谢@Frank!你的解决方案对我来说很有效,但我的实际情况当然没有这个例子那么简单/小。:@Frank nice实际上为您提供了构建静态库的选项,而在标题中,它实际上不适合这样做。谢谢@Frank!您的解决方案对我很有效,但我的实际情况当然没有这个示例那么简单/小。@Frank nice实际上为您提供了构建静态库的选项,而在标题中,它为d我真的不适合那样做。