|
|

楼主 |
发表于 2006-2-6 18:36:12
|
显示全部楼层
1:如果在程序中运行:
- FORCE_MODULE_LINK("myFoo")
复制代码
时,是不是被:
- extern "C" void RTV_myFoo_ForceStaticLink()
复制代码 和
- extern "C" void LOCAL_RTV_myFoo_ForceStaticLink() { RTV_myFoo_ForceStaticLink();
- }
复制代码
代替。
2。看到过这里的extern "C" 类似使用共享库作为动态加载时的对外接口时用到的: extern C External_module(),不同的只是后者没有用双引号。 举个例子:
- include
- #include
- #include SMSGamePlugin.h
- int main(int argc, char** argv)
- {
- void *GameLib = dlopen(./Flower.so, RTLD_LAZY);
- const char *dlError = dlerror();
- if (dlError)
- {
- std::cout << dlopen error! << dlError << std::endl;
- return(-1);
- }
- CSMSGamePlugin *(*pGetGameObject)(void);
- pGetGameObject = (CSMSGamePlugin *(*)(void))dlsym(GameLib, GetGameObject);
- dlError = dlerror();
- if (dlError)
- {
- std::cout << dlsym error! << dlError << std::endl;
- return(-1);
- }
- CSMSGamePlugin *pGame = (*pGetGameObject)();
- pGame->Initialize();
- pGame->Load();
- pGame->Handle();
- delete *pGame;
- dlclose(GameLib);
- }
- 公用基类部分:SMSGamePlugin.h
- #ifndef __SMSGamePlugin_h__
- #define __SMSGamePlugin_h
- class CSMSGamePlugin
- {
- public:
- virtual int Initialize(void) = 0;
- virtual int Load(void) = 0;
- virtual int Handle(void) = 0;
- };
- #endif
- 编译:g++ -rdynamic -ldl -s -o Test main.cpp
- 共享库头文件:Flower.h
- #ifndef __Flower_h__
- #define __Flower_h__
- #include SMSGamePlugin.h
- extern C CSMSGamePlugin *GetGameObject(void);
- class CFlower: public CSMSGamePlugin
- {
- public:
- virtual int Initialize(void);
- virtual int Load(void);
- virtual int Handle(void);
- };
- #endif
- 共享库实现文件:Flower.cpp
- #include
- #include Flower.h
- CSMSGamePlugin *GetGameObject(void)
- {
- return(new CFlower());
- }
- int CFlower::Initialize(void)
- {
- std::cout << Initialize() << std::endl;
- return(0);
- }
- int CFlower::Load(void)
- {
- std::cout << Load() << std::endl;
- return(0);
- }
- int CFlower::Handle(void)
- {
- std::cout << Handle() << std::endl;
- return(0);
- }
- 编译:
- g++ -c Flower.cpp
- g++ -shared -o Flower.so
- 注意:
- 如果不加extern C编译后运行时就会提示:
- dlsym error!./Test: undefined symbol: GetGameObject
复制代码 |
|