【VxWorks5(2),2024年最新理解透彻

typedef struct msg_q MSG_Q_ID; / message queue ID */

typedef struct /* MSG_Q_INFO /
{
int numMsgs; /
OUT: number of messages queued /
int numTasks; /
OUT: number of tasks waiting on msg q */

int sendTimeouts; /* OUT: count of send timeouts /
int recvTimeouts; /
OUT: count of receive timeouts */

int options; /* OUT: options with which msg q was created /
int maxMsgs; /
OUT: max messages that can be queued /
int maxMsgLength; /
OUT: max byte length of each message */

int taskIdListMax; /* IN: max tasks to fill in taskIdList /
int * taskIdList; /
PTR: array of task ids waiting on msg q */

int msgListMax; /* IN: max msgs to fill in msg lists /
char ** msgPtrList; /
PTR: array of msg ptrs queued to msg q /
int * msgLenList; /
PTR: array of lengths of msgs */

} MSG_Q_INFO;

/* macros */

/* The following macro determines the number of bytes needed to buffer

  • a message of the specified length. The node size is rounded up for
  • efficiency. The total buffer space required for a pool for
  • messages each of up to bytes is:
  • maxMsgs * MSG_NODE_SIZE (maxMsgLength)
    */

#define MSG_NODE_SIZE(msgLength)
(MEM_ROUND_UP((sizeof (MSG_NODE) + msgLength)))

结构体类型定义,在此处定义该模块所需的和其他包含该模块所需的结构体类型。


#if defined(STDC) || defined(__cplusplus)

extern STATUS msgQLibInit (void);
extern MSG_Q_ID msgQCreate (int maxMsgs, int maxMsgLength, int options);
extern STATUS msgQDelete (MSG_Q_ID msgQId);
extern STATUS msgQSend (MSG_Q_ID msgQId, char *buffer, UINT nBytes,
int timeout, int priority);
extern int msgQReceive (MSG_Q_ID msgQId, char *buffer, UINT maxNBytes,
int timeout);
extern STATUS msgQInfoGet (MSG_Q_ID msgQId, MSG_Q_INFO *pInfo);
extern int msgQNumMsgs (MSG_Q_ID msgQId);
extern void msgQShowInit (void);
extern STATUS msgQShow (MSG_Q_ID msgQId, int level);

#else /* STDC */

extern STATUS msgQLibInit ();
extern MSG_Q_ID msgQCreate ();
extern STATUS msgQDelete ();
extern STATUS msgQSend ();
extern int msgQReceive ();
extern STATUS msgQInfoGet ();
extern int msgQNumMsgs ();
extern void msgQShowInit ();
extern STATUS msgQShow ();

根据不同的编译器使用不同形式的外部函数声明。

注意观察一下发现,好像是如果是C++编译器的外部函数声明比C编译器的外部函数声明会多函数参数,这个不知道是啥原因,难道C编译器的外部函数声明不需要说明函数参数吗?

上面的推测好像是正确的,C++编译器在对函数生成符号表时,会根据函数参数和函数名一起生成符号表;而C编译器只会根据函数名来生成符号表。

因此对于C++编译器,我们最好把函数参数写出,而对于C语言编译器,我们可以不写,在单独编译包含了该头文件的源文件是,我们不需要详细说明函数参数,等到链接时再确定。

先不管,用代码测试一下

//main.c
#include <stdio.h>
#include “add.h”

int main(void) {
add(1, 3);
}

//add.h
extern int add();//或者extern int add(int a, int b);都可以通过编译

//add.c
int add(int a, int b) {
return a + b;
}

上面的C语言项目使用C编译器可以通过编译,而且是不管add.h中的函数参数可以有也可以没有都可以正常编译运行。

当我们把代码改成如下

//main.cpp
#include
#include “add.h”

using namespace std;
int main(void) {
add(1, 3);
}

//add.h
extern int add();//或者extern int add(int a, int b);都可以通过编译

//add.c
int add(int a, int b) {
return a + b;
}

因为我们使用的头文件是 iostream,所以IDE会使用C++编译器。在使用C++编译器时,该项目是不能通过编译的,提示[Error] too many arguments to function ‘int add()’,意思是你声明的时候没有参数,现在又有了两个参数,因此报错了。

这就是因为C++编译器对函数名的处理了,在main.cpp文件中,我们通过头文件包含的add函数没有参数,因此C++编译器将其处理成符号名 add ,而在add.c 文件中,add函数是有参数的,因此C++编译器会把add函数处理成符号为 add_int_int之类的符号名,当然在他们单独编译时是可能正常的,但到链接时,add函数的两个符号名不能匹配起来,因此会造成错误;而C编译器可以正常处理的原因就是,在C编译器下,函数的符号名只和函数名有关,和函数参数无关。

意外之喜,没想到刚开始看源码,就收获了知识的理解。


头文件内容总结

整个头文件的内容就是这些了,最有意思的应该就是

typedef struct /* MSG_Q_INFO /
{
int numMsgs; /
OUT: number of messages queued /
int numTasks; /
OUT: number of tasks waiting on msg q */

int sendTimeouts; /* OUT: count of send timeouts /
int recvTimeouts; /
OUT: count of receive timeouts */

int options; /* OUT: options with which msg q was created /
int maxMsgs; /
OUT: max messages that can be queued /
int maxMsgLength; /
OUT: max byte length of each message */

int taskIdListMax; /* IN: max tasks to fill in taskIdList /
int * taskIdList; /
PTR: array of task ids waiting on msg q */

int msgListMax; /* IN: max msgs to fill in msg lists /
char ** msgPtrList; /
PTR: array of msg ptrs queued to msg q /
int * msgLenList; /
PTR: array of lengths of msgs */

} MSG_Q_INFO;

看含义应该是消息队列信息结构体类型的定义

里面有各种含义的整型,有数组指针,有二维指针,通过这些数据结构的特性来相应的存储消息队列的各种信息。

int * taskIdList; /* PTR: array of task ids waiting on msg q */任务ID数组

char ** msgPtrList; /* PTR: array of msg ptrs queued to msg q */数组,数组存储的每个元素是消息的字符指针

int * msgLenList; /* PTR: array of lengths of msgs */数组,每个消息的长度


下面就是找来msgQlib.c文件来看看吧

  • 6
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值