c++cmakehdfql

Linking external HDFql library in CMake


I have downloaded the HDFql library and put the whole lot in /usr/local/ in my linux system. I now wish to use this library in a ROS application, so I have tried to link it in my CMakeList, which looks as following:

cmake_minimum_required(VERSION 3.2)
project(data_generator)

add_compile_options(-std=c++11)
set(CMAKE_BUILD_TYPE Release) # Release, RelWithDebInfo
set(CMAKE_CXX_FLAGS "-std=c++11 -pthread ${CMAKE_CXX_FLAGS}")

find_package(catkin_simple REQUIRED)
catkin_simple(ALL_DEPS_REQUIRED)

find_package(OpenCV REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})

set(HDFQL_ROOT "/usr/local/hdfql-2.1.0")

include_directories(${HDFQL_ROOT}/include)
find_library(HDFQL_LIB HDFql)

if (HDFQL_LIB)
  message("Library(HDFQL_LIB) found in ${HDFQL_LIB}")
else()
  message(FATAL_ERROR "Library (HDFQL_LIB) not found")
endif()

cs_add_executable(
        ${PROJECT_NAME}
        src/main.cpp
        src/data_create.cpp
        src/event_definitions.cpp
)

target_include_directories(${PROJECT_NAME}
    PUBLIC "${HDFQL_ROOT}/include"
)

target_link_libraries(${PROJECT_NAME}
   ${OpenCV_LIBRARIES}
   ${HDFQL_LIB}
)

# CMake Indexing
FILE(GLOB_RECURSE LibFiles "include/*")
add_custom_target(headers SOURCES ${LibFiles})

cs_install()
cs_export()

When I build this, it works fine and outputs the message:

Library(HDFQL_LIB) found in /usr/local/hdfql-2.1.0/lib/libHDFql.so

with the following in my main.cpp file:

#include <HDFql.hpp>
....
std::cout <<"HDFql version: " << HDFql::Version << std::endl;

the output is: HDFql version: 2.1.0.

However, as soon as I try to actually use functions of the library - for example:

#include <HDFql.hpp>
....
std::cout <<"HDFql version: " << HDFql::Version << std::endl;
HDFql::execute("CREATE FILE /home/user/test.h5");

I get the error: main.cpp:(.text+0x1858): undefined reference to `HDFql::execute(char const*)'

This suggests to me that while CMake has no issue with the includes, it is having trouble linking the actual library (ie including the libHDFql.a/libHDFql.so files). Can anyone tell me what I'm doing wrong here?

Many thanks!


Solution

  • The problem was that I needed to include the library /usr/local/hdfql-2.1.0/wrapper/cpp/libHDFql.so, where I was using /usr/local/hdfql-2.1.0/lib/libHDFql.so. It's pretty maddening, since the reference manual doesn't make any mention of this and I spent way too long figuring this out. Ohwell, I hope this helps anyone else with this problem.

    For reference, here is a minimal catkin-style CMakeLists that will work:

    cmake_minimum_required(VERSION 3.2)
    project(project_name)
    
    add_compile_options(-std=c++11)
    
    find_package(catkin_simple REQUIRED)
    catkin_simple(ALL_DEPS_REQUIRED)
    
    set(HDFQL_ROOT "path/to/hdfql-2.1.0")
    
    include_directories(${HDFQL_ROOT}/include)
    
    set(CMAKE_BUILD_TYPE Release)
    set(CMAKE_CXX_FLAGS "-std=c++11 -pthread ${CMAKE_CXX_FLAGS}")
    
    cs_add_executable(
        ${PROJECT_NAME}
        # your source file 1
        # your source file 2 
        # ...
    )
    
    target_include_directories(${PROJECT_NAME}
        PUBLIC "${HDFQL_ROOT}/include"
    )
    
    target_link_libraries(
       ${PROJECT_NAME}
       ${OpenCV_LIBRARIES}
    )
    
    target_link_libraries(
       ${PROJECT_NAME}
       "${HDFQL_ROOT}/wrapper/cpp/libHDFql.so"
    )
    

    Of course the absolute paths aren't very pretty, the alternative is to add /usr/local/hdfql-2.1.0/lib to the environment variable CMAKE_PREFIX_PATH (eg export CMAKE_PREFIX_PATH="/usr/local/hdfql-2.1.0/lib:$CMAKE_PREFIX_PATH").