[Dune] Link to external shared library

Tobias Kasper Skovborg Ritschel tobk at dtu.dk
Thu May 18 08:55:03 CEST 2017


I don't know of any advantages in using add_library and set_target_properties. I had found both approaches. I've changed it to use include_directories and target_link_libraries:

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

# 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 executable
add_executable("ResSim"	main.cc)

# Include directories
include_directories("${thermolib_INCLUDE_DIRS}")

# Link libraries
target_link_dune_default_libraries("ResSim")
target_link_libraries("ResSim" "${thermolib_LIBRARY}")

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

The problem remains. "make VERBOSE=1" returns

cd /home/tobk/Documents/Projects/ResSim/release/src && /usr/bin/cmake -E cmake_link_script CMakeFiles/ResSim.dir/link.txt --verbose=1
/usr/bin/g++   -Wall -std=c++14  -O3 -DNDEBUG   -Wl,-no-as-needed -pthread  -Wl,-no-as-needed -pthread CMakeFiles/ResSim.dir/main.cc.o  -o ResSim  -L/home/tobk/Documents/Projects/ThermoLib/lib -rdynamic /usr/local/lib/libdunegrid.a /usr/local/lib/libdunegeometry.a /usr/local/lib/libdunecommon.a -lldl -lspqr -lumfpack -lcholmod -lamd -lcamd -lcolamd -lccolamd -lsuitesparseconfig -lsuperlu -lblas -llapack -lblas -pthread /usr/lib/openmpi/lib/libmpi.so /usr/local/lib/libdunegrid.a /usr/local/lib/libdunegeometry.a /usr/local/lib/libdunecommon.a -lThermoLib -llapack -lblas -Wl,-rpath,/usr/lib/openmpi/lib:/home/tobk/Documents/Projects/ThermoLib/lib 
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

I can't detect the problem with the above. It has "-L/home/tobk/Documents/Projects/ThermoLib/lib" and "-lThermoLib".


-----Original Message-----
From: dune-bounces+tobk=dtu.dk at dune-project.org [mailto:dune-bounces+tobk=dtu.dk at dune-project.org] On Behalf Of Markus Blatt
Sent: 17. maj 2017 20:25
To: dune at dune-project.org
Subject: Re: [Dune] Link to external shared library

Hi,

On Wed, May 17, 2017 at 02:22:05PM +0000, Tobias Kasper Skovborg Ritschel wrote:
> 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?
> 

No there should be no need for this.

> The longer explanation:
> I am working on a DUNE module (ResSim),

Sounds like a reservoir simulator. 

> 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}")

 I have never used the above. What does it buy you? 
You could skip it and use target_link_libraries("ResSim" ${thermolib_LIBRARY}) below with apropriate include_directories(${thermolib_INCLUDE_DIRS]), couldn't you?
> 
> # Add executable
> add_executable("ResSim" main.cc)
> 
> # Link libraries
> target_link_dune_default_libraries("ResSim")
> target_link_libraries("ResSim" thermolib)
> 

> 
> 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
> 

Maybe the linker line gives some insight? With make it shown when using "make VERBOS=1"

Markus

--
Dr. Markus Blatt - HPC-Simulation-Software & Services http://www.dr-blatt.de Hans-Bunte-Str. 8-10, 69123 Heidelberg, Germany
Tel.: +49 (0) 160 97590858

_______________________________________________
Dune mailing list
Dune at dune-project.org
http://lists.dune-project.org/mailman/listinfo/dune




More information about the Dune mailing list