How to use Qhull as an external project in CMake?

1k Views Asked by At

I have tried to use QHull and libqhullcpp as an external project in CMake, but I was not successful.

So far I've used these commands which is a modified version of this:

include(ExternalProject)
ExternalProject_add(qhull
        GIT_REPOSITORY "https://github.com/qhull/qhull.git"
        GIT_TAG "2019.1"
        GIT_PROGRESS TRUE
        SOURCE_DIR ${CMAKE_SOURCE_DIR}/thirdparty/qhull
        BINARY_DIR ${CMAKE_BINARY_DIR}/thirdparty-build/qhull
        CMAKE_ARGS "-DCMAKE_INSTALL_PREFIX=${CMAKE_BINARY_DIR}/thirdparty-install"
        CMAKE_CACHE_ARGS "-DCMAKE_POSITION_INDEPENDENT_CODE:BOOL=true"
        )


### qhullstatic reentrant library
add_library(libqhullstatic_r STATIC IMPORTED)
set_property(TARGET libqhullstatic_r PROPERTY IMPORTED_LOCATION ${CMAKE_BINARY_DIR}/thirdparty-build/qhull/${CMAKE_STATIC_LIBRARY_PREFIX}qhullstatic_r${CMAKE_STATIC_LIBRARY_SUFFIX})
set_property(TARGET libqhullstatic_r PROPERTY IMPORTED_LOCATION_DEBUG ${CMAKE_BINARY_DIR}/thirdparty-build/qhull/Debug/${CMAKE_STATIC_LIBRARY_PREFIX}qhullstatic_r${CMAKE_STATIC_LIBRARY_SUFFIX})
set_property(TARGET libqhullstatic_r PROPERTY IMPORTED_LOCATION_RELEASE ${CMAKE_BINARY_DIR}/thirdparty-build/qhull/Release/${CMAKE_STATIC_LIBRARY_PREFIX}qhullstatic_r${CMAKE_STATIC_LIBRARY_SUFFIX})
add_dependencies(libqhullstatic_r qhull)
### qhullcpp library
add_library(libqhullcpp STATIC IMPORTED)
set_property(TARGET libqhullcpp PROPERTY IMPORTED_LOCATION ${CMAKE_BINARY_DIR}/thirdparty-build/qhull/${CMAKE_STATIC_LIBRARY_PREFIX}qhullcpp${CMAKE_STATIC_LIBRARY_SUFFIX})
set_property(TARGET libqhullcpp PROPERTY IMPORTED_LOCATION_DEBUG ${CMAKE_BINARY_DIR}/thirdparty-build/qhull/Debug/${CMAKE_STATIC_LIBRARY_PREFIX}qhullcpp${CMAKE_STATIC_LIBRARY_SUFFIX})
set_property(TARGET libqhullcpp PROPERTY IMPORTED_LOCATION_RELEASE ${CMAKE_BINARY_DIR}/thirdparty-build/qhull/Release/${CMAKE_STATIC_LIBRARY_PREFIX}qhullcpp${CMAKE_STATIC_LIBRARY_SUFFIX})
set_property(TARGET libqhullcpp PROPERTY INTERFACE_LINK_LIBRARIES libqhullstatic_r)
add_dependencies(libqhullcpp qhull)

add_executable(${PROJECT_NAME} src/main.cpp)
target_link_libraries(${PROJECT_NAME} ${libqhullcpp})

My project cannot find any file of the Qhull code and if I add

#include <libqhullcpp/Qhull.h> 
orgQhull::Qhull qhull;

to my project it will throw this compile error:

CMakeFiles/MyProject.dir/src/main.cpp.o: In function `main':
~/MyProject/src/main.cpp:108: undefined reference to `orgQhull::Qhull::Qhull()'
~/MyProject/src/main.cpp:108: undefined reference to `orgQhull::Qhull::~Qhull()'
~/MyProject/src/main.cpp:108: undefined reference to `orgQhull::Qhull::~Qhull()'
collect2: error: ld returned 1 exit status
CMakeFiles/MyProject.dir/build.make:83: recipe for target 'MyProject' failed
make[3]: *** [MyProject] Error 1
CMakeFiles/Makefile2:104: recipe for target 'CMakeFiles/MyProject.dir/all' failed
make[2]: *** [CMakeFiles/MyProject.dir/all] Error 2
CMakeFiles/Makefile2:111: recipe for target 'CMakeFiles/MyProject.dir/rule' failed
make[1]: *** [CMakeFiles/MyProject.dir/rule] Error 2
Makefile:131: recipe for target 'MyProject' failed
make: *** [MyProject] Error 2
1

