How to correctly include and link a proprietary shared object library in C++

752 Views Asked by At

I have a linux c++ sdk that i need to integrate as a dependency into a .NET 6.0 bigger project.

This sdk is only conditionally included for the linux version of the software, i do already have the windows version running with no problems.

This skd exposes only classes in very nested namespaces, and as far as i'm aware, [DllImport] does not support class methods.

I do already know how to create a wrapper and expose functions with extern "C" and passing the sdk instances as pointers to a .NET Core application running with no problem under linux systems

My problem is that i do have no idea on how to succesfully compile/include/link the existing .so files into a .dll or .so that i can then [DllImport]

The sdk has been sent to me with the following structure (take name-of-lib as the placeholder name. I'm not sure if i can expose the real company name so i won't just in case):

run/
|- x86/
|  +- {same files as x64 but i don't target x86 arcs so it's useless}
+- x64/
   |- install.sh {see #1}
   |- libNameModuleA.so.1.2.3
   |- libNameModuleB.so.3.2.1
   |- libNameApi.so.7.8.9
   +- {etc. etc.}
lib/
|- lib-name-api/
|  |- doc/
|  |  +- somehtmlautogenerateddocs.html
|  |- include/
|  |  +- NameApi.h
|  +- source/    {optionally for the top level apis}
|     +- {varius header .h files}
|- name-of-lib-module-A/
|  +- {same as previus, repeat for every .so file}
+- {etc. etc.}

{#1} script that craetes links as libName.so => libName.so.1 => libName.so.1.2 => etc

All of those library header files reference each other like they were in the same folder, using an #include "subLibraryModule.h" even if this file would actually be in something like ../../subLibrary/include/subLibraryModule.h.

Also, in the skd there are only headers.

now, what would be the (either, i don't really know waht would be the best tool) cmake, make, g++, gcc command, configuration or procedure to be able to include the top-level header that references all of the other ones in the compilation without a file not found error from the compiler?

Say i need to compile a single file wrapper.cpp that includes only the top level headers and contains only extern "C" functions

I've tried doing this with vscode and intellisense actually resolves the headers correctly, but when i try to compile it everything bursts into a fire of file-not-founds

Am I missing something? Is there further information or files i missed?

Thanks in advance.

1

There are 1 best solutions below

0
On BEST ANSWER

So i figured it out.

makefile

The only problem here is that i did not know how to use a glob pattern to include stuff automatically (like -Ilib/*/include) but whatever.

CC := g++
SRC := wrapper.cpp
OBJ := wrapper.o
OUT := libWrapper.so
INCs := -Ilib/lib-name-api/include -Ilib/name-lib-module-A/include {etc. etc.}
LIB := -L/usr/local/lib/installedLibNameSos/ -lNameModuleA -lNameModuleB {etc. etc.}

all: $(OUT)

$(OUT): $(OBJ)
    $(CC) $(OBJ) -shared -o $(OUT) $(LIB)

$(OBJ): $(SRC)
    $(CC) -c $(SRC) -o $(OBJ) $(INCs) $(LIB)

wrapper.cpp

#include <stdio.h>
#include "NameApi.h"
// varius using namespace and other import stuff


extern "C" int test (){
    return 42;
}

extern "C" string somethingReturningAString (){
    // calls and stuff to the native library
    return "Hi mom"
}

obviusly the .so needs to be copied where the executable is

csharpClass.cs

using System;
using System.Runtime.InteropServices;

// extension not needed.
// the library will be loaded along with the required .so's
// that have been linked from /usr/local/lib/installedLibNameSos/
[DllImport("libWrapper")]
extern static int test ();

[DllImport("libWrapper")]
extern static string somethingReturningAString ();


Console.WriteLine(test());
// 42
Console.WriteLine(somethingReturningAString());
// Hi mom

resources:

(note: the process works perfectly. But i've been using a library written and documented by a spastic monkey. So yeah i've literally learned the basics of g++, make and got crippling depression for nothing. lol)