链接指示符

链接指示符extern “C”
为什么C++ 要提供链接指示符? 这是因为C++ 语言支持函数重载, 但C 语言却不支持. 在C++ 中, 为了支持重载, 在汇编代码的生成过程中, 编译器要对函数名做一些处理, 使实现重载的同名函数, 在汇编代码中用不同的名字标识. C 语言中, 只会简单地生成函数名, 而不会加入任何其它信息. C++ 语言为了实现对C 语言的支持, 因而提供了 extern “C”关键字, 以实现代码的复用.
举个简单的例子:
1. 在BSP的src 目录下新建一个目录, 比如Test, 然后在Test目录下新建一个文件print.cpp. 在print.cpp 中只实现一个函数void printmsg(void):

#include

void printmsg(void)
{
NKDbgPrintfW(L"Hello, My friend /r/n");
}


2. 拷贝一个makefile文件到Test 目录下.
3. 新建一个sources文件, 编辑如下:

TARGETNAME=test
TARGETTYPE=LIBRARY
WINCECOD=1

SOURCES= /
print.cpp /


4. 在命令行中CD 到Test目录下, build –c 编译print.cpp文件. 在Test/obj/ARMV4I/retail目录下找到print.cod 文件, 复制到另外一个目录下, 如桌面, 命名为 print_c++.cod.
5. 修改print.cpp文件, 在函数 printmsg 的前面加上 extern “C”:

#include

extern “C” void printmsg(void)
{
NKDbgPrintfW(L"Hello, My friend /r/n");
}


6. 编译 print.cpp, 同样, 在在Test/obj/ARMV4I/retail目录下找到print.cod 文件, 复制到另外一个目录下, 如桌面, 命名为 print_c.cod.
7. 比较print_c++.cod 和 print_c.cod:

……
EXPORT |?printmsg@@YAXXZ| ; printmsg
…………
EXPORT |printmsg|
……


由比较的结果可以看到, 如果不加 extern “C”, 函数printmsg 在编译后, 函数名被加上了一些新的信息, 成了另外一个名字. 而加了 extern “C” 之后, 函数printmsg 在编译后, 名字还和原来一样, 没有改变. 所以, 如果在另外一个C 文件中想要调用 printmsg 函数, 而 printmsg在编译时又没有加上 extern “C”, 就会出现 “unresolved external symbol …”

l #define DLLEXPORT_API extern "C"__declspec(dllexport)
DLLEXPORT_API int __stdcall InitDSPs();

Microsoft 在 Visual C++ 的 16 位编译器版本中引入了 __export,使编译器得以自动生成导出名并将它们放到一个 .LIB 文件中。然后,此 .LIB 文件就可以像静态 .LIB 那样用于与 DLL 链接。

在 32 位编译器版本中,可以使用 __declspec(dllexport) 关键字从 DLL 导出数据、函数、类或类成员函数。__declspec(dllexport) 将导出指令添加到对象文件,因此您不需要使用 .DEF 文件。

当试图导出 C++ 修饰函数名时,这种便利最明显。对名称修饰没有标准规范,因此导出函数的名称在不同的编译器版本中可能不一样。如果使用 __declspec(dllexport),仅当解决任何命名约定更改时才需要重新编译 DLL 和依赖 .EXE 文件。

许多导出指令(如序号、NONAME 和 PRIVATE)只能在 .DEF 文件中创建,并且没有不使用 .DEF 文件指定这些属性的方法。不过,在 .DEF 文件的基础上另外使用 __declspec(dllexport) 不会导致生成错误。

l _TCHAR and _tcsrev
The following code examples illustrate the use of _TCHAR and _tcsrev for mapping to the Unicode and single byte character models.
_TCHAR *RetVal, *szString;
RetVal = _tcsrev(szString);
If _UNICODE is defined, the preprocessor maps this fragment to the code:
wchar_t *RetVal, *szString;
RetVal = _wcsrev(szString);
If _UNICODE is not defined, the preprocessor maps the fragment to single-byte ASCII code:
char *RetVal, *szString;
RetVal = strrev(szString
l TEXT
This macro identifies a string as Unicode when the UNICODE compile flag is used or as an ANSI string when Unicode is not defined.
#define TEXT(quote) L##quote
Parameters
string
Specifies the string to be interpreted as either Unicode or ANSI.
l Core OS services
Core OS services consist of the Windows CE kernel and other features common to all Windows CE OS designs. They enable low-level tasks, such as process, thread, and memory management.
Serial Communications Overview
The Serial Port Support feature for Microsoft? Windows? CE enables communications with other computers, printers, modems, or Global Positioning System (GPS) satellites by way of a serial connection.
Serial I/O is the simplest form of communication supported by Windows CE. It is used when a direct, one-to-one connection exists between two devices. Serial I/O can occur by way of various hardware connections; however, most devices use serial cables or a PC Card device, such as a modem or infrared (IR) transceiver. Exchanging data by way of a serial cable is similar to reading from or writing to a file.
The standard Windows-based desktop functions for serial communications can be used to open, close, and manipulate serial ports, transmit and receive data, and manage the connection.
Feature Summary
The following table shows operating system design information for Serial Communications.
ElementInformation
DependenciesNone
Hardware considerationsSerial port and/or IR port

Modules and Components
The following table shows the components and modules that implement Serial Communications.
ItemModuleComponent
Serial Port Supportcoredllserdev

Implementation Considerations
The following table shows the Sysgen variables that implement the Serial Port Support feature.
Sysgen variableDescription
SYSGEN_SERDEVWhen this variable is set, the Serial Port Support feature is included in the run-time image.
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值