使用linux共享内存机制完成Qt与应用程序之间的通信,Qt加载制作自己的共享内存静态库!!!

使用linux共享内存机制完成Qt与应用程序之间的通信,Qt加载制作自己的共享内存静态库

首先完成共享内存小程序,源码:

shm.h头文件的程序:

#ifndef SHM_H
#define SHM_H

#ifdef __cplusplus
extern "C"{
#endif
typedef struct {
char name[4];
int age;
}people;
people* initshm(void);
void *Create_sharememory(const char *filename,unsigned int SizeCount);
void close_sharememory(void *pshare);
int get_age(people *p);
void set_age(people *p,int age);
#ifdef __cplusplus
}
#endif
#endif

再编写main.cpp函数完成静态库和头文件的调用

编译:g++ -o shm main.cpp -L. -lshm

运行:./shm

运行成功。

使用Qt程序加载前面制作的静态库

shm.cpp源文件的程序:

#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <unistd.h>
#include <fcntl.h>
#include <sys/shm.h>
#include <pthread.h>
#include "shm.h"
const char *shmfile="/usr/local/sharememory1/huangchao.txt";
people* initshm(void)
{
	people *p = (people *)Create_sharememory(shmfile,sizeof(people));
	if(p == NULL)
	printf("create sharememory failed!\n");
	return p;
}
void *Create_sharememory(const char *filename,unsigned int SizeCount)
{
	int shm_id,fd;
	key_t key;
	void *pshare;
	const char *path=filename;
if((fd=open(path,O_RDWR|O_CREAT))<0)
{
	perror("opne");
	return 0;
}
	close(fd);
	key=ftok(path,10);
if(key==-1)
{
	perror("ftok error\n");
	return 0;
}
if((SizeCount%2)!=0)
	SizeCount++;
	shm_id = shmget(key,SizeCount,IPC_CREAT);
if(shm_id==-1)
{
	perror("shmget error\n");
	return 0;
}
	pshare = shmat(shm_id,NULL,0);
if(pshare==NULL)
{
	return 0;
}
	return pshare;
}
void close_sharememory(void *pshare)
{
	if(shmdt(pshare)==-1)
	perror("detach error");
}
int get_age(people *p)
{
	return p->age;
}
void set_age(people *p,int age)
{
	p->age=age;
}

shm.cpp制作成静态库libshm,命令如下

g++ -c shm.cpp //生成shm.o链接文件

生成静态库,命令如下:

ar cr libshm.a shm.o //生成静态库

main.cpp源文件的程序:

#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <unistd.h>
#include <fcntl.h>
#include <sys/shm.h>
#include <pthread.h>
#include "shm.h"
int main()
{
int age;
people *t;
t=initshm();
set_age(t,20);
age = get_age(t);
printf("%d\n",age);
return 0;
}

再编写main.cpp函数完成静态库和头文件的调用

编译:g++ -o shm main.cpp -L. -lshm

生成了shm可执行文件

运行:./shm

运行成功。

使用Qt程序加载前面制作的静态库和头文件,分别把静态库文件libshm.a和头文件shm.h拷贝到自己所建的工程文件夹中,并在工程的源文件中添加头文件#include“shm.h”,如下所示

main.cpp源文件的程序:

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include "shm.h"
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        ui->label->setText("nihao");
        int age;
        people *t;
        t=initshm();
        set_age(t,20);
        age = get_age(t);
        ui->label->setText(QString::number(age));
    }
    MainWindow::~MainWindow()
    {
        delete ui;
    }

然后在pro工程文件中加入LIBS+=-L./ -lshm这样qt就能加载非官方的静态库了。

转载的原地址:http://blog.chinaunix.net/uid-26498888-id-3270926.html

参考资料

http://www.cnblogs.com/hicjiajia/archive/2012/05/17/2506632.html

http://hi.baidu.com/ishowfun/item/b10632f54066770fd89e7225

http://blog.163.com/hitperson@126/blog/static/130245975201151552938133/

http://blog.csdn.net/chenjin_zhong/article/details/6147858

http://it.100xuexi.com/view/otdetail/20120531/9662b7d8-e182-4808-ac4a-50cda4b09e83.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值