【问题标题】:problem w/ linking static function g++问题/链接静态函数c ++
【发布时间】:2009-11-14 00:41:28
【问题描述】:

我正在尝试构建一个小程序,并且我有自己的库 libfoo。我有一个相机类,它从我的 Vector3 类(即 crossProduct)中调用一个静态函数。我的相机类和 Vector3 类编译正常并内置在 libfoo 中。但是,当我这样链接时:

g++ -g -O2 -o test1 main.o -lfoo

我明白了:

libfoo.so: undefined reference to 
foo::Vector3::dotProduct(foo::Vector3 const&, foo::Vector3 const&)

现在 Vector3.h 中的函数是:

static Vector3 crossProduct(const Vector3 &v1, const Vector3 &v2); 并且在 类定义...并且在 Vector3.cpp 中定义。

在我在 Camera.cpp 中调用这个静态函数之前,程序一直很好。为了解决这个问题,我必须将 Vector3.cpp 中的代码从头文件中的内容更改为:

Vector3 Vector3::crossProduct(const Vector3 &v1, const Vector3 &v2) 它编译、链接和运行正常。

Vector3 类是由其他人为 Windows 编译器编写的,但我已将其移至 Linux。这是一个 g++ 的东西吗?还是不好的代码?

TIA。

【问题讨论】:

  • 未定义对“dotProduct”的引用,但函数是“crossProduct”?
  • 您更改了 Vector3.cpp,但您只向我们展示了您的新版本,而不是原始版本。因此我们只能推测。

标签: c++ function static g++ linker


【解决方案1】:

听起来您的 vector3.cpp 文件最初包含以下内容:

Vector3 crossProduct(const Vector3 &v1, const Vector3 &v2)
{
    ...
}

您必须更改为:

Vector3 Vector3::crossProduct(const Vector3 &v1, const Vector3 &v2)
{
    ...
}

前者只是定义了一个名为crossProduct的独立函数,而后者是定义了Vector3的一个成员函数crossProduct。后者是正确的,而我猜测的原始代码是不正确的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-09-20
    • 2012-07-26
    • 1970-01-01
    • 1970-01-01
    • 2019-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多