如何在 C++ 中正确包含和链接专有共享对象库

发布于 2025-01-20 12:51:15 字数 1720 浏览 0 评论 0 原文

我有一个Linux C ++ SDK,我需要将其作为依赖项集成到.NET 6.0较大的项目中。

该SDK仅在软件的Linux版本中 ,我确实已经运行了Windows版本,没有问题。

此SKD仅在非常嵌套的名称空间中公开类,据我所知, [dllimport] 不支持类方法。

我确实已经知道如何创建包装器和 extern“ c” 公开函数,并将SDK实例传递给指针到Linux Systems在Linux Systems毫无问题的.NET核心应用程序中,

我的问题是我确实做到了不知道如何成功地编译/include/link现有 .so 文件到.dll或..这样我就可以 [dllimport]

SDK已发送了对我来说,具有以下结构(以lib的名称为占位符名称。我不确定我是否可以公开真实的公司名称,以防万一,以防万一):

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}脚本craetes链接为libname.so => libname.so.1 => libname.so.1.2 =>等

所有这些库标头文件都像在同一文件夹中一样互相引用,使用 #include“ sublibrarymodule.h” ,即使此文件实际上将在 ../ ../ sublibrary/include/sublibrarymodule.h

另外,在SKD中只有标题。

现在,是什么(我真的不知道Waht是最好的工具)CMake,Make,G ++,GCC命令,配置或过程,可以包括引用所有其他所有内容的顶级标头编译中未找到编译器中的错误?

假设我需要编译一个文件 wrapper.cpp ,该文件仅包含顶级标头,仅包含 extern“ c” 函数,

我尝试使用vscode和intelliSense进行此操作。实际上可以正确解析标头,但是当我尝试编译时,一切都爆发为未创建的文件,

我错过了一些东西吗?我错过了更多信息或文件吗?

提前致谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

话少心凉 2025-01-27 12:51:15

所以我发现了。

MAKEFILE

这里唯一的问题是我不知道如何使用Glob模式自动包含内容(例如 -ilib/*/include ),但无论如何。

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

下复制

casv casvius over.so需要在可执行文件为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

(注意:该过程非常有效。但是我一直在使用痉挛性猴子编写和记录的库。是的,我从字面上学到了G ++的基础知识,使抑郁症无所适从。

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文