How to use Cmake to compile a project with multiple libraries

Asked

Viewed 430 times

0

I’m trying to develop a game in OpenGL, but the IDE I use makes use of the CMake and I’m not getting to set up the project. I’m totally lost. I searched several tutorials of Cmake on the internet, but they are very confusing and it seems that each one does it in a different way. I need help setting up Cmake as correctly as possible. This is the structure of my project (I followed the idea that article)

├── CMakeLists.txt
├── libs
│   ├── GLEW
│   │   ├── include
│   │   │   └── GL
│   │   │       ├── eglew.h
│   │   │       ├── glew.h
│   │   │       ├── glxew.h
│   │   │       └── wglew.h
│   │   └── lib
│   │       └── libGLEW.a
│   ├── GLFW
│   │   ├── include
│   │   │   └── GLFW
│   │   │       ├── glfw3.h
│   │   │       └── glfw3native.h
│   │   └── lib
│   │       └── libglfw3.a
│   └── Logging
│       ├── include
│       │   └── Logging
│       │       └── Logger.hpp
│       └── src
│           └── Logger.cpp
└── src
    └── main.cpp

That’s the main.cpp

#include <GLFW/glfw3.h>
#include <Logging/Logger.hpp>

int main(void)
{
    Logger logger;
    GLFWwindow* window;

    /* Initialize the library */
    if (!glfwInit())
        return -1;

    /* Create a windowed mode window and its OpenGL context */
    window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
    if (!window)
    {
        logger.error("Failed to create a window");
        glfwTerminate();
        return -1;
    }

    /* Make the window's context current */
    glfwMakeContextCurrent(window);

    /* Loop until the user closes the window */
    while (!glfwWindowShouldClose(window))
    {
        /* Render here */
        glClear(GL_COLOR_BUFFER_BIT);

        /* Swap front and back buffers */
        glfwSwapBuffers(window);

        /* Poll for and process events */
        glfwPollEvents();
    }

    glfwTerminate();
    return 0;
}

Please refer me to a well structured Cmake tutorial. I need to learn how to use it correctly.

1 answer

1

For the structure you showed a solution could have 2 Cmakelist.txt files:

1. CMakeLists.txt (na raíz do projeto)
2. src/CMakeLists.txt

The first Cmakelists.txt (at the root of the project) might look something like this:

project (test_gl)

# includes do OpenGL
set (GLEW_INC_DIR ${CMAKE_SOURCE_DIR}/libs/GLEW/include/GL)
set (GLFW_INC_DIR ${CMAKE_SOURCE_DIR}/libs/GLFW)

# libs do OpenGL
set (GLEW_LIB_DIR ${CMAKE_SOURCE_DIR}/libs/GLEW/lib)
set (GLFW_LIB_DIR ${CMAKE_SOURCE_DIR}/libs/GLFW/lib)

# includes de log
set (LOG_INC_DIR ${CMAKE_SOURCE_DIR}/libs/Logging/include)

# fontes de log
set (LOG_SRC_DIR ${CMAKE_SOURCE_DIR}/libs/Logging/src)

# diretorio dos fontes da aplicacao
add_subdirectory(${CMAKE_SOURCE_DIR}/src)

The second Cmakelists.txt (in the src directory) might look something like this:

include_directories(${GLEW_INC_DIR} ${GLFW_INC_DIR} ${LOG_INC_DIR})
link_directories(${GLEW_LIB_DIR} ${GLFW_LIB_DIR})
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_SOURCE_DIR}/src)
add_executable(hello_gl main.cpp ${LOG_SRC_DIR)/Logger.cpp}
target_link_directories(hello_gl libGLEW.a libglfw3.a)

The executable program created by the build will have the name "hello_gl", and will be created in the directory "src".

To generate the build files you fall need to run the commands

make _BUILD_DIR
cd _BUILD_DIR
cmake -G generator ..

The first two commands are not really needed, but they produce an "out-of-Tree" build, i.e., the build will not pollute your project directory (all files created by cmake will be under the _BUILD_DIR subdirectory (I used the name _BUILD_DIR, but it can be any name)

The "Generator" in cmake command will tell you which IDE or build system cmake will generate the files for. This page cmake’s website shows supported build systems. For example, to generate a solution for Visual Studio 2013 the above command would be

cmake -G "Visual Studio 12 2013" ..

That’s more or less it.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.