[Dune] Link to external shared library

Tobias Kasper Skovborg Ritschel tobk at dtu.dk
Wed May 17 16:22:05 CEST 2017


Hi

My question in short:
Do I need to use DUNE buildsystem functionality (e.g. dune_add_library) to link with an external shared library, or can I use CMake functionality?

The longer explanation:
I am working on a DUNE module (ResSim), and I would like to use functions from a shared library (libThermoLib.so) in that code. I have tried to use CMake commands in the following way

-------------------------------------------------------------------------------------------------------------------------------------------

# Find the ThermoLib library
find_library(thermolib_LIBRARY ThermoLib)
message(STATUS "setting thermolib_LIBRARY="${thermolib_LIBRARY}"")

# Find the ThermoLib include directory
find_path(thermolib_INCLUDE_DIRS ThermoLib.h)
message(STATUS "setting thermolib_INCLUDE_DIRS="${thermolib_INCLUDE_DIRS}"")

# Add library
add_library(thermolib SHARED IMPORTED GLOBAL)
set_target_properties(thermolib PROPERTIES
IMPORTED_LOCATION "${thermolib_LIBRARY}"
INTERFACE_INCLUDE_DIRECTORIES "${thermolib_INCLUDE_DIRS}")

# Add executable
add_executable("ResSim" main.cc)

# Link libraries
target_link_dune_default_libraries("ResSim")
target_link_libraries("ResSim" thermolib)

-------------------------------------------------------------------------------------------------------------------------------------------

I use "dunecontrol --module=ResSim --opts=./ResSim/settings/release.opts all" to compile the project, where the release.opts file contains

-------------------------------------------------------------------------------------------------------------------------------------------

CMAKE_FLAGS="
-DCMAKE_C_COMPILER='/usr/bin/gcc'
-DCMAKE_CXX_COMPILER='/usr/bin/g++'
-DCMAKE_CXX_FLAGS='-Wall'
-DCMAKE_CXX_FLAGS_DEBUG='-O3 -DNDEBUG -g0 -funroll-loops'
-DDUNE_SYMLINK_TO_SOURCE_TREE=1
-DCMAKE_BUILD_TYPE=Release
-DCMAKE_PREFIX_PATH='/home/tobk/Documents/Projects/ThermoLib/lib;/home/tobk/Documents/Projects/ThermoLib/hdr'
"
BUILDDIR=release

-------------------------------------------------------------------------------------------------------------------------------------------

The -DCMAKE_PREFIX_PATH ensures that find_library and find_path finds libThermoLib.so and its include directory (and I have checked that they do). However, I get a linker error:

CMakeFiles/ResSim.dir/main.cc.o: In function `main':
main.cc:(.text.startup+0x87): undefined reference to `LoadParams(int*, int, int, double*, double*)'
collect2: error: ld returned 1 exit status

where LoadParams is a function defined in libThermoLib.so, i.e.  "nm -C libThermoLib.so | grep LoadParams" returns

0000000000001b50 T LoadParams

>From what I have found in various StackOverflow threads etc., the above should work with CMake, but perhaps I've missed something minor. Any help is appreciated.

PS: The DUNE buildsystem docs appears to be down (https://www.dune-project.org/sphinx/core-2.4/)

Best regards
Tobias Ritschel
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.dune-project.org/pipermail/dune/attachments/20170517/5e7f2f69/attachment.htm>


More information about the Dune mailing list