Welcome 微信登录

首页 / 操作系统 / Linux / Linux/Windows下静态lib和全局变量初始化

程序链接静态lib时,会将使用到的code部分包含进最终的exe或dll中,如果lib中定义了全局变量,但这个全局变量并没有被其他code使用到,那么最终的exe/dll会执行这个全局变量的定义语句吗?[Linux平台] Fedora14
经过测试,Linux的gcc/g++编译器的处理方法和Windows VS一样。TestLib.h TestLib.cpp TestLibConsole.cpp 都在同一个目录下。>g++ -c TestLib.cpp编译出目标文件:TestLib.o>ar rcs libTestLib.a TestLib.o生成静态lib。命名规则以“lib”为前缀。>g++ TestLibConsole.cpp libTestLib.a -o TestLibConsole编译TestLibConsole.cpp,链接TestLib.a,生成可执行文件TestLibConsole。>./TestLibConsole执行。[Windows 平台] VS2008创建TestLib工程,configuration type选择static library(lib)。创建一个简单的类People。TestLib.h
  1. #include<string>   
  2.   
  3. class People  
  4. {  
  5. public:  
  6.     People(std::string name);  
  7.     ~People();  
  8.   
  9.     std::string _name;  
  10. };  
TestLib.cpp构造函数打印语句。定义了一个全局变量g_Person。
  1. #include "stdafx.h"   
  2. #include <iostream>   
  3.   
  4. #include "TestLib.h"   
  5.   
  6. People::People(std::string name)  
  7. {  
  8.     std::cout << "Create a new People instance: " << name << std::endl;  
  9.     _name = name;  
  10. }  
  11.   
  12. People::~People()  
  13. {  
  14.   
  15. }  
  16.   
  17. People g_Person("Lily");  
再创建一个C++ console工程TestLibConsole。链接TestLib(solution property的project dependencies中设置)。
  1. #include <iostream>   
  2.   
  3. #include "../TestLib/TestLib.h"   
  4.   
  5. using namespace std;  
  6.   
  7. int main()  
  8. {  
  9.     cout << "Begin TestLibConsole.exe" << endl;  
  10.   
  11.     int local = 1;  
  12.     //People p("Panda");   
  13.   
  14.     cout << "End TestLibConsole.exe" << endl;  
  15.     return 0;  
  16. }  
1. 想像中,在load的时候,main函数执行之前,应该初始化g_Person,有输出“Create a new People instance:Lily”,但是没有:(编译器没有发现对TestLib.lib有任何方式的引用,所以根本没有把相关code包含进最终的TestLibConsole.exe中。2种方法确认:1) Notepad++ 打开TestLibConsole.pdb文件,搜索“People”,找不到这个symbol。2) 用windbg debug,Open Executable... 打开TestLibConsole.exe。lm (列出加载的模块)ld * (Load symbols)lm(这时应该会列出TestLibConsole.pdb了)x TestLibConsole!*Local* (有输出,命令x是检查symbol)x TestLibConsole!*People* (没有输出)(其他windog命令:dv, dt, k)
2. 将“People p("Panda")” 的注释去掉,编译执行,如期输出Create a new People instance:LilyBegin TestLibConsole.exeCreate a new People instance:PandaEnd TestLibConsole.exe
3. 可是有时候并不想在exe中这么调用lib中的code,可以这么做:在TestLib.h 增加:
  1. int ForceLinkMethod();   
  2. const int ForceLinkVar = ForceLinkMethod();  
在TestLib.cpp中增加:
  1. int ForceLinkMethod()   
  2. {   
  3.     return 0;   
  4. }  
这样即使没有“People p("Panda")”,这个语句,也会正确初始化全局变量g_Person。