C++ windows,linux跨平台动态库加载整理
2014-01-09 21:45
351 查看
windows及linux动态库加载整理:
/*
* Library.h
*
* Created on: Jan 9, 2014
* Author: nohack
*/
#ifndef LIBRARY_H_
#define LIBRARY_H_
#include <stdlib.h>
#ifdef _WIN32_
#include <string>
#endif
#ifdef _GNU_
#include <string.h>
#endif
using namespace std;
class Library {
private:
string _libPath; ///<the path of library
int _libVersion; ///<the version of library
public:
#ifdef _WIN32_
HMODULE _module; ///<the handle of library
#else if _GNU_
void *_module; ///<the handle of library
#endif
public:
Library();
Library(const string filePath, string version="0");
virtual ~Library();
/**
*@brief load library
*/
bool load(string libFilePath);
/**
*@brief get symbol form library
*/
void getSymbol(const string symbolName);
private:
/**
*@brief release library
*/
void releaseLibrary();
};
#endif /* LIBRARY_H_ */
/*
* Library.cpp
*
* Created on: Jan 9, 2014
* Author: nohack
*/
#include "Library.h"
#include <iostream>
#include <sys/stat.h>
#ifdef _GNU_
#include <dlfcn.h>
#include <errno.h>
#include <string.h>
#endif
using namespace std;
bool fileIsExist(string &libFilePath)
{
struct stat statbuf;
return stat(libFilePath.c_str(), &statbuf)== 0;
}
Library::Library()
:_libPath(""),
_libVersion(0),
_module(NULL)
{
}
Library::Library(const string filePath, string version="0")
:_libPath(""),
_libVersion(0),
_module(NULL)
{
if(!load(filePath))
{
std::cout<<"Load library failed !"<<std::endl;
}
}
Library::~Library()
{
releaseLibrary();
}
bool Library::load(string libFilePath)
{
if(_module != NULL)
{
std::cout<<"The library has loaded !"<<std::endl;
return false;
}
if(!fileIsExist( libfilepath ) )
{
std::cout<<"The library is not exist!"<<std::endl;
}
#ifdef _WIN32_
_module = LoadLibrary(libfilepath.data());
if(_module = NULL)
{
std::cout<< GetlastError()<<std::endl;
}
#else if _GNU_
_module = dlopen(libfilepath.c_str(), RTLD_NOW | RTLD_GLOBAL);
if(_module = NULL)
{
std::cout<< strerror(errno)<<std::endl;
}
#endif
return _module != NULL;
}
void Library::getSymbol(const string symbolName)
{
if(_module == NULL)
{
std::cout<<"The library has not loaded !"<<std::endl;
}
#ifdef _WIN32_
return GetProcessAddress(_module, symbolName.c_str());
#else if _GNU_
return dlsym(_module, symbolName.c_str());
#endif
}
void Library::releaseLibrary()
{
if(_module)
{
#ifdef _WIN32_
FreeLibrary(_module);
#else if _GNU_
_module = NULL;
_libPath = ""
#endif
}
return true;
}
/*
* Library.h
*
* Created on: Jan 9, 2014
* Author: nohack
*/
#ifndef LIBRARY_H_
#define LIBRARY_H_
#include <stdlib.h>
#ifdef _WIN32_
#include <string>
#endif
#ifdef _GNU_
#include <string.h>
#endif
using namespace std;
class Library {
private:
string _libPath; ///<the path of library
int _libVersion; ///<the version of library
public:
#ifdef _WIN32_
HMODULE _module; ///<the handle of library
#else if _GNU_
void *_module; ///<the handle of library
#endif
public:
Library();
Library(const string filePath, string version="0");
virtual ~Library();
/**
*@brief load library
*/
bool load(string libFilePath);
/**
*@brief get symbol form library
*/
void getSymbol(const string symbolName);
private:
/**
*@brief release library
*/
void releaseLibrary();
};
#endif /* LIBRARY_H_ */
/*
* Library.cpp
*
* Created on: Jan 9, 2014
* Author: nohack
*/
#include "Library.h"
#include <iostream>
#include <sys/stat.h>
#ifdef _GNU_
#include <dlfcn.h>
#include <errno.h>
#include <string.h>
#endif
using namespace std;
bool fileIsExist(string &libFilePath)
{
struct stat statbuf;
return stat(libFilePath.c_str(), &statbuf)== 0;
}
Library::Library()
:_libPath(""),
_libVersion(0),
_module(NULL)
{
}
Library::Library(const string filePath, string version="0")
:_libPath(""),
_libVersion(0),
_module(NULL)
{
if(!load(filePath))
{
std::cout<<"Load library failed !"<<std::endl;
}
}
Library::~Library()
{
releaseLibrary();
}
bool Library::load(string libFilePath)
{
if(_module != NULL)
{
std::cout<<"The library has loaded !"<<std::endl;
return false;
}
if(!fileIsExist( libfilepath ) )
{
std::cout<<"The library is not exist!"<<std::endl;
}
#ifdef _WIN32_
_module = LoadLibrary(libfilepath.data());
if(_module = NULL)
{
std::cout<< GetlastError()<<std::endl;
}
#else if _GNU_
_module = dlopen(libfilepath.c_str(), RTLD_NOW | RTLD_GLOBAL);
if(_module = NULL)
{
std::cout<< strerror(errno)<<std::endl;
}
#endif
return _module != NULL;
}
void Library::getSymbol(const string symbolName)
{
if(_module == NULL)
{
std::cout<<"The library has not loaded !"<<std::endl;
}
#ifdef _WIN32_
return GetProcessAddress(_module, symbolName.c_str());
#else if _GNU_
return dlsym(_module, symbolName.c_str());
#endif
}
void Library::releaseLibrary()
{
if(_module)
{
#ifdef _WIN32_
FreeLibrary(_module);
#else if _GNU_
_module = NULL;
_libPath = ""
#endif
}
return true;
}
相关文章推荐
- ISE(Iris Server Engine)是一个基于现代C++的跨平台(Linux和Windows)框架
- linux和windows动态库加载路径区别
- windows C/C++移植linux(2) 加载动态共享库的不同
- Windows与Linux动态库的两种加载方式
- windows 和 linux 下c/c++内存分布(整理)
- c++实现windows和linux之间跨平台的方式
- Windows与Linux动态库的两种加载方式
- Windows与Linux平台Qt中动态库(共享库)的动态加载
- c/c++:动态库 静态库 linux/windows 例子 (转)
- C++遍历目录,并把目录里超过7天的文件删除(跨平台windows&linux)
- linux windows跨平台C++例子
- 【C/C++开发】C++静态库与动态库以及在Linux和Windows上的创建使用
- windows 和 linux 下c/c++内存分布(整理)
- 对比Windows和Linux两系统的动态库
- windows和linux环境下java调用C++代码-JNI技术
- 跨平台Socket通讯程序(Windows+Linux)底层封装
- C调用动态库,windows/linux
- C 文件夹遍历(Windows + Linux)跨平台
- opencv Mat 多通道c++ 操作方法(适用于Windows/Linux)
- 新成员!Visual Studio Code --跨平台的开发工具(支持OSX, Linux 和 Windows)