AntoineG
AntoineG

Reputation: 1247

Linking error: can't figure how to pass/receive arrays as function arguments

I'm working on a small OpenGL demo that reads orientation of a physical sensor (in quaternion format) and consequently rotates an object on screen. Now I'm trying to transform the quaternion-form rotation into a rotation 4x4 matrix. I found a small algorithm online that does that. However, I have a hard time processing my quaternion into matrices because I don't understand how to pass arrays around C++ functions.

So I'm trying to do this:

#include "MatrixMath.h"

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

The readRotation function is defined as:

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

}

When I try to compile, the linker gives me this error:

./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

The fromQuaternionToMatrix function is declared in a file named "MatrixMath.h":

#ifndef MATRIXMATH_H_
#define MATRIXMATH_H_

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

#endif

And implemented in a file named "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;
}

The linker tells me undefined reference to 'fromQuaternionToMatrix(float*, float*)' but the signature of my function is exactly fromQuaternionToMatrix( float *aQuaternion, float *a4x4Flat16Matrix ), so I don't understand what it is whining about. The compiler gives me no errors.

Upvotes: 0

Views: 114

Answers (1)

Aesthete
Aesthete

Reputation: 18848

It not the signature of the function that is the problem, it's the inability to link to any implementation.

If you were passing the wrong arguments to the function you would get a compile time error. What you are getting here is a link time error. The linker cannot find any implementation for the function declared as fromQuaternionToMatrix(float*, float*)

In Visual Studio (I can't comment on other ides), you need to ensure that your .cpp file is added to your project. Otherwise if you are using a lib you will need to reference the library either in the project settings, or in the code.

#pragma comment(lib, "LibFileName");

Upvotes: 2

Related Questions