c++ - 链接错误 : can't figure how to pass/receive arrays as function arguments

标签 c++ linker

我正在开发一个小型 OpenGL 演示,该演示读取物理传感器的方向(四元数格式),然后旋转屏幕上的对象。现在我正在尝试将四元数形式的旋转转换为旋转 4x4 矩阵。我在网上找到了一个可以做到这一点的小算法。但是,我很难将四元数处理成矩阵,因为我不明白如何围绕 C++ 函数传递数组。

所以我正在尝试这样做:

#include "MatrixMath.h"

// somewhere in my render loop
float aRotationMatrix[16];
readRotation(aRotationMatrix);

readRotation 函数定义为:

void readRotation(float *a4x4Flat16Matrix){
   sQuaternionReader.read();
   float aQuaternion[4];

   aQuaternion[0] = sQuaternionReader.getQuarternionX();
   aQuaternion[1] = sQuaternionReader.getQuarternionY();
   aQuaternion[2] = sQuaternionReader.getQuarternionZ();
   aQuaternion[3] = sQuaternionReader.getQuarternionW();

   float *aMatrix = a4x4Flat16Matrix;

   fromQuaternionToMatrix(aQuaternion, aMatrix);  // this is declared faulty by the linker

}

当我尝试编译时,链接器给我这个错误:

./src/main/CupNPlate.cpp:151: error: undefined reference to 'fromQuaternionToMatrix(float*, float*)'
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [CupNPlate] Error 1

fromQuaternionToMatrix 函数在名为“MatrixMath.h”的文件中声明:

#ifndef MATRIXMATH_H_
#define MATRIXMATH_H_

void fromQuaternionToMatrix( float *aQuaternion,
         float *a4x4Flat16Matrix );

#endif

并在名为“MatrixMath.cpp”的文件中实现:

#include "MatrixMath.h"

void fromQuaternionToMatrix( float *aQuaternion,
         float *a4x4Flat16Matrix ){
   // x := 0, y := 1, z := 2, w := 3
   float x2 = aQuaternion[0] * aQuaternion[0];
   float y2 = aQuaternion[1] * aQuaternion[1];
   float z2 = aQuaternion[2] * aQuaternion[2];
   float xy = aQuaternion[0] * aQuaternion[1];
   float xz = aQuaternion[0] * aQuaternion[2];
   float yz = aQuaternion[1] * aQuaternion[2];
   float wx = aQuaternion[3] * aQuaternion[0];
   float wy = aQuaternion[3] * aQuaternion[1];
   float wz = aQuaternion[3] * aQuaternion[2];

   a4x4Flat16Matrix[0] =  1.0f - 2.0f * (y2 + z2);
   a4x4Flat16Matrix[1] =  2.0f * (xy - wz);
   a4x4Flat16Matrix[2] =  2.0f * (xz + wy);
   a4x4Flat16Matrix[3] =  0.0f;

   a4x4Flat16Matrix[4] =  2.0f * (xy + wz);
   a4x4Flat16Matrix[5] =  1.0f - 2.0f *(x2 + z2);
   a4x4Flat16Matrix[6] =  2.0f * (yz - wx);
   a4x4Flat16Matrix[7] =  0.0f;

   a4x4Flat16Matrix[8] =  2.0f * (xz - wy);
   a4x4Flat16Matrix[9] =  2.0f * (yz + wx);
   a4x4Flat16Matrix[10] =  1.0f - 2.0f * (x2 + y2);
   a4x4Flat16Matrix[11] =  0.0f;

   a4x4Flat16Matrix[12] =  0.0f;
   a4x4Flat16Matrix[13] =  0.0f;
   a4x4Flat16Matrix[14] =  0.0f;
   a4x4Flat16Matrix[15] =  1.0f;
}

链接器告诉我 undefined reference to 'fromQuaternionToMatrix(float*, float*)' 但我函数的签名恰好是 fromQuaternionToMatrix( float *aQuaternion, float *a4x4Flat16Matrix ),所以我不明白它在提示什么。编译器没有给我任何错误。

最佳答案

问题不是函数的签名,而是无法链接到任何实现

如果您将错误的参数传递给函数,您将得到一个编译时错误。您在这里得到的是一个链接时间错误。链接器找不到声明为 fromQuaternionToMatrix(float*, float*)

的函数的任何实现

在 Visual Studio 中(我无法评论其他 IDE),您需要确保将您的 .cpp 文件添加到您的项目中。否则,如果您使用的是库,则需要在项目设置或代码中引用该库。

#pragma comment(lib, "LibFileName");

关于c++ - 链接错误 : can't figure how to pass/receive arrays as function arguments,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12050823/

相关文章:

c++字符串 vector - 指针问题

c++ - mxDestroyArray 是否可以正确地释放重新分配的矩阵或大小已更改的矩阵?

linux - 为什么ldconfig能够找到一个库,但是找不到Rust?

c - 如何使用这个链接描述文件程序?

c++ - Visual C++ 2008 链接(特别是嵌入 list )花费的时间太长

c++ - Visual Studio 找不到包含的定义

c++ - llvm::Type 结构的字符串表示

C++:链接库两次,全局构造函数是否运行两次?

c - 链接器是复制函数的内容还是简单地链接到它?

c++ - Qt 验证器正则表达式