在文件夹R003下存在文件
–R003
----R003.h
----R003.cpp
----main.cpp
1、头文件:R003.h
#ifndef R003_H_
#define R003_H_
extern "C"
int IF_Security_DLL(const char* ConfigInform, const char* ConfigPath, char*& ConfigCode);
void Free_ConfigCode_IF_R009(char **rec_result_pureC);
#endif
2、源文件:R003.cpp
#include "R003.h"
#include
#include
#include
int IF_Security_DLL(const char* ConfigInform, const char* ConfigPath, char*& ConfigCode)
{
std::string Inform = ConfigInform;
std::string Path = ConfigPath;
std::string Code = "";
std::cout << Path.substr(Path.length() - 4, 4) << std::endl;
std::cout << Inform.substr(Inform.length() - 5, 5) << std::endl;
if (Path.substr(Path.length() - 4, 4) == "Path"&&Inform.substr(Inform.length() - 5, 5) == "IFSSC")
{
Code = Code + Inform + Path;
}
else
{
return 1;
}
int len = Code.length();
std::cout << len << std::endl;
char * codeout = (char *)malloc((len + 1) * sizeof(char));
//rec_result_pureC_tmp = (char *)malloc(sizeof(char)*len+1);
memset(codeout, 0, len + 1);
Code.copy(codeout, len);
ConfigCode = codeout;
return 0;
}
void Free_ConfigCode_IF_R009(char **rec_result_pureC)
{
if (*rec_result_pureC != NULL)
{
free(*rec_result_pureC);
*rec_result_pureC = NULL;
}
return;
}
3、调用测试文件:main.cpp
#define _CRT_SECURE_NO_WARNINGS
#include "R003.h"
#include
#include
int main()
{
const char* ConfigInform = "yiqundoushishabi_IFSSC";
const char* ConfigPath = "yiqundoushishabi_Path";
char* ConfigCode;
int flag = IF_Security_DLL(ConfigInform, ConfigPath, ConfigCode);
std::cout << strlen(ConfigCode) << std::endl;
for (int i = 0; i < strlen(ConfigCode); i++)
{
std::cout << ConfigCode[i];
}
Free_ConfigCode_IF_R009(&ConfigCode);
return 0;
}
g++ -Wall -g -fPIC -c R003.cpp -o R003.o
g++ -shared R003.o -o libR003.so
上面两条可以合并
g++ -Wall -g -fPIC -c R003.cpp -shared -o libR003.so
g++ -o main main.cpp -L. -lR003
可以通过指令 ldd main查看main依赖项
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home1/dc/dcstuff/CplusCompile/R003/
./main
在文件夹R003opencv下存在文件
–R003
----R003.h
----R003.cpp
----main.cpp
----0.jpg
1、头文件:R003.h
#ifndef R003_H_
#define R003_H_
#include
extern "C"
int IF_Security_DLL(const char* ConfigInform, const char* ConfigPath, char*& ConfigCode);
void Free_ConfigCode_IF_R009(char **rec_result_pureC);
void getimage(cv::Mat image);
#endif
2、源文件:R003.cpp
#include "R003.h"
#include
#include
#include
#include
int IF_Security_DLL(const char* ConfigInform, const char* ConfigPath, char*& ConfigCode)
{
std::string Inform = ConfigInform;
std::string Path = ConfigPath;
std::string Code = "";
std::cout << Path.substr(Path.length() - 4, 4) << std::endl;
std::cout << Inform.substr(Inform.length() - 5, 5) << std::endl;
if (Path.substr(Path.length() - 4, 4) == "Path"&&Inform.substr(Inform.length() - 5, 5) == "IFSSC")
{
Code = Code + Inform + Path;
}
else
{
return 1;
}
int len = Code.length();
std::cout << len << std::endl;
char * codeout = (char *)malloc((len + 1) * sizeof(char));
//rec_result_pureC_tmp = (char *)malloc(sizeof(char)*len+1);
memset(codeout, 0, len + 1);
Code.copy(codeout, len);
ConfigCode = codeout;
return 0;
}
void Free_ConfigCode_IF_R009(char **rec_result_pureC)
{
if (*rec_result_pureC != NULL)
{
free(*rec_result_pureC);
*rec_result_pureC = NULL;
}
return;
}
void getimage(cv::Mat image)
{
cv::imwrite("1.jpg",image);
}
3、调用测试文件:main.cpp
#define _CRT_SECURE_NO_WARNINGS
#include "R003.h"
#include
#include
int main()
{
const char* ConfigInform = "yiqundoushishabi_IFSSC";
const char* ConfigPath = "yiqundoushishabi_Path";
char* ConfigCode;
int flag = IF_Security_DLL(ConfigInform, ConfigPath, ConfigCode);
std::cout << strlen(ConfigCode) << std::endl;
for (int i = 0; i < strlen(ConfigCode); i++)
{
std::cout << ConfigCode[i];
}
Free_ConfigCode_IF_R009(&ConfigCode);
cv::Mat image = cv::imread("0.jpg");
getimage(image);
return 0;
}
1、添加OpenCV的 pkg_config到个人环境变量,一般在安装路径下,比如/usr/local/lib/x86_64-linux-gnu/pkgconfig/
在.barshrc中添加:
export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/x86_64-linux-gnu/pkgconfig
添加完成后记得
source ~/.barshrc
2、编译生成动态库
g++ -Wall -g -fPIC -c R003.cpp $(pkg-config --cflags --libs opencv) -shared -o libR003.so
3、编译调用生成的动态库
g++ -o main main.cpp $(pkg-config --cflags --libs opencv) -L. -lR003
3、添加环境变量路径(可以通过ldd main 查看main的依赖项,如果不能找到刚才生成的libR003 .so,则将当前.so所在的文件夹添加到环境变量中)
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home1/dc/dcstuff/CplusCompile/R003/
添加完成后记得
source ~/.barshrc
4、运行
./main