How to use external DLLs in CMake project

I have searched through the internet, but I couldn't find anything that could answer my question (or I don't know what to look for).

Anyway, here's my problem: I want to use 3rdParty libraries (DLLs) in my CMake project. The library ( https://github.com/pitzer/SiftGPU ) that I want to include is open source and is also available in a binary format that I would like to use and also uses CMake as needed.

Hope I was clear enough.

+9


source to share


1 answer


First, edit your CMakeLists.txt to include your third party library. You will need two things: the path to the header files and the library file to link. For example:

# 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 ()

      

Then you can do the same for other libraries, and when all libraries are found, target reference:

target_link_libraries(yourtarget ${YOUR_LIBRARIES})

      



Then you can customize your project with CMake, but since it doesn't have a magical way to find your installed library, it won't find anything, but it will create two cache variables: SIFTGPU_INCLUDE_DIR

and SIFTGPU_LIBRARY

.

CMake use the graphical interface to SIFTGPU_INCLUDE_DIR

point to the directory that contains the header files and SIFTGPU_LIBRARY

to .lib

file your third party library.

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

+11


source







All Articles