2

I'm trying to utilize a C++ library in Modelica. Modelica compilers generate c from Modelica source and then invokes a c compiler to create an executable. Modelica provides a mechanism to call c functions. I've created a wrapper:

//myWrapper.cpp
#include "headers_of_cpp_library.h"

extern "C" double call_a_cpp_fn(double a, double b){
    return cpp_fn(a,b);
}

External C code is either linked dynamically to the Modelica executable or if the code is pure c, it can be compiled right with the generated c. The only option I have here is to go the dynamic link route. Here's the Makefile for building the C++ library

all: myWrapper.cpp
        clang++ -fPIC -shared -LaCppLib -o libmywrappedcpp.so myWrapper.cpp

When I run the Modelica compiler I get some link errors that due to the fact that there are c++ libraries that need to be dynamically linked.

undefined reference to symbol '__cxa_free_exception@@CXXABI_1.3'

Is there a way to build libmywrappedcpp.so so that the c compiler invoked by the Modelica system won't have to try to link c++?

Adam
  • 67
  • 1
  • 8
  • Try disabling exceptions and runtime information. If you don't use any special stuff from the STL, this might solve your problem. In GCC: `-fno-rtti -fno-exceptions`. – Jorge Bellon Nov 18 '16 at 23:07
  • @Jorge Bellón I tried it. First I added the flags to the makefile for libmywrapper.so. No luck, same error. I also tried passing the flags to the Modelica compiler. No luck there either. Might have found a new clue though... I was passing the flag -laCppLib to the compiler invoked by Modelica. That shouldn't have to be there should it? I removed that flag and the c compiler couldn't find a whole bunch of symbols defined in libaCppLib. Seems like those should be getting linked with libmywrapper.so... Think that could have something to do with it? – Adam Nov 18 '16 at 23:51
  • Can you check if the compiler is linked with a library that is ABI compatible with libmywrapper.so? Some compiler updates my break with a previous ABI and you have to recompile everything or stick with the old version. – Jorge Bellon Nov 18 '16 at 23:56
  • @JorgeBellón, sure. I'm not exactly sure how to tell if what's ABI compatable, I'm going to research though. I'd appreciate any hints there. I'm using clang to build libmywrapper.so. libaCppLib is built using gcc and the c compiler invoked by the Modelica build system is clang. – Adam Nov 19 '16 at 00:15
  • The easy way is to make sure everything was compiled using exactly the same compiler and libraries. This is tedious but straightforward. Take into account that clang++ may use libc++ whereas gcc comes with libstdc++ (they are different implementations). – Jorge Bellon Nov 19 '16 at 00:39
  • Thanks. I recompiled libaCppLib with clang. Still the same error. I have libaCppLib.a and libaCppLib_shared.so. If I run ldd on libaCppLib_shared.so it looks like there's a lot of things to track down. linux-vdso.so.1 => (0x00007ffd2329a000) libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007fab2ffcf000) ... and more .... I guess even in libaCppLib.a there's gnu stuff, so there's no easy shortcuts. – Adam Nov 19 '16 at 00:47

0 Answers0