16,472
社区成员
发帖
与我相关
我的任务
分享
class T6Module
{
public:
T6Module();
virtual ~T6Module();
public:
static T6Module * getModule ( void );
void setIntValue ( int IntValue );
int getIntValue() const;
private:
int m_IntValue;
};
T6Module.cpp
#include "t6module.h"
T6Module::T6Module()
: m_IntValue(1)
{
}
T6Module::~T6Module()
{
}
static T6Module s_T6Module;
T6Module* T6Module::getModule ( void )
{
return &s_T6Module;
}
void T6Module::setIntValue ( int IntValue )
{
this->m_IntValue = IntValue;
}
int T6Module::getIntValue() const
{
return m_IntValue;
}
init.cpp
#include "t6module.h"
class InitA
{
public:
InitA(void)
{
T6Module::getModule()->setIntValue(99);
};
};
InitA g_a;
使用静态库的程序:
#include <stdio.h>
#include "../T006_PragmaDataSeg_SLib/t6module.h"
int main(int argc, char **argv)
{
printf("hello world\n");
int i = T6Module::getModule()->getIntValue();
return 0;
}
如果不把前面三个包成静态库,全放在一个项目下,init.cpp就会被链接,那么main里面 i = 99;
如果包成静态库,init.cpp就不会被链接,因为链接器认为这个档没有用到。那么i = 1;
我现在的推测是mfc其实内部有引用到他写implement_serial的东西,所以链接器还是把那些档链接进去了