Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
310 views
in Technique[技术] by (71.8m points)

c++ - How to use external DLLs in CMake project

I've been searching over the internet but I couldn't find anything that would answer my question (or I don't know what to search for).

Anyway here's my issue: I want to use 3rdParty libraries (.dll files) in my CMake project. Library (https://github.com/pitzer/SiftGPU) that I want to include is open source and is also available in binary which I would like to use and also uses CMake as build tool if that's relevant.

I hope I was clear enough.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...