Search code examples
c++cmakeshared-librariessimulatorrobotics

How to fix Mujoco CmakeLists build error "/usr/bin/ld: cannot find -lglfw"?


I'm trying to simulate physics of a robot with Mujoco in C++. Because the project is a part of a bigger workspace, I need to use cmake to build the executable. However, I cannot seem to properly link all the dependant libraries, so I cannot get rid of the error:

~: /usr/bin/ld: cannot find -lglfw

I did a bit of research on the web on how to properly set up Mujoco in CmakeLists, and found some examples here, here, and here.

I replicated the CmakeLists files from the examples above, but the error still persisted. Here are the relevant snippets from my files. I defined an environment variable MUJOCO_PATH to point to the Mujoco folder on my machine. Concretely $HOME/.mujoco/mujoco200.

CmakeLists.txt

######################################################
# define the include directory of all ${CATKIN_PKGS} #
######################################################
include_directories(
    ${PROJECT_SOURCE_DIR}/include
    ${catkin_INCLUDE_DIRS}
    ${Eigen_INCLUDE_DIRS}
    $ENV{MUJOCO_PATH}/include
)

########################################################
# manage the creation of the libraries and executables #
########################################################
set(USE_GL 1)

link_directories($ENV{MUJOCO_PATH}/bin)

#Finding main mujoco library
if(${USE_GL})
    file(GLOB LIB_MUJOCO $ENV{MUJOCO_PATH}/bin/libmujoco[0-9][0-9][0-9].so)
else()
    file(GLOB LIB_MUJOCO $ENV{MUJOCO_PATH}/bin/libmujoco[0-9][0-9][0-9]nogl.so)
endif()
#Showing mujoco library found
message(STATUS "MuJoCo lib: " ${LIB_MUJOCO})

add_subdirectory(src)

src/CmakeLists.txt

set(BIN_NAME mujoco_finger_test)

add_executable(${BIN_NAME} ${BIN_NAME}.cpp)
target_link_libraries(${BIN_NAME} ${LIB_MUJOCO})

# Standard libraries for GL
target_link_libraries(${BIN_NAME} GL GLU glut )

# Additional libraries from mujoco package
target_link_libraries(${BIN_NAME} libglew.so libglfw.so.3 libglewegl.so libglewosmesa.so)

Does anyone have an idea why this could be the case? Am I missing something from these examples?

Thanks!


Solution

  • You should find the GL/GLW package instead ot this: target_link_libraries(${BIN_NAME} libglew.so libglfw.so.3 libglewegl.so libglewosmesa.so). This doesn't ensure that these libraries are available and can be found, whereas the FIND_PACKAGE(GLEW).

    See Linking GLEW with CMake for more information on the subject.