Chinaunix首页 | 论坛 | 博客
  • 博客访问: 286654
  • 博文数量: 23
  • 博客积分: 491
  • 博客等级: 下士
  • 技术积分: 612
  • 用 户 组: 普通用户
  • 注册时间: 2011-11-29 19:57
文章分类

全部博文(23)

文章存档

2013年(1)

2012年(22)

我的朋友

分类: C/C++

2012-07-11 17:31:38

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

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


点击(此处)折叠或打开

  1. /*shm.h*/

  2. #ifndef SHM_H
  3. #define SHM_H

  4. #ifdef __cplusplus
  5. extern "C"{
  6. #endif
  7. typedef struct {
  8. char name[4];
  9. int age;
  10. }people;
  11. people* initshm(void);
  12. void *Create_sharememory(const char *filename,unsigned int SizeCount);
  13. void close_sharememory(void *pshare);
  14. int get_age(people *p);
  15. void set_age(people *p,int age);
  16. #ifdef __cplusplus
  17. }
  18. #endif
  19. #endif

点击(此处)折叠或打开

  1. /*shm.cpp*/

  2. #include <cstdlib>
  3. #include <cstdio>
  4. #include <cstring>
  5. #include <unistd.h>
  6. #include <fcntl.h>
  7. #include <sys/shm.h>
  8. #include <pthread.h>
  9. #include "shm.h"
  10. const char *shmfile="/home/xjf/Desktop/pro/1.txt";
  11. people* initshm(void)
  12. {
  13. people *p = (people *)Create_sharememory(shmfile,sizeof(people));
  14. if(p == NULL)
  15. printf("create sharememory failed!\n");
  16. return p;
  17. }
  18. void *Create_sharememory(const char *filename,unsigned int SizeCount)
  19. {
  20. int shm_id,fd;
  21. key_t key;
  22. void *pshare;
  23. const char *path=filename;
  24. if((fd=open(path,O_RDWR|O_CREAT))<0)
  25. {
  26. perror("opne");
  27. return 0;
  28. }
  29. close(fd);
  30. key=ftok(path,10);
  31. if(key==-1)
  32. {
  33. perror("ftok error\n");
  34. return 0;
  35. }
  36. if((SizeCount%2)!=0)SizeCount++;
  37. shm_id = shmget(key,SizeCount,IPC_CREAT);
  38. if(shm_id==-1)
  39. {
  40. perror("shmget error\n");
  41. return 0;
  42. }
  43. pshare = shmat(shm_id,NULL,0);
  44. if(pshare==NULL)
  45. {
  46. return 0;
  47. }
  48. return pshare;
  49. }
  50. void close_sharememory(void *pshare)
  51. {
  52. if(shmdt(pshare)==-1)
  53. perror("detach error");
  54. }
  55. int get_age(people *p)
  56. {
  57. return p->age;
  58. }
  59. void set_age(people *p,int age)
  60. {
  61. p->age=age;
  62. }


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

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

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


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



点击(此处)折叠或打开

  1. /*main.cpp*/


  2. #include <cstdlib>
  3. #include <cstdio>
  4. #include <cstring>
  5. #include <unistd.h>
  6. #include <fcntl.h>
  7. #include <sys/shm.h>
  8. #include <pthread.h>
  9. #include "shm.h"
  10. int main()
  11. {
  12. int age;
  13. people *t;
  14. t=initshm();
  15. set_age(t,20);
  16. age = get_age(t);
  17. printf("%d\n",age);
  18. return 0;
  19. }


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

运行:./shm

运行成功。

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

点击(此处)折叠或打开

  1. #include "mainwindow.h"
  2. #include "ui_mainwindow.h"
  3. #include "shm.h"
  4. MainWindow::MainWindow(QWidget *parent) :
  5.     QMainWindow(parent),
  6.     ui(new Ui::MainWindow)
  7. {
  8.     ui->setupUi(this);
  9.     ui->label->setText("nihao");
  10.     int age;
  11.     people *t;
  12.     t=initshm();
  13.     set_age(t,20);
  14.     age = get_age(t);
  15.     ui->label->setText(QString::number(age));
  16. }
  17. MainWindow::~MainWindow()
  18. {
  19.     delete ui;
  20. }


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


参考资料

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

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

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




阅读(8295) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~