一切只是开始

来源: BlogBus 原始链接: http://www.blogbus.com:80/blogbus/blog/diary.php?diaryid=197662 存档链接: https://web.archive.org/web/20040607205829id_/http://www.blogbus.com:80/blogbus/blog/diary.php?diaryid=197662


一切只是开始 Linux, 嵌入式系统, 串口通信, PALM OS, Bluetooth ... 是工作中正用到的

Java, software engineering, 以及其他的很多技术,是我感兴趣的 <<<用GDB调试程序 (noted) | 首页 | How to Write Secure Code>>> 不要把 c++ 和 68k 共享库混在一起 时间: 04/05/29 from Combee on PalmOS translated by Carol C++ and 68K Shared Libraries Don't Mix 从其它平台转到 Palm OS 的人,希望可以建立 c++ 特征的共享库,但是传统68k共享库在palmos上很有限,导致c++代码有一些问题的。 先看看系统共享库是如何工作的。palm os 共享库是一个存在资源数据库(resource database)里的单独代码资源,类型 'libr', ID 0。程序调用共享库时,首先用 SysLibLoad API 调用共享库。通常应用程序会首先确认库是否load了。对于程序而言,最终结果就是一个LibRef值,用来进行之后的库调用。 动态库的调用使用的是和调用 OS 函数一样的系统追踪机制(system trap mechanism)。这就导致使用c++的一个问题。对于共享库调用而言, LibRef 值必须是传到函数中的第一个参数,而 c++ 方法调用传递 "this"指针作为隐藏的第一个参数。―― 这里就有一个冲突了。之所以要把 libref 作为第一个参数,因为你的调用不是直接到共享库的代码,而是进入了OS, LibRef用来决定分配系统里的哪个共享库。如果不把它作为第一个参数,OS就不知道怎么去追踪调用了,整个共享库的机制都会失败。 所以我们无法使用标准的机制,在共享库里调用c++方法。为什么我不能在库里使用c++而对外提供c 的接口呢?可以这么做,但是会遇到另一个 Palm OS 共享库的限制:库无法读/写全局变量。很多的c++特征依赖于使用全局变量,包括虚函数,异常处理,还有 RTTI。其他c++特征以来一个 runtime library 来实现帮助函数,来实现主特征,像 new 和 delete. 如果可以忍受上述限制,你可以在共享库里使用c++,但是我猜想大多数想使用c++共享库的人,希望 export 类, 而这是不支持的或者使用目前的机制。你得用一个c函数返回一个借口指针,类似 COM 的,但是共享库又无法维持一个 virtual table 来做这件事。 以后再谈谈对于实现者来说共享库工作的机制,默认的完成库安装的方式,不支持 multi code segmentation,我会谈谈那些有创意的和masochistic开发者是怎么做的。 People moving to Palm OS from other platforms often want to build shared libraries on the device that use C++ features. This usually is a bad idea, because traditional 68K shared libraries on Palm OS are very limited, and those limitations cause real problems with C++ code. First, lets look at how system shared libraries actually work. A Palm OS shared library is a single code resource with type 'libr' and ID 0 that stored in a resource database. When an application wants to call a shared library function, it first has to install the shared library using the SysLibLoad API call. Usually, the application will check first to see if the library is already loaded using the SysLibFind call, and will refrain from unloading the library when finished if it was loaded originally. To the application, the net result of this is a LibRef value, which is used for all future calls to the library. A shared library call is actually done using the same system trap mechanism used to call OS functions. This causes the first problem with C++; for the shared library call to work, the LibRef value has to be the first argument passed to the function, but a C++ method call passes the "this" pointer as a hidden first argument. You've got a conflict here. The reason this first argument has to be the library reference is that your call doesn't go directly to the shared library code. It actually goes into the OS, where the LibRef is used to determine to which shared library in the system the call should be dispatched. If that argument weren't first on the stack, the OS wouldn't know how to route the call, and the whole shared library mechanism would fail. So, we know that we can't call a C++ method in a shared library using the standard mechanism. Why can't I use C++ in the library and just expose a C interface? You can do this, but now you hit on another Palm OS shared library limitation: libraries don't have access to read/write global variables. Lots of features in C++ depend on global variables being available, including virtual functions, exception handling, and RTTI. Other C++ features depend on a runtime library implementing helper functions to implement hose features, like new and delete If you can live with those limits, you can use C++ for your shared library implementation, but I suspect most of the people wanting C++ shared libraries want to export classes, and that's just not supported or possible using the current mechanism. You have a C function that returns an interface pointer, similar to COM, but the shared library can't hold the virtual tables needed to make this work. I'll talk more about the mechanics of how shared libraries work for the implementer later; the default way of implementing library installation doesn't support multiple code segments, but I have some ideas about how they could be done by creative and masochistic developers. Note: some of this text doesn't apply to GLib shared libraries, an alternative mechanism supported by prc-tools. Ian Goldberg's article on GLib shared libraries is quite informative. carol 发表于 04/05/29 23:18 引用(Trackback0) 评论 发表评论 最后更新 The 11 Truths of Debugging [转贴]微软Smartphone应用程序设计入门 smartphone 一览 [Palm] 编写 modal form 的三种方法 蓝牙HID应用规范及系统解决方案 蓝牙应用连接的建立 How to Write Secure Code 不要把 c++ 和 68k 共享库混在一起 用GDB调试程序 (noted) debug mode to release mode