One solution of a CMakeLists.txt with extra tips
cmake_minimum_required(VERSION 3.8)
set(NAME cmakecopytest)
project(${NAME})
# The place to put built libraries
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_DEBUG "${CMAKE_BINARY_DIR}/bin/windows-64/debug")
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY_DEBUG "${CMAKE_BINARY_DIR}/bin/windows-64/debug")
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG "${CMAKE_BINARY_DIR}/bin/windows-64/debug")
# Copying pre-compiled dll libraries next to the built libraries for them to be found in runtime without extra effort
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/deps/FreeImage/FreeImagePlus.dll" "${CMAKE_BINARY_DIR}/bin/windows-64/debug/FreeImagePlus.dll" COPYONLY)
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/deps/FreeImage/FreeImage.dll" "${CMAKE_BINARY_DIR}/bin/windows-64/debug/FreeImage.dll" COPYONLY)
add_executable(${NAME}_exe main.cpp)
target_include_directories(${NAME}_exe PUBLIC ./ include/ deps/FreeImage)
target_link_libraries(${NAME}_exe PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/deps/FreeImage/FreeImage.lib" PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/deps/FreeImage/FreeImagePlus.lib")
# target_link_libraries(${NAME}_exe PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/deps/FreeImage/FreeImage" PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/deps/FreeImage/FreeImagePlus") # ERROR LNK1104: cannot open file '..\deps\FreeImage\FreeImage.obj'
Extra tips:
- CMAKE_BINARY_DIR contains the place where you built the solution (i.e., D:/Work/Project/build)
Setting the output directories and then copying the precompiled libraries to that same location is a easy way to have your code run without any added extra configurations in MSVS
math(EXPR platform_bits "${CMAKE_SIZEOF_VOID_P} * 8")
set(platform_dir bin/${CMAKE_SYSTEM_NAME}-${platform_bits})
foreach(config DEBUG RELEASE RELWITHDEBINFO MINSIZEREL)
foreach(var
CMAKE_ARCHIVE_OUTPUT_DIRECTORY_${config}
CMAKE_LIBRARY_OUTPUT_DIRECTORY_${config}
CMAKE_RUNTIME_OUTPUT_DIRECTORY_${config}
)
set(${var} "${CMAKE_BINARY_DIR}/${platform_dir}/${config}")
string(TOLOWER "${${var}}" ${var})
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/deps/FreeImage/FreeImagePlus.dll ${var}/FreeImagePlus.dll COPYONLY)
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/deps/FreeImage/FreeImage.dll ${var}/FreeImage.dll COPYONLY)
endforeach()
endforeach()
And this for cycle does that for you for all the desired configurations
However, I do not like having to specify the '.lib' in target_link_libraries(...FreeImage.lib...)
Anyone else has a solution to be able to say target_link_libraries(${NAME}_exe PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/deps/FreeImage/FreeImage" PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/deps/FreeImage/FreeImagePlus")
while avoiding the linking error LNK1104: cannot open file '..\deps\FreeImage\FreeImage.obj'
?