cmakeglut

Unable to link GLUT


I have a CMake file that has the following directive inside:

# GLUT
find_package(GLUT REQUIRED)
target_link_libraries(${OutputExecutable} ${GLUT_LIBRARIES})

That seems to be ok as no error is returned. However, later I'm getting the following error:

ld: library not found for -lglut
clang: error: linker command failed with exit code 1 (use -v to see invocation)

which is indicating that the linker can't find the glut library's location.

Glut is installed through brew and is properly configured with pkg-config too.

When I do:

pkg-config --libs glut

I get:

-L/usr/local/Cellar/freeglut/3.4.0/lib -lglut

When I dig inside the Cmake's cache files, I can see that path being pulled in at various instances.

Any ideas?

I am on OSX Big Sur (11.6.5), Cmake 3.22

I suspect that the problem is that on OSX, I already have GLUT as a Framework inside:

/System/Library/Frameworks/GLUT.framework

on top of that, I have installed Freeglut through brew inside:

/usr/local/Cellar/freeglut/3.4.0/lib

So the following:

# GLUT
find_package(GLUT REQUIRED)
target_link_libraries(${OutputExecutable} ${GLUT_LIBRARIES})

was finding the brew installed GLUT.


Solution

  • I have replaced the following:

    # GLUT
    find_package(GLUT REQUIRED)
    target_link_libraries(${OutputExecutable} ${GLUT_LIBRARIES})
    

    with this:

    # GLUT
    target_link_libraries(${OutputExecutable} "-framework GLUT")
    

    This seems to have resolved the problem. So I have to move on. Unfortunately I'm still not sure why the brew installed GLUT was failing. But since this has solved my problem, I have to leave it for now.