google-deepmind / mujoco

Multi-Joint dynamics with Contact. A general purpose physics simulator.
https://mujoco.org
Apache License 2.0
7.83k stars 780 forks source link

I'm trying to inlcude mujoco into my own CMakeLists.txt for my project and I'm getting lots of `undefined reference` errors #402

Closed MotorCityCobra closed 2 years ago

MotorCityCobra commented 2 years ago

I built and installed MuJoCo from source on my Linux machine. I am able to include mujoco in my C++ and it looks like not every part of mujoco I try to use throws an error but many do.

Here is my CMakeLists.txt


cmake_minimum_required(VERSION 3.5.1)
project(mujoco_gym)

set(CMAKE_CXX_STANDARD 17)
# It prevents the decay to C++98 when the compiler does not support C++14
set(CMAKE_CXX_STANDARD_REQUIRED ON)
# It disables the use of compiler-specific extensions
# e.g. -std=c++14 rather than -std=gnu++14
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall")

set(Torch_DIR libtorch/share/cmake/Torch)
find_package(Torch PATHS ${Torch_DIR} NO_DEFAULT REQUIRED)
if (Torch_FOUND)
    message(STATUS "Torch library found!")
    message(STATUS "    include path: ${TORCH_INCLUDE_DIRS}" \n)
else ()
    message(FATAL_ERROR "Could not locate Torch" \n)
endif()

include_directories(mujoco/build/_deps/glfw-src/include)
include_directories(mujoco/build/lib)
include_directories(mujoco/build)
include_directories(mujoco/sample)
include_directories(mujoco/include)

find_package(mujoco REQUIRED)
link_libraries(mujoco::mujoco)

find_package(GSL REQUIRED)
link_libraries(GSL::gsl)

file(GLOB SOURCE_FILES small_simulator.cpp)

#CMAKE_PROJECT_NAME is from 'project(mujoco_gym)' on the second line of this script
add_executable(${CMAKE_PROJECT_NAME} ${SOURCE_FILES})

target_link_libraries (
        ${CMAKE_PROJECT_NAME}
        ${TORCH_LIBRARIES}
        ${MUJOCO_LIBRARIES}
        ${GSL_LIBRARIES}
)

$cmake .. completes without errors but cmake --build . causes these errors

$ cmake --build . 
Consolidate compiler generated dependencies of target mujoco_gym
[ 50%] Linking CXX executable mujoco_gym
/usr/bin/ld: CMakeFiles/mujoco_gym.dir/small_simulator.cpp.o: in function `mouse_button(GLFWwindow*, int, int, int)':
small_simulator.cpp:(.text+0x87): undefined reference to `glfwGetMouseButton'
/usr/bin/ld: small_simulator.cpp:(.text+0xa4): undefined reference to `glfwGetMouseButton'
/usr/bin/ld: small_simulator.cpp:(.text+0xc1): undefined reference to `glfwGetMouseButton'
/usr/bin/ld: small_simulator.cpp:(.text+0xea): undefined reference to `glfwGetCursorPos'
/usr/bin/ld: CMakeFiles/mujoco_gym.dir/small_simulator.cpp.o: in function `mouse_move(GLFWwindow*, double, double)':
small_simulator.cpp:(.text+0x1a1): undefined reference to `glfwGetWindowSize'
/usr/bin/ld: small_simulator.cpp:(.text+0x1b2): undefined reference to `glfwGetKey'
/usr/bin/ld: small_simulator.cpp:(.text+0x1c8): undefined reference to `glfwGetKey'
/usr/bin/ld: CMakeFiles/mujoco_gym.dir/small_simulator.cpp.o: in function `main':
small_simulator.cpp:(.text+0x79c): undefined reference to `glfwInit'
/usr/bin/ld: small_simulator.cpp:(.text+0x7d8): undefined reference to `glfwCreateWindow'
/usr/bin/ld: small_simulator.cpp:(.text+0x7ee): undefined reference to `glfwMakeContextCurrent'
/usr/bin/ld: small_simulator.cpp:(.text+0x7f8): undefined reference to `glfwSwapInterval'
/usr/bin/ld: small_simulator.cpp:(.text+0x889): undefined reference to `glfwSetKeyCallback'
/usr/bin/ld: small_simulator.cpp:(.text+0x8a2): undefined reference to `glfwSetCursorPosCallback'
/usr/bin/ld: small_simulator.cpp:(.text+0x8bb): undefined reference to `glfwSetMouseButtonCallback'
/usr/bin/ld: small_simulator.cpp:(.text+0x8d4): undefined reference to `glfwSetScrollCallback'
/usr/bin/ld: small_simulator.cpp:(.text+0xa44): undefined reference to `glfwGetFramebufferSize'
/usr/bin/ld: small_simulator.cpp:(.text+0xabc): undefined reference to `glfwSwapBuffers'
/usr/bin/ld: small_simulator.cpp:(.text+0xac1): undefined reference to `glfwPollEvents'
/usr/bin/ld: small_simulator.cpp:(.text+0x104b): undefined reference to `glfwWindowShouldClose'
collect2: error: ld returned 1 exit status
gmake[2]: *** [CMakeFiles/mujoco_gym.dir/build.make:106: mujoco_gym] Error 1
gmake[1]: *** [CMakeFiles/Makefile2:83: CMakeFiles/mujoco_gym.dir/all] Error 2
gmake: *** [Makefile:91: all] Error 2
$ 

