Linux环境下,JAVA环境调用C++的动态链接库so

1、使用QT开发一个动态链接库.so(本次使用linux环境,在windows下是.dll)
代码文件包含
testdll.cpp
testdll.h
testdll.pro
testdll_global.h
在这里插入图片描述
testdll.pro

#-------------------------------------------------
#
# Project created by QtCreator 2023-01-09T14:01:03
#
#-------------------------------------------------

QT       -= gui

TARGET = testdll
TEMPLATE = lib

DEFINES += TESTDLL_LIBRARY

# The following define makes your compiler emit warnings if you use
# any feature of Qt which has been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

SOURCES += \
        testdll.cpp

HEADERS += \
        testdll.h \
        testdll_global.h 

unix {
    target.path = /usr/lib
    INSTALLS += target
}

testdll_global.h

#ifndef TESTDLL_GLOBAL_H
#define TESTDLL_GLOBAL_H

#include <QtCore/qglobal.h>

#if defined(TESTDLL_LIBRARY)
#  define TESTDLLSHARED_EXPORT Q_DECL_EXPORT
#else
#  define TESTDLLSHARED_EXPORT Q_DECL_IMPORT
#endif

#endif // TESTDLL_GLOBAL_H

testdll.h

#ifndef TESTDLL_H
#define TESTDLL_H

#include "testdll_global.h"

class TESTDLLSHARED_EXPORT Testdll
{

public:
    Testdll();
    //void PrintTestInfo();
};

extern "C" TESTDLLSHARED_EXPORT void helloWorld();
extern "C" TESTDLLSHARED_EXPORT int add(int a, int b);

#endif // TESTDLL_H

testdll.cpp

#include "testdll.h"
#include <iostream>

using namespace std;

Testdll::Testdll()
{
}

void helloWorld()
{
    cout << "Hello World!!!!!" << endl;
}

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

生成动态库
在这里插入图片描述
2、使用C++调用动态链接库so

#include <QCoreApplication>

#include <iostream>
#include <cstring>
#include <QLibrary>

using namespace std;

typedef int (*Fun)(int, int);

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    
    QLibrary mylib("/home/kylin/桌面/qttest/build-testdll-Desktop_Qt_5_9_6_GCC_64bit-Debug/libtestdll.so");
    int result;
    if (mylib.load()) {
        cout << "so load is ok!" << endl;
        Fun add = (Fun)mylib.resolve("add");
        if (add)
        {
            cout << "Link to add Function is ok!" << endl;
            result = add(6, 7);
            cout << result << endl;

        } else {
            cout << "Link to add Function is failed!" << endl;
        }
    } else {
        cout << "so is not loaded!" << endl;
    }

    return a.exec();
}

运行结果
在这里插入图片描述
3、使用JAVA调用动态链接库so

使用jna.jar包作为映射包,调用动态链接库

代码目录结构
在这里插入图片描述
TestDLL1Service.java

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

public class TestDLL1Service {

    public interface TestDLL1 extends Library {
        TestDLL1 INSTANCE = (TestDLL1) Native.loadLibrary(
                "/home/kylin/桌面/qttest/build-testdll-Desktop_Qt_5_9_6_GCC_64bit-Debug/libtestdll.so",
                TestDLL1.class);
        public int add(int a, int b);
    }

    public static void main(String[] args) {
        //TestDLL1.INSTANCE.add(6, 7);
        System.out.println(TestDLL1.INSTANCE.add(7, 8));
        //System.out.println("Hello world!");
    }

}

运行结果
在这里插入图片描述
4、至此,全部完工!

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值