简体   繁体   中英

Linking dependencies for an External Project in CMake

I have the following code in my CMakeLists.txt :

ExternalProject_Add(
    LibSndFile
    URL "http://www.mega-nerd.com/libsndfile/files/libsndfile-1.0.25.tar.gz"
    CONFIGURE_COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/lib/LibSndFile/configure --prefix=<INSTALL_DIR>
    BUILD_COMMAND ${MAKE}
    SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/lib/LibSndFile
    UPDATE_COMMAND ""
    INSTALL_COMMAND ""
    LOG_DOWNLOAD ON
    LOG_CONFIGURE ON
    LOG_BUILD ON
)

Everything configures and builds just fine, until the project is ready for linking. Because LibSndFile depends on flac , libogg , and libvorbis it needs to link to those, but it can't see them.

How can I make it so that my External Project can link to those dependencies installed on my system (is there some LINK_LIBRARY option I'm not seeing)? If they weren't installed on my system, how would I go about linking them to LibSndFile?

So the safest way to do this I found is to use another ExternalProject_Add for the dependencies of LibSndFile:

find_package(FLAC) # test if FLAC is installed on the system
if(${FLAC_FOUND}) # do something if it is found, maybe tell the user
else(${FLAC_FOUND}) # FLAC isn't installed on the system and needs to be downloaded
    ExternalProject_Add(
        FLAC
        URL "http://downloads.xiph.org/releases/flac/flac-1.3.0.tar.xz"
        CONFIGURE_COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/lib/flac/configure --prefix=<INSTALL_DIR>
        BUILD_COMMAND ${MAKE}
        SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/lib/flac
        UPDATE_COMMAND ""
        INSTALL_COMMAND ""
        LOG_DOWNLOAD ON
        LOG_CONFIGURE ON
        LOG_BUILD ON
    )
endif(${FLAC_FOUND})

And then use the DEPENDS directive in LibSndFile to point it to the targets on which the project depends.

ExternalProject_Add(
    LibSndFile
    DEPENDS FLAC libogg libvorbis
    URL "http://www.mega-nerd.com/libsndfile/files/libsndfile-1.0.25.tar.gz"
    CONFIGURE_COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/lib/LibSndFile/configure --prefix=<INSTALL_DIR>
    BUILD_COMMAND ${MAKE}
    SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/lib/LibSndFile
    UPDATE_COMMAND ""
    INSTALL_COMMAND ""
    LOG_DOWNLOAD ON
    LOG_CONFIGURE ON
    LOG_BUILD ON
)

Running configure from libsndfile followed by make install (I believe) produces sndfile.pc which contains the information you want for linking.

How about extracting the library settings from this file? You can add custom steps to ExternalProject_Add according to the documentation .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM