Linux g++編譯動態鏈接庫以及C++OpenCV工程調用

編譯簡單的動態鏈接庫

代碼與文件格式

在文件夾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 <string.h>
#include <stdlib.h>
#include <iostream>

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 <string.h>
#include <iostream>

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;
}

編譯與調用

1、生成動態鏈接庫

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

2、編譯調用生成的動態庫

g++ -o main main.cpp -L. -lR003

可以通過指令 ldd main查看main依賴項

3、添加環境變量路徑

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home1/dc/dcstuff/CplusCompile/R003/

4、運行

 ./main

編譯依賴第三方庫(opencv)的動態鏈接庫

代碼與文件格式

在文件夾R003opencv下存在文件
–R003
----R003.h
----R003.cpp
----main.cpp
----0.jpg

1、頭文件:R003.h

#ifndef R003_H_
#define R003_H_

#include <opencv2/opencv.hpp>
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 <string.h>
#include <stdlib.h>
#include <iostream>
#include <opencv2/opencv.hpp>

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 <string.h>
#include <iostream>


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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章