0

Anyone know how to copy the SQLite3.dll into the executable directory using CMAKE? I am able to use sqlite in generated VS project but when i try to run the exe it cannot find the dll.

I am guessing that CMAKE would write a copy command into the post-build events in the VS project settings? But how is that done through CMAKE.

cmake_minimum_required(VERSION 3.10)
add_executable(APP)
target_sources(APP 
                PRIVATE
                    include/box.hpp
                    src/box.cpp
                    src/main.cpp
                )       
target_include_directories(APP PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)                    


# SQlite
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_SOURCE_DIR}/../cmake/Modules)
set(SQLite3_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../external/sqlite3/include)
set(SQLite3_LIBRARY ${CMAKE_CURRENT_SOURCE_DIR}/../external/sqlite3/libraries/win10/x64)
find_package (SQLite3)
if (SQLITE3_FOUND)
  include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../external/sqlite3/include)
  list(APPEND EXTRA_LIBS ${CMAKE_CURRENT_SOURCE_DIR}/../external/sqlite3/libraries/win10/x64/sqlite3.lib)
endif (SQLITE3_FOUND)



list(APPEND EXTRA_LIBS LIBCORE)
list(APPEND EXTRA_LIBS Boost::filesystem)
target_link_libraries(APP PRIVATE ${EXTRA_LIBS})




# INSTALL

install(TARGETS APP DESTINATION ${PROJECT_BINARY_DIR}/TEMP/bin)
ariia
  • 63
  • 1
  • 7
  • 1
    Does this answer your question? [How to copy DLL files into the same folder as the executable using CMake?](https://stackoverflow.com/questions/10671916/how-to-copy-dll-files-into-the-same-folder-as-the-executable-using-cmake) – Kevin Aug 08 '20 at 15:18
  • I wanted to avoid using cmake in that way. I wanted tou use one of the cmake language features. – ariia Aug 12 '20 at 19:12

1 Answers1

0

It is possible to use cmake in CMakeLists.txt as a tool and it has file copy functionality

Something like this might be a start

set(source_file  "D:/temp/from/sqlite3.dll")
set(target_file "D:\\temp\\to\\sqlite3.dll")
execute_process( COMMAND  ${CMAKE_COMMAND} -E copy_if_different "${source_file}" "${target_file}" RESULT_VARIABLE sResult )

CMake tutorial

Per Ghosh
  • 449
  • 5
  • 10
  • I got this to work but not sure it is recommened ```file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/../external/sqlite3-x64/sqlite3.dll DESTINATION ${CMAKE_BINARY_DIR}/proj/Debug) file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/../external/sqlite3-x64/sqlite3.dll DESTINATION ${CMAKE_BINARY_DIR}/proj/Release) ``` – ariia Aug 08 '20 at 19:14
  • That will work too, almost same as in my answer, just another command. What you could do is to extend it some and check if the file `sqlite3.dll` exists in target directories before copying. If not found then copy, if it is there, skip copying – Per Ghosh Aug 08 '20 at 23:22
  • Ah ok, thats good to know. Have you used file(GET_RUNTIME_DEPENDENCIES ? I kept getting errors when i try to use it as described.But it seems the recommened way. – ariia Aug 09 '20 at 12:20
  • No I haven't used GET_RUNTIME_DEPENDENCIES. One advice is that you always need to know what is needed and that you can do it by hand. Never trust tools to do things for you. First do it you self and if you know how to you could use tooling to save time but you have to know. If there is problems and you don't know what is required you are i "no man's land" – Per Ghosh Aug 09 '20 at 21:08