了解函数的返回类型

时间:2022-10-18 18:08:25

I am looking at this code passed onto me to resolve an issue but I have trouble understanding one of the function's definition.

我正在看这个传递给我的代码来解决一个问题,但我无法理解函数的定义之一。

void DESC_API ApplDesc(DescMsg* pMsg)

void DESC_API ApplDesc(DescMsg * pMsg)

I can see that the function isnt returning anything so the void holds good. But what is the DESC_API doing in here? This is how it is defined in the header file

我可以看到函数没有返回任何东西,所以void保持良好。但是DESC_API在这里做了什么?这是在头文件中定义的方式

#if defined(DESC_API)
#else
 /* No paging used. */
#define DESC_API
#endif

4 个解决方案

#1


3  

Looks like DESC_API a is visibility macro. It might be defined to __declspec(dllexport) or to __declspec(dllimport) for MSVC or to __attribute__ ((visibility ("default"))) for gcc or clang.

看起来像DESC_API是可见性宏。它可能被定义为__declspec(dllexport)或__declspec(dllimport)用于MSVC或__attribute __((visibility(“default”)))用于gcc或clang。

Of course, it might be defined to something else (as said bellow), but the most popular use case is just symbols visibility attributes.

当然,它可能被定义为其他东西(如下所述),但最流行的用例只是符号可见性属性。

Since gcc and clang export all symbols by default, no attributes is needed and DESC_API is blank. As for MSVC, your build system might set it via /DDESC_API=__declspec(dllimport) externally.

由于gcc和clang默认导出所有符号,因此不需要任何属性,DESC_API为空。对于MSVC,您的构建系统可能通过/ DDESC_API = __ declspec(dllimport)在外部设置它。

#2


1  

It looks like DESC_API could be used to switch between a function returning nothing and a function returning a void*.

看起来DESC_API可用于在返回任何函数的函数和返回void *的函数之间切换。

If you define

如果你定义

#define DESC_API *

the function would be returning a void pointer; otherwise, the function would return nothing.

该函数将返回一个void指针;否则,该函数将不返回任何内容。

Of course the rest of the function must follow through with a conditional return based on the value of DESC_API.

当然,函数的其余部分必须遵循基于DESC_API值的条件返回。

#3


1  

The macro appears to be a hook by which additional qualifiers can be injected into function signatures. I'm inclined to think that its intended usage is to leverage compiler-specific extensions under certain circumstances, such as to mark functions as using some particular calling convention.

该宏似乎是一个钩子,通过该钩子可以将额外的限定符注入到函数签名中。我倾向于认为其预期用途是在某些情况下利用特定于编译器的扩展,例如将函数标记为使用某些特定的调用约定。

Note that the macro definition you present is conditional. It provides for the macro to be defined (with empty replacement text) in the event that it is not already defined, where a previous definition might come from another header or from a command-line option.

请注意,您提供的宏定义是有条件的。它提供了在未定义宏的情况下定义宏(使用空替换文本),其中先前的定义可能来自另一个头或命令行选项。

#4


0  

Well, probably does nothing, as you can see by the macro the DESC_API is replaced by blank, hence this is probably a macro used for readability convenience that's it though

好吧,可能什么都不做,因为你可以看到宏被DESC_API替换为空白,因此这可能是一个用于方便阅读的宏,虽然它是

#1


3  

Looks like DESC_API a is visibility macro. It might be defined to __declspec(dllexport) or to __declspec(dllimport) for MSVC or to __attribute__ ((visibility ("default"))) for gcc or clang.

看起来像DESC_API是可见性宏。它可能被定义为__declspec(dllexport)或__declspec(dllimport)用于MSVC或__attribute __((visibility(“default”)))用于gcc或clang。

Of course, it might be defined to something else (as said bellow), but the most popular use case is just symbols visibility attributes.

当然,它可能被定义为其他东西(如下所述),但最流行的用例只是符号可见性属性。

Since gcc and clang export all symbols by default, no attributes is needed and DESC_API is blank. As for MSVC, your build system might set it via /DDESC_API=__declspec(dllimport) externally.

由于gcc和clang默认导出所有符号,因此不需要任何属性,DESC_API为空。对于MSVC,您的构建系统可能通过/ DDESC_API = __ declspec(dllimport)在外部设置它。

#2


1  

It looks like DESC_API could be used to switch between a function returning nothing and a function returning a void*.

看起来DESC_API可用于在返回任何函数的函数和返回void *的函数之间切换。

If you define

如果你定义

#define DESC_API *

the function would be returning a void pointer; otherwise, the function would return nothing.

该函数将返回一个void指针;否则,该函数将不返回任何内容。

Of course the rest of the function must follow through with a conditional return based on the value of DESC_API.

当然,函数的其余部分必须遵循基于DESC_API值的条件返回。

#3


1  

The macro appears to be a hook by which additional qualifiers can be injected into function signatures. I'm inclined to think that its intended usage is to leverage compiler-specific extensions under certain circumstances, such as to mark functions as using some particular calling convention.

该宏似乎是一个钩子,通过该钩子可以将额外的限定符注入到函数签名中。我倾向于认为其预期用途是在某些情况下利用特定于编译器的扩展,例如将函数标记为使用某些特定的调用约定。

Note that the macro definition you present is conditional. It provides for the macro to be defined (with empty replacement text) in the event that it is not already defined, where a previous definition might come from another header or from a command-line option.

请注意,您提供的宏定义是有条件的。它提供了在未定义宏的情况下定义宏(使用空替换文本),其中先前的定义可能来自另一个头或命令行选项。

#4


0  

Well, probably does nothing, as you can see by the macro the DESC_API is replaced by blank, hence this is probably a macro used for readability convenience that's it though

好吧,可能什么都不做,因为你可以看到宏被DESC_API替换为空白,因此这可能是一个用于方便阅读的宏,虽然它是