简体   繁体   English

如何在C中的extern函数中使用typedef?

[英]How to use typedef for extern function in C?

I have two modules written in C11 in my project: 'test1.c' and 'test2.c'. 我在项目中有两个用C11编写的模块:“ test1.c”和“ test2.c”。 Module 'test1.c': 模块“ test1.c”:

int FunctionWithVeryLONGLONGLONGLONGName(char* data)
{
    // do something
}

Module 'test2.c': 模块“ test2.c”:

extern int FunctionWithVeryLONGLONGLONGLONGName(char* data);

int main(void)
{
    char data[ DATA_LEN + 1 ] = { "test_data" };
    FunctionWithVeryLONGLONGLONGLONGName(data);
    return 0;
}

I want to use short name for function 'FunctionWithVeryLONGLONGLONGLONGName' in module 'test2.c' without modification of module 'test1.c'. 我想在模块“ test2.c”中使用函数“ FunctionWithVeryLONGLONGLONGLONGName”的简称,而无需修改模块“ test1.c”。 Fe, something like this: 铁,像这样:

FuncWithShortName(data);

I try to do: 我尝试做:

extern int FunctionWithVeryLONGLONGLONGLONGName(char* data);
typedef int FunctionWithVeryLONGLONGLONGLONGName(char* data);
FunctionWithVeryLONGLONGLONGLONGName FuncWithShortName;

int main(void)
{
    char data[ DATA_LEN + 1 ] = { "test_data" };
    FuncWithShortName(data);
    return 0;
}

But compiler gave an error: "Definition of function FunctionWithVeryLONGLONGLONGLONGNamerequires parentheses." 但是编译器给出了一个错误:“函数FunctionWithVeryLONGLONGLONGLONGName的定义需要括号。” What did I do wrong ? 我做错了什么 ?

typedef creates a type alias, not a function alias or anything else. typedef创建类型别名,而不是函数别名或其他任何东西。

You could get a sort of function alias by defining a file-local pointer to function: 您可以通过定义函数的文件本地指针来获得某种函数别名:

static int (*const FuncWithShortName)(char*) =
    FuncWithVeryLONGLONGLONGLONGName;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM