Linux에서 Qt 간단한 사용, 파일 선택, 동적 링크 라이브러리 호출

28567 단어 과정 설계/실천
1. Linux에서 Qt를 간단하게 사용하면 간단한 클릭 단추를 만들어서 텍스트 상자에 Hello World가 나타나는 것을 예로 삼아 참고 링크를 만들 수 있다.https://www.cnblogs.com/letben/p/5205060.html이 링크는 다른 사람의 블로그를 옮겨 싣고 노트로만 사용하며 삭제합니다.
2. Qt내 편집mainwindow.cpp는 파일을 선택하고 동적 링크 라이브러리를 호출합니다. 우선 호출할 동적 링크 라이브러리를 만듭니다.안의 내용은 선생님이 요구하는 출력 Hello World 함수, 암호화 함수와 복호화 함수입니다.
//func.c
# include 

void HELLO_WORLD()
{
    printf("hello world......
"
); } int Crypt_Enc_Block(unsigned char *input, int in_len, unsigned char *output, int *out_len, unsigned char *key, int keylen)// { int len=0, i=0; for (int t_keylen=0; i<in_len; i++, len++){ output[i]=input[i] ^ key[t_keylen]; t_keylen++; if (t_keylen>=keylen){ t_keylen=0; } } output[i]='\0'; *out_len=len; if (*out_len==in_len) return 0; else return -1; } int Crypt_Dec_Block(unsigned char *input, int in_len, unsigned char *output, int *out_len, unsigned char *key, int keylen)// { int len=0, i=0; for (int t_keylen=0; i<in_len; i++, len++){ output[i]=input[i] ^ key[t_keylen]; t_keylen++; if (t_keylen>=keylen){ t_keylen=0; } } output[i]='\0';// , bug *out_len=len; if (*out_len==in_len) return 0; else return -1; }

그 다음에 위의 절차에 따라 항목을 만들면 단추 하나만 있으면 된다.이 단추의 설정 클릭 () 함수 슬롯에 코드를 추가하면 아래로 변합니다.
//mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"

#include 
#include 
#include 
#include 

#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;


clock_t t_start , t_end;

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_pushButton_clicked()//   button    
{
    QString fileName;
    fileName = QFileDialog::getOpenFileName(this , tr("  "),"" , tr("(*.so)"));
    //this     ,tr   ,""            ,        .so  
    string str = fileName.toStdString();
    const char *p = str.c_str();
    if(!fileName.isNull())
    {
		//       .so   ,       main.cpp             
        void *handle = dlopen(p , RTLD_LAZY);
        if(!handle)
        {
            cout<<"open lib error
"
; cout<<dlerror()<<endl; return; } typedef void(*f_t)(); f_t f = (f_t)dlsym(handle , "HELLO_WORLD"); if(!f) { cout<<dlerror()<<endl; dlclose(handle); return; } // HELLO_WORLD , t_start = clock(); for(int i=1; i<=5 ; i++) { f(); } t_end = clock(); double total = (double)(t_end-t_start)/CLOCKS_PER_SEC; cout<<"total time:"<<total*1000<<"ms"<<endl; char ming_wen[32]={"hello world"}; char mi_wen[32]; int len_mingwen=11; int len_miwen; char mykey[32]={"123"}; int len_mykey=3; char res[32]; int len_res; // Crypt_Enc_Block typedef int(*Enc)(unsigned char *input, int in_len, unsigned char *output, int *out_len, unsigned char *key, int keylen); Enc f1 = (Enc)dlsym(handle, "Crypt_Enc_Block"); if (!f1){ cout<<dlerror()<<endl; dlclose(handle); return; } f1((unsigned char*)ming_wen, len_mingwen, (unsigned char*)mi_wen, &len_miwen, (unsigned char*)mykey, len_mykey); // Crypt_Dec_Block //typedef int(*Enc)(unsigned char *input, int in_len, unsigned char *output, int *out_len, unsigned char *key, int keylen); Enc f2 = (Enc)dlsym(handle, "Crypt_Dec_Block"); if (!f2){ cout<<dlerror()<<endl; dlclose(handle); return; } f2((unsigned char*)mi_wen, len_miwen, (unsigned char*)res, &len_res, (unsigned char*)mykey, len_mykey); cout<<ming_wen<<endl; cout<<mi_wen<<endl; cout<<res<<endl; if (strcmp(ming_wen, res)==0) cout<<"success"<<endl; else cout<<"failed"<<endl; dlclose(handle); } }

오류 발생 가능성: undefined reference to symbol'dlsym@@GLIBC2.2.5'해결 방법: 프로 파일의 맨 뒤에dl 라이브러리 추가: LIBS+=-ldl

좋은 웹페이지 즐겨찾기