[CMake] How to Build Subdirectory CMake Project GLFW and Change Targets

frodak17 frodak17 at gmail.com
Fri May 17 09:43:38 EDT 2019


On Fri, May 17, 2019 at 2:55 AM junk email <mlg420sniperxxx69 at gmail.com>
wrote:

> Hello,
>
> I am trying to understand how to build a subdirectory project that has its
> own CMakeLists.txt file already setup. In this case, I am using the open
> source library GLFW to help me with some of my OpenGL applications. Here is
> a sample directory structure I am working with.
>
> Root
> ├── CMakeLists.txt
> ├── MainProject
> ├── build
> └── glfw-3.3
>
> The folder MainProject is where I will be doing all my own development and
> has a src, include, and lib folder associated with it. The glfw-3.3
> directory was downloaded from online and has its own CMakeLists.txt file as
> mentioned previously. So if I were to build my project from the current
> build directory shown here, how should I add the glfw library to it and
> also change its target outputs?
>
> Answers online suggest using ExternalProject_Add(), while others say to
> simply add_subdirectory() and then change some of the properties. I want to
> be able to have a user run the root CMake file and have the targets be
> correctly placed (since users might be on different operating systems).
> Either way I have not been able to find an explicit example, perhaps my
> Googling skills for CMake are lacking, and any help on this would be
> appreciated. Thank you for your time.
>
> Best,
> mlg
>
>
>
If Root/CMakeLists.txt is the root of the source directory it's pretty
simple.  Just use add_subdirectory() as usual.

Again I don't follow why GLFW targets would have to change.  It builds in
the Build directory like everything else.  In this case it would be
Root/build/glfw-3.3 (assuming Root/build is what was supplied on the
command line as something like "cmake -S Root -B Root/build").

For me I downloaded GLFW and put it outside my source tree.  Then I just
used a relative path to it's location.  Because it is outside the source
tree I had to specify the build directory to use.

But GLFW looked to be pretty simple and there wasn't any changes to its
CMakeLists.txt file.  I just set some of the options to blank so that they
wouldn't be built.

This is from the CMakeLists.txt for my directory structure:
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(GLFW_INSTALL OFF CACHE BOOL "" FORCE)
add_subdirectory(../glfw-3.2.1 glfw)
...
target_link_libraries(application glfw)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://cmake.org/pipermail/cmake/attachments/20190517/b5709cc0/attachment.html>


More information about the CMake mailing list