用gcc生成静态库和动态库.pdf以及opencv的使用

一、gcc生成动态库和静态库

1、准备过程

创建hello.h文件并编写hello.h

#ifndef HELLO_H
#define HELLO_H
void hello(const char *name);
#endif//HELLO_H

创建hello.c文件,并编写hello.c

#include<stdio.h>
void hello(const char *name)
{
	printf("Hello %s\n",name);
}

创建main.c文件,并编写main.c文件

#include"hello.h"
int main()
{
	hello("everyone");
	return 0;
}

 gcc编译得到.o文件

gcc -c hello.c

2、静态库使用

(1)创建静态库

ar -crv libmyhello.a hello.o

(2)使用静态库

gcc -o hello main.c -L. -lmyhello

gcc main.c libmyhello.a -o hello

先生成main.o gcc -c main.c
生成可执行文件 gcc -o hello main.c libmyhello.a

 3、动态库使用

gcc -shared -fPIC -o libmyhello.so hello.o

 

 在程序中执行动态库

gcc -o hello main.c -L. -lmyhello

执行程序

mv libmyhello.so /usr/lib

中途出现没有权限进行mv libmyhello.so /usr/lib

需要获取权限来完成代码。 

四、静态库与动态库的使用

(1)编写sub1.c程序

float x2x(int a,int b)
{
	float c=0;
	c=a+b;
	return c;
}

(2)编写sub2.c

float x2y(int a,int b)
{
	float c=0;
	c=a/b;
	return c;
}

(3)sub.h的编写

#ifndef SUB_H
#define SUB_H
float x2x(int a,int b);
float x2y(int a,int b);
#endif

(4)main.c的编写

#include<stdio.h>
#include"sub.h"
void main()
{
	int a,b;
	printf("Please input the value of a:");
	scanf("%d",&a);
	printf("Please input the value of b:");
	scanf("%d",&b);
	printf("a+b=%.2f\n",x2x(a,b));
	printf("a/b=%.2f\n",x2y(a,b));
}

执行如下程序:

gcc -c sub1.c sub2.c

静态库

ar crv libsub.a sub1.o sub2.o

 之后执行

gcc -o main main.c libsub.a

动态库:分别执行如下代码段

gcc -shared -fPIC libsub.so sub1.o sub2.o
gcc -o main main.c libsub.so

静态库:

动态库:

经过观察比较发现静态库要比动态库要小很多,生成的可执行文件大小也存在较小的差别。

二、opencv的使用

 opencv的安装请参考链接(6条消息) 在 Ubuntu系统下安装 OpenCV 全过程_潇洒过路客的博客-CSDN博客

1、图片使用 

代码编写

首先建立一个文件夹study,然后在study目录下建立一个文件test1.cpp。

cd code
gedit test1.cpp

编写test1.cpp

#include <opencv2/highgui.hpp>
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
	CvPoint center;
    double scale = -3; 

	IplImage* image = cvLoadImage("lena.jpg");
	argc == 2? cvLoadImage(argv[1]) : 0;
	
	cvShowImage("Image", image);
	
	
	if (!image) return -1; 	center = cvPoint(image->width / 2, image->height / 2);
	for (int i = 0;i<image->height;i++)
		for (int j = 0;j<image->width;j++) {
			double dx = (double)(j - center.x) / center.x;
			double dy = (double)(i - center.y) / center.y;
			double weight = exp((dx*dx + dy*dy)*scale);
			uchar* ptr = &CV_IMAGE_ELEM(image, uchar, i, j * 3);
			ptr[0] = cvRound(ptr[0] * weight);
			ptr[1] = cvRound(ptr[1] * weight);
			ptr[2] = cvRound(ptr[2] * weight);
		}

	Mat src;Mat dst;
	src = cvarrToMat(image);
	cv::imwrite("test.png", src);

    cvNamedWindow("test",1);  	imshow("test", src);
	 cvWaitKey();
	 return 0;
}

执行如下命令

g++ test1.cpp -o test1 `pkg-config --cflags --libs opencv`

在study目录下保存如图所示图片

执行命令

./test1

 然后生成一个如图所示文件

对比两张图片

2、录制视频

(1)创建test.cpp文件

gedit test3.cpp

(2)编写test33.cpp文件,代码如下:

/*********************************************************************
打开电脑摄像头,空格控制视频录制,ESC退出并保存视频RecordVideo.avi
*********************************************************************/
#include<iostream>
#include <opencv2/opencv.hpp>
#include<opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>
using namespace cv;
using namespace std;

int main()
{
	//打开电脑摄像头
	VideoCapture cap(0);
	if (!cap.isOpened())
	{
		cout << "error" << endl;
		waitKey(0);
		return 0;
	}

	//获得cap的分辨率
	int w = static_cast<int>(cap.get(CV_CAP_PROP_FRAME_WIDTH));
	int h = static_cast<int>(cap.get(CV_CAP_PROP_FRAME_HEIGHT));
	Size videoSize(w, h);
	VideoWriter writer("RecordVideo.avi", CV_FOURCC('M', 'J', 'P', 'G'), 25, videoSize);
	
	Mat frame;
	int key;//记录键盘按键
	char startOrStop = 1;//0  开始录制视频; 1 结束录制视频
	char flag = 0;//正在录制标志 0-不在录制; 1-正在录制

	while (1)
	{
		cap >> frame;
		key = waitKey(100);
		if (key == 32)//按下空格开始录制、暂停录制   可以来回切换
		{
			startOrStop = 1 - startOrStop;
			if (startOrStop == 0)
			{
				flag = 1;
			}
		}
		if (key == 27)//按下ESC退出整个程序,保存视频文件到磁盘
		{
			break;
		}

		if (startOrStop == 0 && flag==1)
		{
			writer << frame;
			cout << "recording" << endl;
		}
		else if (startOrStop == 1)
		{
			flag = 0;
			cout << "end recording" << endl;
			
		}
		imshow("picture", frame);
	}
	cap.release();
	writer.release();
	destroyAllWindows();
	return 0;
}

(3)编译 test3.cpp 文件。

g++ test3.cpp -o test3 `pkg-config --cflags --libs opencv`

(4)执行如下

./test3

(5)执行结果,生成了一个avi文件。

 三、关于gcc的部分理解

命令    功能
-c只激活预处理、编译和汇编,生成扩展名为.o的目标代码文件(编写大型程序所必须)
-s只激活预处理和编译,生成扩展名.s的汇编代码文件
-E只激活预处理,并将结果输出至标准输出
-g为调试程序(如gdb)生成相关信息
-O对程序进行优化编译、链接,采用这个选项,整个源代码会在编译、链接过程中进行优化处理
-o用于指定要生成的结果文件用于指定要生成的结果文件

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值