r/cmake•
Super-Type7369

Dlls only copied if built twice

I have a simple CMake project where I have to sub directories.

Lib : Compiles as a SHARED library and itself depends on the 3rd party libpng
App : Compiles as an executable, and depends on the Lib project above

I am then opening this in Visual Studio, invoking Configure, and then building.

The problem is that when I build, the output of App only contains `Lib.dll`, it is missing `libpng16.dll` and `z.dll`

If I then do a rebuild, they then appear?

Main CmakeLists.txt

cmake_minimum_required (VERSION 3.10)

project ("CMakeProject")

find_package(PNG REQUIRED CONFIG)

add_subdirectory(Lib)
add_subdirectory(App)

Lib

add_library (Lib SHARED
"Lib.cpp" 
"Lib.h"
)

target_include_directories(Lib
    PUBLIC
        ${CMAKE_CURRENT_SOURCE_DIR}
)

target_link_libraries(Lib PRIVATE 
PNG::PNG
)

target_compile_definitions(Lib PRIVATE LIB_EXPORTS)

App

add_executable (App
"App.cpp" 
"App.h"
)

target_link_libraries(App PRIVATE 
Lib
)

add_custom_command(TARGET App POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E copy_if_different
        $<TARGET_RUNTIME_DLLS:App>
        $<TARGET_FILE_DIR:App>
    COMMAND_EXPAND_LISTS
)

Any ideas?

First Build

>------ Build All started: Project: CMakeExample, Configuration: x64-release ------
  [0/1] Re-running CMake...
  -- Running vcpkg install
  Detecting compiler hash for triplet x64-windows...
  Compiler found: C:/Program Files/Microsoft Visual Studio/18/Insiders/VC/Tools/MSVC/14.51.36231/bin/Hostx64/x64/cl.exe
  The following packages are already installed:
      libpng:x64-windows@1.6.58
    * vcpkg-cmake:x64-windows@2024-04-23
    * vcpkg-cmake-config:x64-windows@2024-05-23
    * zlib:x64-windows@1.3.2#1
  libpng provides CMake targets:
  
    find_package(PNG REQUIRED)
    target_link_libraries(main PRIVATE PNG::PNG)
  
  All requested installations completed successfully in: 251 us
  -- Running vcpkg install - done
  -- Configuring done (2.9s)
  -- Generating done (0.0s)
  -- Build files have been written to: C:/Users/xxx/Desktop/CMakeExample/out/build/x64-release
  [1/2] Linking CXX executable App\App.exe
  C:/Users/xxx/Desktop/CMakeExample/out/build/x64-release/App/App.exe: message: deploying dependencies

Build All succeeded.

Second build (rebuild all clicked) ... now it does more in the deploy step

>------ Rebuild All started: Project: CMakeExample, Configuration: x64-release ------
  [1/1] Cleaning all built files...
  Cleaning... 5 files.
  [1/4] Building CXX object App\CMakeFiles\App.dir\App.cpp.obj
  [2/4] Building CXX object Lib\CMakeFiles\Lib.dir\Lib.cpp.obj
  [3/4] Linking CXX shared library Lib\Lib.dll
  C:/Users/xxx/Desktop/CMakeExample/out/build/x64-release/Lib/Lib.dll: message: deploying dependencies
  C:\Users\xxx\Desktop\CMakeExample\out\build\x64-release\vcpkg_installed\x64-windows\bin\libpng16.dll -> C:\Users\xxx\Desktop\CMakeExample\out\build\x64-release\Lib\libpng16.dll skipped, up to date
  C:/Users/xxx/Desktop/CMakeExample/out/build/x64-release/Lib\libpng16.dll: message: deploying dependencies
  C:\Users\xxx\Desktop\CMakeExample\out\build\x64-release\vcpkg_installed\x64-windows\bin\z.dll -> C:\Users\xxx\Desktop\CMakeExample\out\build\x64-release\Lib\z.dll skipped, up to date
  C:/Users/xxx/Desktop/CMakeExample/out/build/x64-release/Lib\z.dll: message: deploying dependencies
  [4/4] Linking CXX executable App\App.exe
  C:/Users/xxx/Desktop/CMakeExample/out/build/x64-release/App/App.exe: message: deploying dependencies
  C:/Users/xxx/Desktop/CMakeExample/out/build/x64-release/App\Lib.dll: message: deploying dependencies
  C:\Users\xxx\Desktop\CMakeExample\out\build\x64-release\vcpkg_installed\x64-windows\bin\libpng16.dll -> C:\Users\xxx\Desktop\CMakeExample\out\build\x64-release\App\libpng16.dll done
  C:/Users/xxx/Desktop/CMakeExample/out/build/x64-release/App\libpng16.dll: message: deploying dependencies
  C:\Users\xxx\Desktop\CMakeExample\out\build\x64-release\vcpkg_installed\x64-windows\bin\z.dll -> C:\Users\xxx\Desktop\CMakeExample\out\build\x64-release\App\z.dll done
  C:/Users/xxx/Desktop/CMakeExample/out/build/x64-release/App\z.dll: message: deploying dependencies

Rebuild All succeeded.

Posted Jul 11, 2026, 1:12 PM UTC

28
Loading comments…