Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

When I add a new subdirectory in CMake, the bug occurs

I have project that builds with CMake.
Here is project hierarchy:

Project
   Dependencies
      glfw
   Source
      CMakeList.txt
      main.cpp
   CMakeList.txt

Then I try to add GLFW to my project and my CMakeList file looks like:

cmake_minimum_required(VERSION 3.8)

# Enable Hot Reload for MSVC compilers if supported.
if (POLICY CMP0141)
cmake_policy(SET CMP0141 NEW)
set(CMAKE_MSVC_DEBUG_INFORMATION_FORMAT "$<IF:$<AND:$<C_COMPILER_ID:MSVC>,$<CXX_COMPILER_ID:MSVC>>,$<$<CONFIG:Debug,RelWithDebInfo>:EditAndContinue>,$<$<CONFIG:Debug,RelWithDebInfo>:ProgramDatabase>>")
endif()

project( Project )

# Include sub - projects.

set(GLFW_BUILD_DOCS OFF CACHE BOOL "" FORCE)
set(GLFW_BUILD_TESTS OFF CACHE BOOL "" FORCE)
set(GLFW_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE)

set(MAIN "Source/main.cpp")

add_subdirectory("Dependencies/glfw")

add_executable( Project ${MAIN} )

target_link_libraries( Project glfw )

With the version of the file above, everything works fine.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

When I add this line ‘add_subdirectory("Source")`’ to CMakeList, an error occurs in the main.cpp that the file cannot be found: ‘GLFW/glfw3.h’. I want to understand why the error occurs.

main.cpp:

#include <stdio.h>
#include <stdlib.h>

#include <GLFW/glfw3.h>

int main()
{
    //glewExperimental = true; // Needed for core profile
    if (!glfwInit())
    {
        fprintf(stderr, "Failed to initialize GLFW\n");
        return -1;
    }
}

EDIT:
CMakeList.txt in Source directory:

add_executable (CMakeTarget "Game Engine.cpp" )

if (CMAKE_VERSION VERSION_GREATER 3.12)
  set_property(TARGET CMakeTarget PROPERTY CXX_STANDARD 20)
endif()

>Solution :

The issue is that you link Project to glfw but do not link CMakeTarget (strange name, btw) to glfw.

You need to add the line:

target_link_libraries( CMakeTarget glfw )

This is because CMake uses link libraries to determine how to set up include paths for the compiler as well as for linking. Specifically, it works because glfw has a PUBLIC target_include_directories here: https://github.com/glfw/glfw/blob/9a87635686c7fcb63ca63149c5b179b85a53a725/src/CMakeLists.txt#L140

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading