How to Use External Dlls in Cmake Project

How to use external DLLs in CMake project

First, edit your CMakeLists.txt to include your third party library. You'll need two thing: path to header files and library file to link to. For instance:

# searching for include directory
find_path(SIFTGPU_INCLUDE_DIR siftgpu.h)

# searching for library file
find_library(SIFTGPU_LIBRARY siftgpu)

if (SIFTGPU_INCLUDE_DIR AND SIFTGPU_LIBRARY)
# you may need that if further action in your CMakeLists.txt depends
# on detecting your library
set(SIFTGPU_FOUND TRUE)

# you may need that if you want to conditionally compile some parts
# of your code depending on library availability
add_definitions(-DHAVE_LIBSIFTGPU=1)

# those two, you really need
include_directories(${SIFTGPU_INCLUDE_DIR})
set(YOUR_LIBRARIES ${YOUR_LIBRARIES} ${SIFTGPU_LIBRARY})
endif ()

Next, you can do the same for other libraries and when every libraries are detected, link to the target:

target_link_libraries(yourtarget ${YOUR_LIBRARIES})

Then you can configure your project with CMake, but as it doesn't have any magic way to find your installed library, it won't find anything, but it'll create two cache variables: SIFTGPU_INCLUDE_DIR and SIFTGPU_LIBRARY.

Use the CMake GUI to have SIFTGPU_INCLUDE_DIR pointing to the directory containing the header files and SIFTGPU_LIBRARY to the .lib file of your third party library.

Repeat for every third party library, configure again and compile.

How to link shared library *dll with CMake in Windows

Although this question is old. You are targeting the link library wrongly.
target_link_libraries(test2 library.dll) is wrong. This is an example linking SDL2. In the main CMakeList.txt

cmake_minimum_required(VERSION 3.12)
project(GraphicTest)

set(CMAKE_CXX_STANDARD 11)

include_directories("${PROJECT_SOURCE_DIR}/SDL")
add_subdirectory(SDL)

add_executable(GraphicTest main.cpp)
target_link_libraries(GraphicTest SDL2)

and in the library folder. Here SDL, add a CMakeLists.txt

message("-- Linking SDL")
add_library(SDL2 SDL2.dll)
set_target_properties(SDL2 PROPERTIES LINKER_LANGUAGE C)

Use external DLL in cmake build

In my opinion, there are two options:

In case you have put your FreeImage sources in your projects' source-tree, the easiest option may be to use the execute_process() command. Assuming FreeImage is in your projects' source-tree in "3rdparty/FreeImage/" you can do something like,

execute_process( COMMAND make WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/3rdParty/FreeImage )

Optionally, you can copy the dll from 3rdParty/FreeImage/bin into you own bin directory. And then you can write a FreeImageConfig.cmake for importing the library:

add_library( FreeImage IMPORTED )
set_target_properties( FreeImage PROPERTIES IMPORTED_LOCATION ${PROJECT_SOURCE_DIR}/3rdParty/FreeImage/lib )
...

The other option is to make use of the ExternalProject module. You can also take a look at this article from Kitware for an overview of this module. In essence, you specify the full chain of commands needed to get the source, configure the build, build the source and install it. All in your own CMakeLists.txt

How to link any DLL to Cmake project

If CMake does not now a target (i.e. it is not build by CMake) it assumes it to be a prebuild library.

link_directories(
"C:/Program Files/program"
)

add_executable(${PROJECT_NAME} "main.cpp")

target_link_libraries(${PROJECT_NAME}
any_dll
)

Link to a dynamic link library in a cmake project

The property INTERFACE_INCLUDE_DIRECTORIES need to be added to the test target.

set_target_properties(
test
PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES /path/client/lib
IMPORTED_LOCATION /path/client/lib/test.dll
IMPORTED_IMPLIB /path/client/lib/libtest.dll.a
)

NB: The add_subdirectory(lib) command can be safely removed, as you did not place a CMakeLists.txt file there and nothing needs to be built inside this dir.



Related Topics



Leave a reply



Submit