16

I am having trouble properly including / using the glm math library (http://glm.g-truc.net/ ) in my c++ project. Since glm is a "header only" library, I thought I could just include it with this line:

#include "glm/glm.hpp"

At first this seemed to be working, as I could create and use matrices and vectors. However when I tried to use the glm::translate(...) function I got this error:

error: ‘translate’ is not a member of ‘glm’

On the GLM website, they recommend including the library with triangle brackets, like so:

#include <glm/glm.hpp>

...but isn't it correct to think that I can include it the other way, given that it is inside the project directory structure?

I have set up the test below to illustrate the problem I am having. The glm folder is sitting next to the testglm.cpp file.

#include <iostream>

#include "glm/glm.hpp"

using namespace std;

int main(void) {

    // works:
    glm::mat4 testMatrix1 = glm::mat4(5.0f) * glm::mat4(2.0f);

    cout << testMatrix1[0][0] << endl; // output: 10

    // doesn't work - (error: ‘translate’ is not a member of ‘glm’):
    glm::mat4 testMatrix2 = glm::translate(glm::mat4(1.0f), glm::vec3(1.0f));

}

I am building this test with this build command from the terminal, on osx:

g++ -o bin/glm_test src/testglm.cpp

I'm not sure if my problem relates to how I'm including the library, how I'm using it, or how I'm building the project. Google does not give me any hits for that error message, so I wonder if I am doing something fundamentally wrong. Advice would be much appreciated. Thanks.

genpfault
  • 51,148
  • 11
  • 85
  • 139
null
  • 1,187
  • 1
  • 8
  • 20

4 Answers4

34

yngum's suggestion lead me to look more closely at the documentation, and I realised that glm::translate is actually part of a module that extends the glm core. I needed to include both the glm core and the matrix_transform extension:

#include "glm/glm.hpp"
#include "glm/gtc/matrix_transform.hpp"

Now the test example works. (I also noticed that I had also made a silly mistake in the test which would have prevented it from compiling. That has been fixed in the original question now for the sake of future readers who may experience the problem I had for the same reason.)

null
  • 1,187
  • 1
  • 8
  • 20
15

Maybe Im a bit late but instead of

#include "glm/glm.hpp"

one could use

#include "glm/ext.hpp"
Tqq
  • 171
  • 1
  • 4
3

make sure you have the correct arguments or use an IDE to simplify your life.
here are the glm::translate signatures I can find

detail::tmat4x4<T> translate (detail::tmat4x4<T> const &m, detail::tvec3<T> const &v);
detail::tmat4x4<T> translate (T x, T y, T z)
detail::tmat4x4<T> translate (detail::tmat4x4<T> const &m, T x, T y, T z)
detail::tmat4x4<T> translate (detail::tvec3<T> const &v)
yngccc
  • 5,594
  • 2
  • 23
  • 33
  • Thanks for taking the time to answer. You were right that I had an extra argument in my call to `translate()` - a typo from when I was trying the `rotate()` function. However, this was not the actual cause of the problem, as `translate` is actually found in an extension I had not included. I decided to fix the typo and answer my own question, as I think this will be of more practical value to future readers googling for that error. +1 for making me look more closely at the docs though. Thanks. – null Oct 12 '13 at 04:07
  • BTW I would certainly use an ide for serious projects but at the moment I'm forcing myself to learn to write Makefiles. I think it's a valuable exercise that will ultimately pay off in avoided pain and suffering. – null Oct 12 '13 at 04:11
0

Write in the terminal:

sudo apt install libglm-dev

This worked for me.

  • 2
    This does not appear to answer the question. The OP seems to have already glm successful installed and is able to include the relevant header files included in that package. – Brian61354270 Feb 18 '20 at 20:06