首页 > 系统服务 > 详细

Linux动态库

时间:2020-11-26 11:26:33      阅读:23      评论:0      收藏:0      [点我收藏+]

Linux如果不设置参数编译动态库,默认所有的非static函数是向外导出的,如果我们只需要部分函数应该如下:

so.cpp如下:

 1 #include <stdio.h>
 2 #include "so.h"
 3 
 4 
 5 void  test()
 6 {
 7     printf("test\n");
 8 }
 9 
10 
11 int  test2(int _v)
12 {
13     return _v*_v;
14 }
15 
16 
17 void  foo::a()
18 {
19     printf("foo::a()\n");
20 }
21 
22 
23 int  foo::b(int _v)
24 {
25     return _v*_v;
26 }

 

 1 #ifndef __SO_H__
 2 #define __SO_H__
 3 
 4 #define DLL_PUBLIC __attribute__ ((visibility("default")))
 5 
 6 #ifdef __cplusplus
 7 extern "C" {
 8 #endif
 9 
10 DLL_PUBLIC  void  test();
11 int   test2(int _v);
12 
13 
14 class foo
15 {
16 public:
17     DLL_PUBLIC  void a();
18     int  b(int _v);
19 };
20 
21 
22 #ifdef __cplusplus
23 }
24 #endif
25 
26 
27 #endif

$ g++ -shared -o test.so -fPIC -fvisibility=hidden so.cpp

其中,__attribute__ ((visibility("default")))是默认可见标签,还有一个是__attribute__ ((visibility("hidden")))。-fvisibility=hidden,

意思是将动态库中的符号设置为默认不导出。这样一来,只有添加了DLL_PUBLIC,

也就是__attribute__ ((visibility("default")))标签的符号才会被导出

 

Linux动态库

原文:https://www.cnblogs.com/sudochen/p/14040683.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!