码迷,mamicode.com
首页 > 系统相关 > 详细

linux dynamic lib

时间:2017-03-12 12:54:52      阅读:213      评论:0      收藏:0      [点我收藏+]

标签:.cpp   invalid   bsp   initial   which   man   end   eve   name   

1 // test1.h
2 int a = 10;
3 struct AA
4 {
5     int a,b:
6 };
 AA b(5,6);
7 int ball();
 1 // test1.cpp
 2 # include"test1.h"
 3 
 4 // the following line is invalid for it will not executed( if it‘s initialization of a var, it‘s valid)
 5 // a = 11;
 6 
 7 int ball()
 8 {
 9     a =13;
10     b = AA(3,4);
11     return a+4;
12 }

build a lib with command line : g++ -fPIC -shared test1.cpp -o libtt.so

# include<iostream>
using namespace std;

extern int a;
extern int b;
int ball();

int main()
{
    cout << " call ball() " << ball() <<endl;
    cout << "value of a " << a <<endl;
    cout << "value of b" << b << endl;
}

to build an executable with command line: g++ test-main.cpp -L./ -ltt

Notice:

  • in test-main.cpp, we can‘t use " int a" instead of "extern int a", because "int a" is a declaration, which also can be a defination.
  • wahile we can use " int ball()" instread of "extern int ball()" because " int ball() " will never be seen as a defination.
  • in test1.h, dont define "int a" with prefix ‘ extern "C" ‘, because in the symbol table of it‘s lib, the name of variable is the symbol
  • in test-main.cpp, we assume b is of type int and succeeds, because the symbol of b is "b", has no other information
  • C or C++ excutable can get the value of a/b; but if C wants to call ball() from a C++ lib, we should add the prifix " extern "C" " to "int ball()"

 

linux dynamic lib

标签:.cpp   invalid   bsp   initial   which   man   end   eve   name   

原文地址:http://www.cnblogs.com/resibe-3/p/6537285.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!