完整的工程源码可以访问下载: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
}