c++visual-studio-codeglfwlibigl

Libigl Tutorial Example 102 WINDOWS Visual Studio Code with Build Tools (integrated CMake and compiler cl.exe)


I am using Visual Studio 2022 and its build tools in Windows. In particular, I use Visual Studio with x64 Native Tools Command Prompt for VS 2022 to open VS and use its integrated CMake and compiler cl.exe. I downloaded libigl from the github repository and used the CMakeLists.txt to have all the necessary libraries.

I had no problem in building the basic examples. However, the problem comes when viewing the resultant mesh with viewer. We have to keep in mind that there are some dependencies of the igl::opengl::glfw::Viewer (OpenGL, glad and GLFW). For that, I downloaded GLFW aside.

Now, the difference for this tutorial is that in order to visualize the mesh I have to do the linking with GLFW and I am not sure how to do it correctly.

The tutorial is the following:

#include <igl/readOFF.h>
#include <igl/opengl/glfw/Viewer.h>

Eigen::MatrixXd V;
Eigen::MatrixXi F;

int main(int argc, char *argv[])
{
  // Load a mesh in OFF format
  igl::readOFF(TUTORIAL_SHARED_PATH "/bunny.off", V, F);

  // Plot the mesh
  igl::opengl::glfw::Viewer viewer;
  viewer.data().set_mesh(V, F);
  viewer.launch();
}

In this case, I think I have to use GLFW as a DLL, right? Or as a static library? I used it as a DLL so I used specifically the glfw3dll.lib file. Doing some research I realised that I also have to define the GLFW_DLL macro before any inclusion of the GLFW header but I am not sure where I have to put it, in the file that has #include <GLFW/glfw3.h> or in my main.cpp? I don´t know either if I did the linking well.

In short, I wrote the following:

cl /EHsc tutorial/102_DrawMesh/main.cpp build/_deps/glad-src/src/glad.c

(I also included the necessary libraries)

/I "E:\GLFW\include\GLFW" glfw3dll.lib /link /LIBPATH:"E:\GLFW\lib-vc2022"

I run it and I get:

Compilador de optimización de C/C++ de Microsoft (R) versión 19.34.31935 para x64 (C) Microsoft Corporation. Todos los derechos reservados.

main.cpp

Generating code...

Compiling...

glad.c

Generating code...

Microsoft (R) Incremental Linker Version 14.34.31935.0

Copyright (C) Microsoft Corporation. All rights reserved.

/out:main.exe

/LIBPATH:E:\GLFW\lib-vc2022

main.obj

glad.obj

glfw3dll.lib

However, when I try to open the main.obj, the viewer opens but it is not able to plot it, it closes all of a sudden.


Solution

  • After some time working on how to correctly build libigl in my computer, I finally have a solution to my question. I hope this information is also helpful for users who are used to working with Visual Studio Code (not Visual Studio!) in the Windows system. So here I go.

    First of all, I insist on the difference between VSC (Visual Studio Code) and Visual Studio. While VSC is a code editor, VS is a full-featured Integrated Development Environment (IDE). VS can easily manipulate solution files, but for Visual Studio Code this is different. Hence, by default, there is no solution explorer in Visual Studio Code. However, we will be able to build solution files in VSC without any problem.

    To have VSC well set for the project, we need to install Microsoft Visual Studio Build Tools. Although these tools are for Visual Studio, they also work for Visual Studio Code. In particular, these tools will allow us to use the default compiler of Visual Studio Code: cl. Notice that if we want to use these tools, the only way VSC can recognise them is by opening VSC from the Developer Command Prompt, which has this special function. Thus, the compiler cl will not be recognised in the Windows terminal or in the integrated terminal of VSC.

    Once we have downloaded libigl from the github repository, we want to use CMake to configure our project. For this, we will use the CMakeLists.txt in the root folder. Also, if you want to start with the libigl-example-project, for ease of use, place libigl and libigl-example-project in the same folder and then start configuring the last one.

    Let´s continue with libigl...

    However, be careful! Before the CMake process, there is something you have to modify in one of the .cpp files of the project. Apparently, in the file Viewer.cpp line 187, the identifier GLVersion is undefined! If you do nothing about it, you will have 2 errors when building the solution file libigl.sln after. In order to correct these two errors, you can just simply comment out the line.

    Now, for CMake, there are two options:

    1. Use CMake extensions of VSC: CMake and CMake Tools. When you open the folder, you will be asked to select a kit. You simply choose the native compiler and the project will be configured automatically.

    2. Install CMake for Windows: https://github.com/libigl/libigl/issues/1052#issuecomment-449656124 Here everything is well-explained (thank you!)

    You will end up with a full build folder. We are interested in the file libigl.sln, which is a solution file. There are two ways to build this file:

    1. Use Visual Studio, which has the option of opening solutions. The solution will be opened and you will see the structure of the project. You then right-click on the solution and build it.

    2. In the integrated terminal of VSC (more comfortable) go to the build folder (the solution is located there) and write MSBuild.exe along with the solution file: MSBuild.exe ./libigl.sln. The building process will start.

    In both cases, you will have a bin folder located in build and inside the Debug folder you will see all the executables for the tutorials! In order to run them, it suffices to do the usual: go to this folder and write ./nameoftutorial.exe.

    I would also like to refer to these two videos of youtube which have helped me during this task: https://youtu.be/HRGZDnNMc5U?list=PLalVdRk2RC6o5GHu618ARWh0VO0bFlif4 https://youtu.be/gGxi500Q5uE

    If there is something to be added or commented, please do not hesitate. Now, enjoy!