Complete project source code can access the download: download.csdn.net/download/li… \

First, the main program 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; }Copy the code


\

\

The corresponding go.pro file

TEMPLATE = app CONFIG += console c++11 CONFIG -= app_bundle CONFIG -= qt DESTDIR = .. /bin QMAKE_LFLAGS += -ldl # Compiler option, otherwise will fail. Comile option,dlopen() #Failed Load library! #libslstrlen.so: cannot open shared object file: No such file or directory LIBS += -L.. /bin/ -lslstrlen SOURCES += main.cppCopy the code


\

Dynamic library

The source file 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; }Copy the code

The header file slstrlen. H

#ifndef SLSTRLEN_H #define SLSTRLEN_H #include "slstrlen_global.h" // if the dynamic library is compiled using g++, extern "C" will be added to the function definition. Otherwise, undefined symbol error will be displayed. extern "C" int SLSTRLENSHARED_EXPORT StrLen(char *string); extern "C" char * SLSTRLENSHARED_EXPORT StrCopy(char *desc, char *src); #endif // SLSTRLEN_HCopy the code

The header file 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
Copy the code

Slstrlen. Pro file

#------------------------------------------------- # # 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. It's a lot shorter than the APIs deprecated before Qt 6.0.0 SOURCES += slstrlen.cpp HEADERS += slstrlen.h\ slstrlen_global.h unix { target.path = /usr/lib INSTALLS += target }Copy the code




\