Linux qtcreator编程实现动态加载动态链接库
来源:互联网 发布:js字符串格式化0在前面 编辑:程序博客网 时间:2024/05/21 15:44
完整的工程源码可以访问下载: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;}
相应的go.pro文件
TEMPLATE = appCONFIG += console c++11CONFIG -= app_bundleCONFIG -= qtDESTDIR = ../binQMAKE_LFLAGS += -ldl #编译选项,否则会失败。comile option,dlopen()#如果程序员想在QtCretor直接运行并动态加载动态库的话,则需要添加好该库,否则会报错。#Failed Load library!#libslstrlen.so: cannot open shared object file: No such file or directoryLIBS += -L../bin/ -lslstrlenSOURCES += 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 guiTARGET = slstrlenTEMPLATE = libDESTDIR = ../binDEFINES += 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.0SOURCES += slstrlen.cppHEADERS += slstrlen.h\ slstrlen_global.hunix { target.path = /usr/lib INSTALLS += target}
阅读全文
0 0
- Linux qtcreator编程实现动态加载动态链接库
- linux 动态链接库加载
- linux 动态链接库加载
- linux 动态加载 动态链接库
- linux 动态加载 动态链接库
- linux动态加载动态链接库
- Linux动态加载动态链接库
- Linux静态链接库,动态链接库,动态加载库
- linux 动态链接库加载实例
- linux动态链接库的加载顺序:
- Linux环境下加载动态链接库
- Linux qtcreator引用动态库.pro文件
- linux下的动态链接库编程
- Linux 动态链接库编程入门
- Linux动态链接库编程入门
- Linux 动态链接库编程基础
- Linux动态链接库编程入门
- Linux动态链接库编程入门
- Win10系统80端口被系统进程占用
- 成为JavaGC专家(1)—深入浅出Java垃圾回收机制
- Hadoop datanode服务无法启动
- c# 两个intPtr之间内存拷贝
- Echart-折线图
- Linux qtcreator编程实现动态加载动态链接库
- 关于如何下载spring tool Suite指定版本的安装包
- 6/26作业:抽象
- [题解]codeforces 718c Sasha and Array
- Python selenium.common.exceptions.WebDriverException
- 【c++】vector.clear()的内存泄露问题
- 面向对象的三大特征之一封装
- Linux笔记 pthread
- CodeForces 828C String Reconstruction(思维)