gpt4 book ai didi

c# - 在 Linux 和 Windows 中与 c# 集成的跨平台 c++

转载 作者:太空宇宙 更新时间:2023-11-04 12:33:16 25 4
gpt4 key购买 nike

我在 c++ 中有以下代码来确定 os 中的可用 RAM

#if defined(_WIN32)
#include <stdio.h>
#include <windows.h>
#include "string.h"
#include "setjmp.h"
#elif defined(__linux__)
#include "stdio.h"
#include "string.h"
#include <unistd.h>
#endif

extern "C"
{
unsigned long long getAvailableSystemMemory_Windows64();
unsigned long long getAvailableSystemMemory_Linux64();
}

#if defined(_WIN32)

__declspec(dllexport) extern unsigned long long getAvailableSystemMemory_Windows64()
{

MEMORYSTATUSEX status;
status.dwLength = sizeof(status);
GlobalMemoryStatusEx(&status);
return status.ullAvailPhys / 1024 / 1024;
}

#elif defined(__linux__)

extern unsigned long long getAvailableSystemMemory_Linux64()
{

unsigned long long ps = sysconf(_SC_PAGESIZE);
unsigned long long pn = sysconf(_SC_AVPHYS_PAGES);
unsigned long long availMem = ps * pn;
return availMem / 1024 / 1024;
}

#endif

int main()
{
#if defined(_WIN32)
printf("%d", getAvailableSystemMemory_Windows64());
#elif defined(__linux__)
printf("%d", getAvailableSystemMemory_Linux64());

#endif
printf("MB");

int a;
scanf("This is the value %d", &a);
}

和下面的c#代码

class Program
{

[DllImport("hello.dll", CallingConvention = CallingConvention.Cdecl)]
extern static long getAvailableSystemMemory_Windows64();


[DllImport("hello.so", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl, EntryPoint = "getAvailableSystemMemory_Linux64")]
extern static long getAvailableSystemMemory_Linux64();

static void Main(string[] args)
{
long text = 0;
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
text = getAvailableSystemMemory_Windows64();
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
text = getAvailableSystemMemory_Linux64();


Console.WriteLine(text);
Console.WriteLine("Hello World!");
Console.ReadLine();
}

然后在 Windows 中,我使用 g++ --shared -o hello.dll hello.cpp 编译 C++ 代码,并将 dll 复制到调试文件夹。一切正常。

对于 Linux,我使用 g++ -o hello.so hello.cpp 在 Opensuse 上编译它并将 .so 文件复制到调试中,但它不起作用。我得到异常

Unhandled Exception: System.DllNotFoundException: Unable to load shared 
library 'hello.so' or one of its dependencies. In order to help diagnose
loading problems, consider setting the LD_DEBUG environment variable:
libhello.so.so: cannot dynamically load executable
at CallCDll.Program.getAvailableSystemMemory_Linux64()
at CallCDll.Program.Main(String[] args) in
/home/CallCDll/Program.cs:line 22

我使用 LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/CallCDll/bin/Debug/netcoreapp2.2.so 文件目录路径添加到 LD_LIBRARY_PATH 但不起作用.

我该怎么办?无论我在哪里复制 .so 文件,它都找不到它。

最佳答案

根据 [Man7]: GCC(1) (man gcc):

-shared

Produce a shared object which can then be linked with other objects to form an executable. Not all systems support this option. For predictable results, you must also specify the same set of options used for compilation (-fpic, -fPIC, or model suboptions) when you specify this linker option.[1]

因此,您的命令没有生成共享对象或库 (.so),而是生成可执行文件(即使名为 hello.so)。如果您尝试运行它,您将看到 main 的输出。
要解决问题,请将构建命令更改为:

g++ -shared -fPIC -o hello.so hello.cpp

其他注意事项:

关于c# - 在 Linux 和 Windows 中与 c# 集成的跨平台 c++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57756328/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com