Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 489
syb0rg Avatar asked Sep 30 '22 10:09

syb0rg


1 Answers

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
)
like image 65
syb0rg Avatar answered Oct 18 '22 11:10

syb0rg