【发布时间】:2015-10-27 14:47:30
【问题描述】:
我在链接时遇到了一个奇怪的错误。
标题:
global.h
#include <cmath>
#include <iostream>
#include <vector>
#include <blitz/tinyvec2.h>
typedef blitz::TinyVector<double,3> vettore;
#include "animal.h"
animal.h
#ifndef __ANIMAL_H
#define __ANIMAL_H
//! basic data structure for an animal -------------------------
struct Animal
{
int age;
public:
Animal(int _age) : age(_age) {}
};
//! contains info about a pair of animals ----------------------
struct AnimalPairs
{
vettore distance;
AnimalPairs( const vettore& _distance ) : distance(_distance) {}
};
typedef std::vector<AnimalPairs> pair_list;
//! data structure for a group of animals ----------------------
class AnimalVector
{
private:
std::vector<Animal> animals;
pair_list pairs;
public:
AnimalVector( const AnimalVector &other );
};
#endif
这里是*cpp 文件
main.cpp
#include "global.h"
int main ()
{
std::cout<< "Hello" << std::endl;
}
animal.cpp
#include "global.h"
AnimalVector::AnimalVector( const AnimalVector &other )
{
pairs = other.pairs;
}
编译我使用
g++ main.cpp animal.cpp -I/usr/include/boost -I/fs01/ma01/homes/matc/local/blitz/include
这是我得到的错误:
/tmp/ccGKHwoj.o: In function `AnimalPairs::AnimalPairs(AnimalPairs const&)':
animal.cpp:(.text._ZN11AnimalPairsC2ERKS_[_ZN11AnimalPairsC5ERKS_]+0x1f):
undefined reference to \`blitz::TinyVector<double,
3>::TinyVector(blitz::TinyVector<double, 3> const&)'
collect2: error: ld returned 1 exit status
由于某些原因,如果我将AnimalVector 构造函数设置为inline,则代码将起作用。
谁能解释一下为什么?
编辑:
这是blitz/tinyvec2.h的链接
https://github.com/syntheticpp/blitz/blob/master/blitz/tinyvec2.h
【问题讨论】:
-
你为什么使用
structs 来代替Animal和AnimalPairs?关于链接器问题(我不熟悉使用的框架),是否需要将库(TinyVector实现所在)传递给 g++? -
我希望当构造函数是
inline时,它永远不需要被实例化,所以无论错误是什么都会被跳过。但是没有看到blitz/tinyvec2.h,我猜不出这个错误。 -
@CristiFati 因为我希望可以轻松了解动物的年龄,以及两只动物之间的距离。最初的项目不是关于动物,而是关于物理模拟中的粒子,我想在其中轻松访问位置和速度等数据。
-
@JSF 感谢您指出这一点。我正在将 blitz/tinyvec2.h 的链接添加到问题中。
-
我不记得在哪里记录了:g++ 有跳过函数实例化的规则(对于一些其他构建选择可能已经内联,但现在没有内联),理论认为一个格式良好程序总是会在其他模块中实例化这些函数。您的程序显然违反了这些规则背后的假设(复制构造函数作为
AnimalVector的唯一定义的构造函数)。所以 g++ 正在跳过实例化 TinyVector 的复制构造函数。但我不知道它跳过的确切规则。
标签: c++ boost linker inline blitz++