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、调测结果

相关推荐
进击的女IT3 分钟前
SpringBoot上传图片实现本地存储以及实现直接上传阿里云OSS
java·spring boot·后端
Miqiuha10 分钟前
lock_guard和unique_lock学习总结
java·数据库·学习
一律清风1 小时前
QT-文件创建时间修改器
c++·qt
一 乐1 小时前
学籍管理平台|在线学籍管理平台系统|基于Springboot+VUE的在线学籍管理平台系统设计与实现(源码+数据库+文档)
java·数据库·vue.js·spring boot·后端·学习
数云界2 小时前
如何在 DAX 中计算多个周期的移动平均线
java·服务器·前端
风清扬_jd2 小时前
Chromium 如何定义一个chrome.settingsPrivate接口给前端调用c++
前端·c++·chrome
阑梦清川2 小时前
Java继承、final/protected说明、super/this辨析
java·开发语言
Death2002 小时前
Qt 6 相比 Qt 5 的主要提升与更新
开发语言·c++·qt·交互·数据可视化
快乐就好ya3 小时前
Java多线程
java·开发语言
IT学长编程3 小时前
计算机毕业设计 二手图书交易系统的设计与实现 Java实战项目 附源码+文档+视频讲解
java·spring boot·毕业设计·课程设计·毕业论文·计算机毕业设计选题·二手图书交易系统