There are 1 best solutions below

0
On

I've modified your CMakeLists.txt file and done some optimization, so you don't build in source. My tree gives this:

--QHull
|   |--CMakeLists.txt
|   |--CMakeLists.txt.in
|   |--src
|       |--main.cpp
|--build

with CMakeLists.txt:

cmake_minimum_required(VERSION 3.0)
project(TestQHull)

configure_file(CMakeLists.txt.in qhull-download/CMakeLists.txt)

execute_process(COMMAND "${CMAKE_COMMAND}" -G "${CMAKE_GENERATOR}" .
    WORKING_DIRECTORY "${CMAKE_BINARY_DIR}/qhull-download"
)
execute_process(COMMAND "${CMAKE_COMMAND}" --build .
    WORKING_DIRECTORY "${CMAKE_BINARY_DIR}/qhull-download"
)

add_subdirectory("${CMAKE_BINARY_DIR}/qhull-src"
                 "${CMAKE_BINARY_DIR}/qhull-build"
)

include_directories(${CMAKE_BINARY_DIR}/qhull-src/src)
link_directories(${CMAKE_BINARY_DIR}/qhull-build)

### qhullstatic reentrant library
add_library(libqhullstatic_r STATIC IMPORTED)
set_property(TARGET libqhullstatic_r PROPERTY IMPORTED_LOCATION ${CMAKE_BINARY_DIR}/qhull-build/${CMAKE_STATIC_LIBRARY_PREFIX}qhullstatic_r${CMAKE_STATIC_LIBRARY_SUFFIX})
add_dependencies(libqhullstatic_r qhull)
### qhullcpp library
add_library(libqhullcpp STATIC IMPORTED)
set_property(TARGET libqhullcpp PROPERTY IMPORTED_LOCATION ${CMAKE_BINARY_DIR}/qhull-build/${CMAKE_STATIC_LIBRARY_PREFIX}qhullcpp${CMAKE_STATIC_LIBRARY_SUFFIX})
set_property(TARGET libqhullcpp PROPERTY INTERFACE_LINK_LIBRARIES libqhullstatic_r)
add_dependencies(libqhullcpp qhull)

add_executable(${PROJECT_NAME} src/main.cpp)
target_link_libraries(${PROJECT_NAME} libqhullstatic_r libqhullcpp)

and

CMakeLists.txt.in contents this:

cmake_minimum_required(VERSION 3.0)
project(qhull-download NONE)

include(ExternalProject)

ExternalProject_add(qhull
    GIT_REPOSITORY https://github.com/qhull/qhull.git
    GIT_TAG 2019.1
    GIT_PROGRESS TRUE
    SOURCE_DIR ${CMAKE_BINARY_DIR}/qhull-src
    BINARY_DIR ${CMAKE_BINARY_DIR}/qhull-build
    CMAKE_ARGS -DCMAKE_INSTALL_PREFIX=${CMAKE_BINARY_DIR}
    CMAKE_CACHE_ARGS ""
    CONFIGURE_COMMAND ""
    BUILD_COMMAND ""
    INSTALL_COMMAND ""
    TEST_COMMAND ""
)

If you cd build && cmake ../QHull, it'll download and start building qhull for you.

The only drawback: the ExternalProjects keep the ${CMAKE_SOURCE_DIR}\QHull\src as INCLUDE_DIRECTORIES instead of the needed ${CMAKE_BINARY_DIR}\qhull-src\src.

As I couldn't fix this, my work-around is to replace -I${CMAKE_SOURCE_DIR}\QHull\src with -I${CMAKE_SOURCE_DIR}\QHull\src in the generator script (e.g. build.ninja in my case) after cmake configure step.

After that, I was able to build my own example main.cpp.