链接错误:无法弄清楚如何将数组作为函数参数传递/接收



我正在制作一个小型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");

最新更新