天天看點

Linux環境下,JAVA環境調用C++的動态連結庫so

1、使用QT開發一個動态連結庫.so(本次使用linux環境,在windows下是.dll)

代碼檔案包含

testdll.cpp

testdll.h

testdll.pro

testdll_global.h

Linux環境下,JAVA環境調用C++的動态連結庫so

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

           

生成動态庫

Linux環境下,JAVA環境調用C++的動态連結庫so

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();
}

           

運作結果

Linux環境下,JAVA環境調用C++的動态連結庫so

3、使用JAVA調用動态連結庫so

使用jna.jar包作為映射包,調用動态連結庫
           

代碼目錄結構

Linux環境下,JAVA環境調用C++的動态連結庫so

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!");
    }

}
           

運作結果

Linux環境下,JAVA環境調用C++的動态連結庫so

4、至此,全部完工!