首页 > 代码库 > linux dynamic lib
linux dynamic lib
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
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。