C++生成动态库,C++和C#以及Java在windows和linux调用

Windows生成dllC++库

1、创建动态链接库项目

源文件编写函数

cpp 复制代码
// dllmain.cpp : 定义 DLL 应用程序的入口点。
#include "pch.h"

int sum(int a, int b) {
    return a + b;
}

BOOL APIENTRY DllMain( HMODULE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
                     )
{
    switch (ul_reason_for_call)
    {
    case DLL_PROCESS_ATTACH:
    case DLL_THREAD_ATTACH:
    case DLL_THREAD_DETACH:
    case DLL_PROCESS_DETACH:
        break;
    }
    return TRUE;
}

头文件编写如下

cpp 复制代码
// pch.h: 这是预编译标头文件。
// 下方列出的文件仅编译一次,提高了将来生成的生成性能。
// 这还将影响 IntelliSense 性能,包括代码完成和许多代码浏览功能。
// 但是,如果此处列出的文件中的任何一个在生成之间有更新,它们全部都将被重新编译。
// 请勿在此处添加要频繁更新的文件,这将使得性能优势无效。

#ifndef PCH_H
#define PCH_H

// 添加要在此处预编译的标头
#include "framework.h"

#endif //PCH_H

#define DLLAPI extern "C" _declspec(dllexport)
DLLAPI int sum(int a, int b);

Release生成dll

Linux生成C++.so文件

1、创建项目

2、编写方法生成so文件

cpp 复制代码
extern "C" {
	int sumSo(int a, int b) {
		return a + b;
	}
}

C++项目调用

1、创建空项目,源文件编写如下

cpp 复制代码
#include <iostream>
#include "windows.h"

typedef int (*addfunction)(int ,int);
HMODULE dll = LoadLibrary(L"wuzhidll.dll");

int main() {
	if (dll) {
		std::cout << "DLL Ok\n";
		addfunction sum_ = (addfunction)GetProcAddress(dll, "sum");
		if (sum_ != NULL) {
			std::cout << "HOOK set up success";
			std::cout << "调用sum函数" << sum_(300, 500) << std::endl;
		}
		else { std::cout << "HOOK is no"; }
	}
	else
	{
		std::cout << "DLL No\n";
	}
	return 0;
}

将dll拷贝在项目中,调试生成

C#调用dll

1、创建调用类

cs 复制代码
  internal static class SumInfo
    {
        public const string DLL = "wuzhidll.dll";

        public const string So = "libConsoleSo.so";

        [DllImport(DLL)]
        public static extern int sum(int a,int b);


        [DllImport(So)]
        public static extern int sumSo(int a, int b);
    }

2、调试调用

cpp 复制代码
// See https://aka.ms/new-console-template for more information
using ConsoleApp1;
int a = 24;
int b = 2000;
int c = SumInfo.sum(a, b);
Console.WriteLine(c);
Console.ReadLine();

3、调测

4、linux调试

Java调用C++dll和So

1、创建控制台项目、编写代码

java 复制代码
package org.example;

import com.sun.jna.Native;

public class App 
{
    public static void main( String[] args )
    {
        String os = System.getProperty("os.name");
        int sum=0;
        //判断操作系统
        if (os != null && os.toLowerCase().startsWith("windows")) {
            sum =  MyLibrary.INSTANCE.sum(5, 6);
        } else if (os != null && os.toLowerCase().startsWith("linux")) {
            sum =  MyLibraeySo.INSTANCESo.sum(5, 6);
        } else { //其它操作系统
            System.out.println(String.format("当前系统版本是:%s", os));
        }
        System.out.println( "调用C++函数获取求和结果:"+sum );
    }
}
java 复制代码
package org.example;

import com.sun.jna.Library;
import com.sun.jna.Native;


public interface MyLibrary extends Library {

    String currentPath = System.getProperty("user.dir")+"\\wuzhidll.dll";

    MyLibrary INSTANCE = (MyLibrary) Native.loadLibrary(currentPath,MyLibrary.class);

    int sum(int a, int b);
}
java 复制代码
package org.example;

import com.sun.jna.Native;

public interface MyLibraeySo {
    String currentPathSo = System.getProperty("user.dir")+"\\libConsoleSo.so";

    MyLibrary INSTANCESo = (MyLibrary) Native.loadLibrary(currentPathSo,MyLibrary.class);

    int sum(int a, int b);
}

注意pom文件添加jna的引用

XML 复制代码
 <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>com.sun.jna</groupId>
      <artifactId>jna</artifactId>
      <version>3.0.9</version>
    </dependency>
  </dependencies>

2、调测结果

相关推荐
黄焖鸡能干四碗9 分钟前
智慧教育,智慧校园,智慧安防学校建设解决方案(PPT+WORD)
java·大数据·开发语言·数据库·人工智能
神里流~霜灭16 分钟前
(C++)数据结构初阶(顺序表的实现)
linux·c语言·数据结构·c++·算法·顺序表·单链表
lssjzmn17 分钟前
Spring Web 异步响应实战:从 CompletableFuture 到 ResponseBodyEmitter 的全链路优化
java·前端·后端·springboot·异步·接口优化
John_ToDebug25 分钟前
从源码视角全面解析 Chrome UI 布局系统及 Views 框架的定制化实现方法与实践经验
c++·chrome·架构
new_daimond31 分钟前
二级缓存在实际项目中的应用
java
one99633 分钟前
WPF应用程序中的异常处理
c#·.net·wpf
一只乔哇噻38 分钟前
java后端工程师进修ing(研一版 || day41)
java·开发语言·学习·算法
愚润求学40 分钟前
【贪心算法】day7
c++·算法·leetcode·贪心算法
深耕AI1 小时前
【MFC简介:从基础概念到实际应用】
c++·mfc
User_芊芊君子1 小时前
【Java】设计模式——单例、工厂、代理模式
java·设计模式·代理模式