[Dune] Link to external shared library
Tobias Kasper Skovborg Ritschel
tobk at dtu.dk
Thu May 18 12:42:39 CEST 2017
That did the trick. Thanks a lot for the help, especially since it turned out not to be Dune related.
-----Original Message-----
From: Jö Fahlke [mailto:jorrit at jorrit.de]
Sent: 18. maj 2017 10:02
To: Tobias Kasper Skovborg Ritschel
Cc: dune at dune-project.org
Subject: Re: [Dune] Link to external shared library
Am Thu, 18. May 2017, 06:55:03 +0000 schrieb Tobias Kasper Skovborg Ritschel:
> Date: Thu, 18 May 2017 06:55:03 +0000
> From: Tobias Kasper Skovborg Ritschel <tobk at dtu.dk>
> To: "dune at dune-project.org" <dune at dune-project.org>
> Subject: Re: [Dune] Link to external shared library
> X-No-Auth: unauthenticated sender
> Thread-Topic: [Dune] Link to external shared library
> Thread-Index: AdLPFmhrcPB40s68QbeX6HeVfJ7csgAE7QEAAB0uGsA=
> Accept-Language: en-US, da-DK
> Content-Language: en-US
> X-MS-Has-Attach:
> X-Envelope-From: <tobk at dtu.dk>
> X-OfflineIMAP: 4291489864-9125989685
>
> 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".
The linker also found libThermoLib.so, otherwise it would have complained about missing that before complaining about undefined references.
I can offer the following lead:
- Is the linkage of LoadParams correct?
If ThermoLib is a C library, LoadParams will be put in the lib using C
linkage. If you include the thermolib header from a C++ program, all
function declarations will declare functions in C++ linkage (i.e. with name
mangling) by default. Try
```
extern C {
#include <TermoLib.h>
}
```
instead of just a plain include. Really, TermoLib.h should probably wrap
all its declarations when it detects a C++ compiler, i.e. from openmpi's
[mpi.h](https://github.com/open-mpi/ompi/blob/master/ompi/include/mpi.h.in#L318):
```
//...
#if defined(c_plusplus) || defined(__cplusplus)
extern "C" {
#endif
// declarations here
#if defined(c_plusplus) || defined(__cplusplus)
}
#endif
//...
```
Regards, Jö.
--
More information about the Dune
mailing list