My C++ file is one from the sample directory in the repo. It's large and could be any sample .cc file in the repo so I am not including.
I know MuJoCo installed on my machine in various paths under /usr/local and I have included every path I can think of from the mujoco repo I have downloaded so it's not clear to me why I can't include the library and build with cmake.

saran-t commented 2 years ago

CMake doesn't add dependencies transitively, so you need to add_dependencies on GLFW as well as mujoco::mujoco.

~In other news, I think there's something a bit broken with our CMake setup since I'm setting missing MuJoCo symbols. Still investigating.~ EDIT: My bad, I forgot target_link_libraries.

MotorCityCobra commented 2 years ago

@saran-t Thank you for the reply.
This works for you?
Is there a problem with my CMakeLists.txt? I thought I already have what you recommend. For GLFW I did include_directories(mujoco/build/_deps/glfw-src/include/GLFW). I don't know how to add it with add dependencies.

I've tried a few variations and I get the same error as in the original post. I've been trying to do this for a few days. I'm having to include_directories for quite a few things to avoid uitools.h: No such file or directory and array_safety.h: No such file or directory. After that I still get all the undefined errors. I did the install, last part of the cmake build. Should I try to add these to the CMakeLists.txt in the base mujoco directory of the repo before building and installing?

I'd prefer to have a clear answer either way, that I can bring the working library into a CMakeLists.txt or I can't. Maybe it's something coming in the future?

saran-t commented 2 years ago

array_safety.h and uitools.h aren't currently officially part of MuJoCo itself, rather they're shipped as part of our sample applications. This may change in the near future, but in the meantime if your codebase needs these two headers you should copy and incorporate them into your own project.

Also, you shouldn't need include_directories, only target_dependencies should suffice. And you still need to add a dependency on GLFW as I mentioned above.

Are you actually using the GUI? If not then you shouldn't need uitools.h...

MotorCityCobra commented 2 years ago

only target_dependencies should suffice. And you still need to add a dependency on GLFW as I mentioned above.

I assume you mean add_dependencies for glfw.

I ended up installing glfw by cloning the glfw repo and building from source. I included it in my CMakeLists.txt with the code below.

find_package(glfw3 REQUIRED)

find_package(GSL REQUIRED)
link_libraries(GSL::gsl)

file(GLOB SOURCE_FILES mujoco_gym.cpp)

#CMAKE_PROJECT_NAME is from 'project(mujoco_gym)' on the second line of this script
add_executable(${CMAKE_PROJECT_NAME} ${SOURCE_FILES})

target_link_libraries (
        ${CMAKE_PROJECT_NAME}
        ${TORCH_LIBRARIES}
        ${GSL_LIBRARIES}
        ${mujoco_LIBRARIES}
        glfw
)

The glfw undefinded errors are gone.

I think I only have ui errors now


/usr/bin/ld: CMakeFiles/mujoco_gym.dir/mujoco_gym.cpp.o: in function `loadmodel()':
mujoco_gym.cpp:(.text+0xa9ac): undefined reference to `uiModify'
/usr/bin/ld: mujoco_gym.cpp:(.text+0xa9d3): undefined reference to `uiModify'
/usr/bin/ld: CMakeFiles/mujoco_gym.dir/mujoco_gym.cpp.o: in function `uiEvent(mjuiState_*)':
mujoco_gym.cpp:(.text+0xb1f4): undefined reference to `uiModify'
/usr/bin/ld: mujoco_gym.cpp:(.text+0xb218): undefined reference to `uiModify'
/usr/bin/ld: mujoco_gym.cpp:(.text+0xba01): undefined reference to `uiModify'
/usr/bin/ld: CMakeFiles/mujoco_gym.dir/mujoco_gym.cpp.o:mujoco_gym.cpp:(.text+0xba64): more undefined references to `uiModify' follow
/usr/bin/ld: CMakeFiles/mujoco_gym.dir/mujoco_gym.cpp.o: in function `init()':
mujoco_gym.cpp:(.text+0xd6e9): undefined reference to `uiFontScale'
/usr/bin/ld: mujoco_gym.cpp:(.text+0xd761): undefined reference to `uiSetCallback'
/usr/bin/ld: mujoco_gym.cpp:(.text+0xd9e8): undefined reference to `uiModify'
/usr/bin/ld: mujoco_gym.cpp:(.text+0xda0f): undefined reference to `uiModify'
/usr/bin/ld: CMakeFiles/mujoco_gym.dir/mujoco_gym.cpp.o: in function `main':
mujoco_gym.cpp:(.text+0xdb47): undefined reference to `uiClearCallback'
collect2: error: ld returned 1 exit status
gmake[2]: *** [CMakeFiles/mujoco_gym.dir/build.make:109: mujoco_gym] Error 1
gmake[1]: *** [CMakeFiles/Makefile2:83: CMakeFiles/mujoco_gym.dir/all] Error 2
gmake: *** [Makefile:91: all] Error 2
saran-t commented 2 years ago

When you place uitools.h and uitools.c into your own project, you either need to add it in the sources list of your binary/library target, or put them into a dedicated library and target_link_libraries to that.

guofeng509 commented 3 months ago

where can I find the file of uitools.h and uitools.c

yuvaltassa commented 3 months ago

These makefiles are old and unsupported. You can fix them and send a PR or we can just delete them. @saran-t has been wanting to delete them for a while...

guofeng509 commented 2 months ago

ok, Thank you very much