2

I am trying to clone and build 3rd party libraries from git using Cmake's ExternalProject_Add.

Could someone please explain to me how to use ExternalProject_Add correctly?

Currently I have this in my CmakeLists.txt

cmake_minimum_required(VERSION 3.10)
project(openGLtest)

include(ExternalProject)
set(CMAKE_CXX_STANDARD 17)
set(OUTPUT_DIR ${PROJECT_SOURCE_DIR}/glfw)
set(CMAKE_CXX_FLAGS "-framework Cocoa -framework IOKit -framework CoreFoundation -framework CoreVideo")

ExternalProject_Add(glfw
        GIT_REPOSITORY https://github.com/glfw/glfw.git
        CMAKE_ARGS  -DCMAKE_INSTALL_PREFIX=${OUTPUT_DIR})

include_directories(glad/include)
include_directories(${OUTPUT_DIR}/include)
link_directories(${OUTPUT_DIR}/lib)

add_executable(openGLtest glad/src/glad.c main.cpp)
add_dependencies(openGLtest glfw)
target_link_libraries(openGLtest glfw3.a)

I'm using GLFW, and GLAD. The issue is that GLFW's dependencies are not being linked when it get's compiled, that's why I added the set CMAKE_CXX_FLAGS line. Everything compiles now, but I am pretty sure that shoehorning in system specific compiler flags is not the right solution.

-EDIT-

My problem is not with this library specifically, it's about the finding the correct way of setting up my Cmake to get the libraries that my project needs from the internet when I build the project. I'm sure Cmake is capable of pulling, or downloading the necessary libraries when it builds my project. It also seems to me that using ExternalProject_Add is the right way to do that. I just don't know how to use ExternalProject_Add to get the libraries, without there being weird compile errors.

Any help would be greatly appreciated, I've been stuck with this problem for almost a week at this point.

GuiGWR
  • 125
  • 1
  • 20
T_R_O_N
  • 29
  • 6
  • "I am pretty sure that shoehorning in system specific compiler flags is not the right solution." - If you mean, that setting `CMAKE_CXX_FLAGS` isn't a true way, then you are right: In CMake, `-framework` option is passed via `target_link_libraries` call (see e.g. [that question](https://stackoverflow.com/questions/27585896/how-to-use-framework-using-cmake)). – Tsyvarev Jul 03 '18 at 13:12
  • 1
    You’re right, that would make things better, but it still won’t completely solve the problem. By that I mean my inclusion of the GLFW library would still not be correct, because I’m still manually linking the library’s dependencies. From what I understand it is possible to build GLFW from source, so that it automatically detects which system it’s being built on, and the automatically links the correct dependencies. That is what I’m trying to achieve with ExternalProject_Add, but I don’t know why it’s not working. – T_R_O_N Jul 03 '18 at 13:57
  • `ExternalProject_Add` operates at build-time. If you want to make sure downloading happens at configure-time, consider using [`FetchContent`](https://cmake.org/cmake/help/latest/module/FetchContent.html), which wraps the parts of `ExternalProject_Add` that do the download, update, and patch steps, and then does the rest with `add_subdirectory` instead of requiring installing the external project and then adding it with `find_package`. – starball Aug 30 '22 at 06:36
  • When you use `ExternalProject`, you need to set up a lot of boilerplate to make the external project use the same settings as the "parent" project, such as the toolchain file, c++ compiler, c++ standard, configuration types / build type, etc. There are pros and cons to choosing `ExternalProject` vs `FetchContent` that mostly revolve around how much control you want/need when it comes to isolating the configs of the top-level and external projects from each other. – starball Aug 30 '22 at 06:40

0 Answers0