1

This is a very easy question, but I'm struggling unreasonably hard to find answers online.

DeepMind just made MuJoCo free, so I decided to download it on my Windows computer and test it out. When I install, however, all I get is a folder.

I've created a different folder (not inside the downloaded folder), and copy pasted the hello.xml and hello.c files from the tutorial into this new folder. However, VSCode has underlined #include "mujoco.h" in red inside hello.c with the warning cannot open source file "mujoco.h".

I assume that I need to add some things to my path or somehow make my compiler able to find the header file. How should I go about doing this?

Overall, I want to be able to run hello.c.

(I have looked at many different links, which I can link here to prove I've researched elsewhere if needed, but most resources online seem to either 1. Assume things will just work / that you have experience with C and library importing 2. Are for Macs/Linux or 3. Are for mujoco-python)

Pro Q
  • 4,391
  • 4
  • 43
  • 92
  • 2
    Inside the downloaded folder, is there a file called `mujoco.h`? If there is, you need to modify your project settings. Add an "include path" that points to the folder that contains `mujoco.h`. – user3386109 Oct 18 '21 at 21:43
  • 1
    Most likely once you get it to compile you will also have to add a path to the library for the linker – stark Oct 18 '21 at 21:53

2 Answers2

2

TL;DR

  1. Create empty visual C++ project
  2. Copy the code
  3. Add MuJoCo Header files via VCC++ Directories -> Include Directories (make sure the set the platform to x64)
  4. Add Library Directories of Mujoco installation ("bin" directory) via Linker -> Input -> Additional Library Directories.
  5. Add the library names (glfw3.lib, mujoco200.lib... etc.) via Linker -> General -> Additional Dependencies
  6. Compile
  7. Put the resulting exe into the bin dir of the MuJoCo installation

Create the project

Open Visual Studio File -> New -> Project -> Visual C++ -> Empty Project

enter image description here

Write the Code

stackoverflowMuJoCo -> Source Files -> Add New Item -> C++ File (I called it "main.cpp" but it shouldn't matter)

Copy the code from hello.c into main.cpp

#include "mujoco.h"
#include "stdio.h"

char error[1000];
mjModel* m;
mjData* d;

int main(void)
{
   // activate MuJoCo
   mj_activate("mjkey.txt");

   // load model from file and check for errors
   m = mj_loadXML("../model/hello.xml", NULL, error, 1000);
   if( !m )
   {
      printf("%s\n", error);
      return 1;
   }

   // make data corresponding to model
   d = mj_makeData(m);

   // run simulation for 10 seconds
   while( d->time<10 )
      mj_step(m, d);

   // free model and data, deactivate
   mj_deleteData(d);
   mj_deleteModel(m);
   mj_deactivate();

   return 0;
}

*note i downloaded it before deepmind took over it so I still need to use the mj_activate call but you can just ignore it. Also i change the path to hello.xml because later i am going to copy the executable into the bin dir of the MuJoCo installation(see below)

Add the header files

  1. Right Click on the Project -> Properties
  2. Change Configuration from whatever is selected (most likely Debug/x86) to "All Configurations" and set the Platform to x64*
  3. VCC++ Directories -> Include Directories
  4. Add the include directories of your MuJoCo installation "mujoco200_win64\mujoco200_win64\include" (should be a path like this)

enter image description here

*this makes sure you don't have to repeat the whole process for every configuration.

VSCode has underlined #include "mujoco.h" in red inside hello.c

This should now be gone.

Add the libraries

  1. Right Click on the Project -> Properties
  2. Configuration Properties -> Linker -> Input -> Additional Dependencies
  3. Enter the names of the .lib files located in your "installation" of MuJoCo

enter image description here

enter image description here

  1. Linker -> General -> Additonal Library Directories add the "mujoco200_win64\mujoco200_win64\bin" directory

The compilation should now succeed but the execution will probably fail enter image description here

Execution and Debugging

At this stage the execution will probably fail with the following errors message.

enter image description here

To solve this you can just copy the outputed exe file stackoverflowMuJoCo\x64\Release\stackOverflowMuJoCo.exe into the bin directory of your MuJoCo installation.

This will make sure it can find the required dll's.

If you want to be able to debug: manually copy all the dll files into the "stackoverflowMuJoCo\x64\Debug" directory.

Warning

The code from the hello world example is passive simulation this means you won't see anything on the screen.

If you want to see something replace the code with this: https://github.com/atabakd/MuJoCo-Tutorials/blob/master/src/0_preliminaries/pd.cpp and add the invertedPendulum.xml to the models directory.

Some notes

There is probably a better/easier workflow but right now I just tried to get it to work. Also I used a different version of MuJoCo than you but it should basically work the same way (except for the activation stuff).

KoKlA
  • 898
  • 2
  • 11
  • 15
1

Installing the C version of MuJoCo 2.2.1 on windows and compiling/running code

(A) Installing MuJoCo and loading a model file ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

A1) Navigate to https://github.com/deepmind/mujoco/releases and download the windows installation, mujoco-2.2.1-windows-x86_64.zip Unzip this file and put it in a good location (e.g., Documents)

A2) Navigate to bin folder and double click “simulate”. This will open up a GUI.

A3) To load a model, go to the model folder and drop an xml, say humanoid.xml onto the open window. If everything worked fine, you should see a humanoid in the window

(B) Compiling the C programs provided by Deepmind ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

B1) Assuming you have done A1) above

B2) Download the Visual Studio Installer (select community version) here: https://visualstudio.microsoft.com/downloads/

Once the installer has downloaded, run it. When prompted to choose programs, choose the one that says “Desktop development with C++” (see screenshot below). Please restart your computer after installation.

 B3) Now we will get some additional libraries to compile and create executables for mujoco on windows. Go to https://github.com/glfw/glfw/releases and download the latex version for Windows (usually glfw-3.x.x.bin.WIN64.zip). Unzip the file.

Now we will drag and drop some files from the glfw folder to mujoco i) Copy the entire include/GLFW subdirectory to mujoco/include/GLFW. ii) Copy glfw3dll.lib from the subdirectory corresponding into your compiler (here the compiler is lib-vc2022) to mujoco/lib/glfw3dll.lib. iii) Copy glfw3.dll from the subdirectory corresponding into your compiler (here the compiler is lib-vc2022) to mujoco/bin/glfw3.dll.

B4) We will open the x64 shell to compile and run MuJoCo. Go to: Start (bottom left corner) —> Visual studio —> x64_Native Tools Command Prompt.

From this shell navigate to the sample folder. Then type make or nmake.

B5) Navigate to bin folder. (cd .. followed by cd bin). Then type simulate. Now you can do A3) above.

This video explains these steps: https://youtu.be/u6tNfvLXK-I