linux qt 添加动态链接库_Linux qtcreator编程实现动态加载动态链接库

本文介绍了如何在Linux环境下使用Qt Creator进行动态加载动态链接库的编程。通过实例展示了在main.cpp中使用dlopen和dlsym函数加载libslstrlen.so库,并调用库中的StrLen和StrCopy函数。同时,提供了相关的.pro文件配置和动态库的源码示例。
摘要由CSDN通过智能技术生成

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

一、主程序main.cpp

#include

#include

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;

}

相应的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

#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

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值