创建动态链接库,头文件报错expected initializer before “function name“

本文解释了_cdecl与__stdcall的区别,以及在函数声明中使用不当导致的初始化问题。重点在于C++调用约定的正确使用,以避免编译错误和潜在的栈溢出风险。
extern "C" MATHLIBRARY_API void _cdecl fibonacci_init(
    const unsigned long long a, const unsigned long long b);

头文件这个函数前报出expected initializer before “fibonacci_init”,这是由于函数前的_cdecl造成,只有一个下划线的cdecl和两个下划线的__cdecl是同义词,但是某些配置下,它们不一样:

为了与以前的版本兼容, cdecl 和 _cdecl 是一个同义词 __cdecl ,除非指定编译器选项 /Za (禁用语言扩展)

调用规范
语言扩展

因此,函数声明处应该用__cdecl或者直接不写,使用默认的C调用规范。
顺便在此说明一下__cdecl__stdcall的差异,
__cdecl是C语言的默认调用规范:

(1)参数从右向左依次压入堆栈.
(2)由调用者恢复堆栈,称为手动清栈。
(3)函数名自动加前导下划线。

即参数堆栈由调用函数者清理,堆栈空间有限,可能会发生Stack Overflow栈溢出。
__stdcall是C++调用规范:

(1)参数从右向左依次压入堆栈.
(2)由被调用函数自己来恢复堆栈,称为自动清栈。
(3)函数名自动加前导下划线,后面紧跟着一个@,其后紧跟着参数的大小。

C++调用规范下int func( int a, double b ) 的函数按如下所示进行修饰:_func@12

make报错In file included from src/ar.c:22:0: src/filedef.h:132:49: error: use of enum ‘cmd_state’ without previous declaration void set_command_state (struct file *file, enum cmd_state state); ^ In file included from src/ar.c:23:0: src/dep.h:73:10: error: declaration of ‘floc goaldep::floc’ [-fpermissive] floc floc; ^ In file included from src/ar.c:18:0: src/makeint.h:534:5: error: changes meaning of ‘floc’ from ‘typedef struct floc floc’ [-fpermissive] } floc; ^ In file included from src/ar.c:23:0: src/dep.h:139:6: error: use of enum ‘update_status’ without previous declaration enum update_status update_goal_chain (struct goaldep *goals); ^ src/dep.h:139:61: error: invalid type in declaration before ‘;’ token enum update_status update_goal_chain (struct goaldep *goals); ^ src/ar.c: In function ‘intmax_t ar_member_date_1(int, const char*, int, long int, long int, long int, intmax_t, int, int, unsigned int, const void*)’: src/ar.c:83:45: error: invalid conversion from ‘const void*’ to ‘const char*’ [-fpermissive] return ar_name_equal (name, mem, truncated) ? date : 0; ^ In file included from src/ar.c:18:0: src/makeint.h:611:5: error: initializing argument 1 of ‘int ar_name_equal(const char*, const char*, int)’ [-fpermissive] int ar_name_equal (const char *name, const char *mem, int truncated); ^ src/ar.c: In function ‘intmax_t ar_glob_match(int, const char*, int, long int, long int, long int, intmax_t, int, int, unsigned int, const void*)’: src/ar.c:212:23: error: expected unqualified-id before ‘new’ struct nameseq *new = xcalloc (state->size); ^ src/ar.c:212:23: error: expected initializer before ‘new’ src/ar.c:219:12: error: expected type-specifier before ‘->’ token new->name = strcache_add(concat(4, state->arname, "(", mem, ")"));
04-23
char**)’: /home/rosnoetic/demo2_ws/src/plumbing_pub_sub/src/demo2_pub.cpp:56:17: error: expected initializer before ‘ss’ 56 | ss<<"hello--->"<<count; | ^~ /home/rosnoetic/demo2_ws/src/plumbing_pub_sub/src/demo2_pub.cpp:58:26: error: ‘ss’ was not declared in this scope 58 | msg.data=ss.str(); | ^~ make[2]: *** [plumbing_pub_sub/CMakeFiles/demo2_pub.dir/build.make:63:plumbing_pub_sub/CMakeFiles/demo2_pub.dir/src/demo2_pub.cpp.o] 错误 1 make[1]: *** [CMakeFiles/Makefile2:418:plumbing_pub_sub/CMakeFiles/demo2_pub.dir/all] 错误 2 make: *** [Makefile:141:all] 错误 2 Invoking "make -j2 -l2" failed * 终端进程“/usr/bin/bash '-c', 'catkin_make'”已终止,退出代码: 1。 * 终端将被任务重用,按任意键关闭。 * 正在执行任务: catkin_make Base path: /home/rosnoetic/demo2_ws Source space: /home/rosnoetic/demo2_ws/src Build space: /home/rosnoetic/demo2_ws/build Devel space: /home/rosnoetic/demo2_ws/devel Install space: /home/rosnoetic/demo2_ws/install #### #### Running command: "make cmake_check_build_system" in "/home/rosnoetic/demo2_ws/build" #### #### #### Running command: "make -j2 -l2" in "/home/rosnoetic/demo2_ws/build" #### Scanning dependencies of target demo2_pub [ 50%] Building CXX object plumbing_pub_sub/CMakeFiles/demo2_pub.dir/src/demo2_pub.cpp.o /home/rosnoetic/demo2_ws/src/plumbing_pub_sub/src/demo2_pub.cpp: In function ‘int main(int, char**)’: /home/rosnoetic/demo2_ws/src/plumbing_pub_sub/src/demo2_pub.cpp:56:17: error: expected initializer before ‘ss’ 56 | ss<<"hello--->"<<count; | ^~ /home/rosnoetic/demo2_ws/src/plumbing_pub_sub/src/demo2_pub.cpp:58:26: error: ‘ss’ was not declared in this scope 58 | msg.data=ss.str(); | ^~ make[2]: *** [plumbing_pub_sub/CMakeFiles/demo2_pub.dir/build.make:63:plumbing_pub_sub/CMakeFiles/demo2_pub.dir/src/demo2_pub.cpp.o] 错误 1 make[1]: *** [CMakeFiles/Makefile2:418:plumbing_pub_sub/CMake
最新发布
07-21
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值