# Downloading pre-built libraries

**URL:** https://discourse.cmake.org/t/downloading-pre-built-libraries/3247
**Category:** Usage
**Created:** [May 2, 2021, 12:40pm UTC](https://discourse.cmake.org/t/downloading-pre-built-libraries/3247 "2021-05-02T12:40:14Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![joshua-barnett](https://discourse.cmake.org/user_avatar/discourse.cmake.org/joshua-barnett/32/1421_2.png) [@joshua-barnett](https://discourse.cmake.org/u/joshua-barnett)
#### Post date: [May 2, 2021, 12:40pm UTC](https://discourse.cmake.org/t/downloading-pre-built-libraries/3247/1 "2021-05-02T12:40:14Z")

</div>

I’m trying to use [`FetchContent`](https://cmake.org/cmake/help/latest/module/FetchContent.html) to download the pre-built development libraries of SDL2.

So they can then be subsequently picked up by a `FindSDL2.cmake` module.

However, at the moment I find that `find_package` is being executabled before the library has downloaded causing the first clean `CMake` build to fail at locating SDL2.

However, on the second run because the pre-built libraries are already downloaded SDL2 can be located.

Any suggestions on how to ensure these pre-built libraries have been downloaded before calling `find_package`?

- [FindSDL2 CMake Modules](https://gitlab.com/aminosbh/sdl2-cmake-modules.git)

My `CMakeLists.txt` file.

```auto
cmake_minimum_required(VERSION 3.19.2)

project(racer VERSION 0.1.0)

# Racer
file(GLOB SOURCES src/*.cpp)
add_executable(${PROJECT_NAME} ${SOURCES})

# OpenGL
find_package(OpenGL REQUIRED)
target_include_directories(${PROJECT_NAME} PRIVATE ${OPENGL_INCLUDE_DIRS})
target_link_libraries(${PROJECT_NAME} ${OPENGL_LIBRARIES})

# FindSDL2 Modules
list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake/sdl2)

# Fetch Content Module
include(FetchContent)

# Visual C++
## SDL2
FetchContent_Declare(
  SDL2
  URL https://www.libsdl.org/release/SDL2-devel-2.0.14-VC.zip
  URL_MD5 2b521c5ec247955dc342235d06ebd874
)
FetchContent_MakeAvailable(SDL2)
set(SDL2_PATH ${sdl2_SOURCE_DIR})
find_package(SDL2 REQUIRED)
target_include_directories(${PROJECT_NAME} PRIVATE ${SDL2_INCLUDE_DIRS})
target_link_libraries(${PROJECT_NAME} ${SDL2_LIBRARIES})
add_custom_command(TARGET racer POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E copy_directory
        ${sdl2_SOURCE_DIR}/lib/x64
        $<TARGET_FILE_DIR:racer>)

```

First run log.

```auto
[main] Configuring folder: racer 
[proc] Executing command: "C:\Program Files\CMake\bin\cmake.EXE" --no-warn-unused-cli -DCMAKE_EXPORT_COMPILE_COMMANDS:BOOL=TRUE -DCMAKE_BUILD_TYPE:STRING=Debug "-DCMAKE_C_COMPILER:FILEPATH=C:\Program Files\LLVM\bin\clang.exe" "-DCMAKE_CXX_COMPILER:FILEPATH=C:\Program Files\LLVM\bin\clang++.exe" -Hc:/Users/Josh/projects/racer -Bc:/Users/Josh/projects/racer/build -G "Unix Makefiles"
[cmake] Not searching for unused variables given on the command line.
[cmake] -- The C compiler identification is Clang 12.0.0 with GNU-like command-line
[cmake] -- The CXX compiler identification is Clang 12.0.0 with GNU-like command-line
[cmake] -- Detecting C compiler ABI info
[cmake] -- Detecting C compiler ABI info - done
[cmake] -- Check for working C compiler: C:/Program Files/LLVM/bin/clang.exe - skipped
[cmake] -- Detecting C compile features
[cmake] -- Detecting C compile features - done
[cmake] -- Detecting CXX compiler ABI info
[cmake] -- Detecting CXX compiler ABI info - done
[cmake] -- Check for working CXX compiler: C:/Program Files/LLVM/bin/clang++.exe - skipped
[cmake] -- Detecting CXX compile features
[cmake] -- Detecting CXX compile features - done
[cmake] -- Found OpenGL: opengl32   
[cmake] CMake Error at C:/Program Files/CMake/share/cmake-3.19/Modules/FindPackageHandleStandardArgs.cmake:218 (message):
[cmake] Could NOT find SDL2 (missing: SDL2_LIBRARY SDL2_INCLUDE_DIR)
[cmake] Call Stack (most recent call first):
[cmake] C:/Program Files/CMake/share/cmake-3.19/Modules/FindPackageHandleStandardArgs.cmake:582 (_FPHSA_FAILURE_MESSAGE)
[cmake] cmake/sdl2/FindSDL2.cmake:313 (FIND_PACKAGE_HANDLE_STANDARD_ARGS)
[cmake] CMakeLists.txt:29 (find_package)
[cmake] 
[cmake] 
[cmake] -- Configuring incomplete, errors occurred!
[cmake] See also "C:/Users/Josh/projects/racer/build/CMakeFiles/CMakeOutput.log".
[cmake] See also "C:/Users/Josh/projects/racer/build/CMakeFiles/CMakeError.log".

```

Second run log (without cleaning the `build` directory).

```auto
[main] Configuring folder: racer 
[proc] Executing command: "C:\Program Files\CMake\bin\cmake.EXE" --no-warn-unused-cli -DCMAKE_EXPORT_COMPILE_COMMANDS:BOOL=TRUE -DCMAKE_BUILD_TYPE:STRING=Debug "-DCMAKE_C_COMPILER:FILEPATH=C:\Program Files\LLVM\bin\clang.exe" "-DCMAKE_CXX_COMPILER:FILEPATH=C:\Program Files\LLVM\bin\clang++.exe" -Hc:/Users/Josh/projects/racer -Bc:/Users/Josh/projects/racer/build -G "Unix Makefiles"
[cmake] Not searching for unused variables given on the command line.
[cmake] -- Found SDL2: C:/Users/Josh/projects/racer/build/_deps/sdl2-src/lib/x64/SDL2.lib (found version "2.0.14") 
[cmake] CMake Warning (dev) at C:/Program Files/CMake/share/cmake-3.19/Modules/FindPackageHandleStandardArgs.cmake:426 (message):
[cmake] The package name passed to `find_package_handle_standard_args` (SDL2main)
[cmake] does not match the name of the calling package (SDL2). This can lead to
[cmake] problems in calling code that expects `find_package` result variables
[cmake] (e.g., `_FOUND`) to follow a certain pattern.
[cmake] Call Stack (most recent call first):
[cmake] cmake/sdl2/FindSDL2.cmake:318 (FIND_PACKAGE_HANDLE_STANDARD_ARGS)
[cmake] CMakeLists.txt:29 (find_package)
[cmake] This warning is for project developers. Use -Wno-dev to suppress it.
[cmake] 
[cmake] -- Found SDL2main: C:/Users/Josh/projects/racer/build/_deps/sdl2-src/lib/x64/SDL2main.lib (found version "2.0.14") 
[cmake] -- Configuring done
[cmake] -- Generating done
[cmake] -- Build files have been written to: C:/Users/Josh/projects/racer/build

```

---

<div class="post-metadata">

### Author: ![scivision](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/s/a87d85/32.png) [@scivision](https://discourse.cmake.org/u/scivision)
#### Post date: [May 31, 2021, 3:48am UTC](https://discourse.cmake.org/t/downloading-pre-built-libraries/3247/2 "2021-05-31T03:48:03Z")

</div>

- What if you try naming the `FetchContent_Declare(SDL_DL` in case there is some variable name conflict.

- does the new (CMake 3.19) `find_package(SDL)` built into CMake work?

I think your approach should work with FetchContent in general
