且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Linux qtcreator编程实现动态加载动态链接库

更新时间:2021-10-03 07:06:58

完整的工程源码可以访问下载:http://download.csdn.net/download/libaineu2004/9896700

一、主程序main.cpp


#include <iostream>
#include <dlfcn.h>
 
using namespace std;
typedef int (*pStrLenFun)(char *str);
typedef char *(*pStrCopyFun)(char *desc, char *src);
 
int main(int argc, char *argv[])
{
    char src[]="Hello Dymatic";
    char desc[80];
 
    pStrLenFun fun1;
    pStrCopyFun fun2;
 
    void *phandle = NULL;
    char *perr = NULL;
    phandle = dlopen("libslstrlen.so", RTLD_LAZY);//RTLD_NOW
    if(!phandle)
    {
        printf("Failed Load library!\n");
    }
 
    perr = dlerror();
    if(perr != NULL)
    {
        printf("%s\n",perr);
        return 0;
    }
 
    fun1 = (pStrLenFun)dlsym(phandle, "StrLen");
    perr = dlerror();
    if(perr != NULL)
    {
        printf("%s\n",perr);
        return 0;
    }
 
    fun2 = (pStrCopyFun)dlsym(phandle, "StrCopy");
    perr = dlerror();
    if(perr != NULL)
    {
        printf("%s\n",perr);
        return 0;
    }
 
    printf("The string is: %s\n",src);
    printf("the string length is: %d\n",fun1(src));
    printf("the string copyed:%s\n",fun2(desc,src));
 
    return 0;
}

————————————————
版权声明:本文为CSDN博主「libaineu2004」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/libaineu2004/article/details/75035946

相应的go.pro文件


TEMPLATE = app
CONFIG += console c++11
CONFIG -= app_bundle
CONFIG -= qt
 
DESTDIR = ../bin
 
QMAKE_LFLAGS += -ldl #编译选项,否则会失败。comile option,dlopen()
 
#如果程序员想在QtCretor直接运行并动态加载动态库的话,则需要添加好该库,否则会报错。
#Failed Load library!
#libslstrlen.so: cannot open shared object file: No such file or directory
LIBS += -L../bin/ -lslstrlen
 
SOURCES += main.cpp

二、动态库

源文件slstrlen.cpp

#include "slstrlen.h"
 
#define ENDSTRING '\0'
int StrLen(char *string)
{
  int len = 0;
  while(*string++ != ENDSTRING)
    len++;
  return len;
}
 
char *StrCopy(char *desc, char *src)
{
    return 0;
}

头文件slstrlen.h

#ifndef SLSTRLEN_H
#define SLSTRLEN_H
 
#include "slstrlen_global.h"
 
//如果动态库使用g++编译,那么动态库定义函数的时候加上extern "C",否则会提示undefined symbol错误。
extern "C" int SLSTRLENSHARED_EXPORT StrLen(char *string);
extern "C" char * SLSTRLENSHARED_EXPORT StrCopy(char *desc, char *src);
 
#endif // SLSTRLEN_H

头文件slstrlen_global.h

#ifndef SLSTRLEN_GLOBAL_H
#define SLSTRLEN_GLOBAL_H
 
//#include <QtCore/qglobal.h>
 
#define Q_DECL_EXPORT     __attribute__((visibility("default")))
#define Q_DECL_IMPORT     __attribute__((visibility("default")))
#define Q_DECL_HIDDEN     __attribute__((visibility("hidden")))
 
#if defined(SLSTRLEN_LIBRARY)
#  define SLSTRLENSHARED_EXPORT Q_DECL_EXPORT
#else
#  define SLSTRLENSHARED_EXPORT Q_DECL_IMPORT
#endif
 
#endif // SLSTRLEN_GLOBAL_H

slstrlen.pro文件

#-------------------------------------------------
#
# Project created by QtCreator 2017-07-12T14:58:54
#
#-------------------------------------------------
 
QT       -= core gui
 
TARGET = slstrlen
TEMPLATE = lib
DESTDIR = ../bin
 
DEFINES += SLSTRLEN_LIBRARY
 
# The following define makes your compiler emit warnings if you use
# any feature of Qt which as 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 += slstrlen.cpp
 
HEADERS += slstrlen.h\
        slstrlen_global.h
 
unix {
    target.path = /usr/lib
    INSTALLS += target