.h文件应该是与外部函数的接口;如何在包含标头的.cc文件中进行定义

.h file supposed to be interface with extern functions; how to define in .cc file that includes the header?

本文关键字:文件 cc 包含标 定义 外部 接口 函数      更新时间:2023-10-16

我的头文件1t.h中有一个函数,如下所示:

extern int dthreads_libinit(dthreads_func_t func, void *arg);

然后我想在一个单独的文件中实现这个功能,其中包括1t.h:

int dthreads_libinit(dthreads_funct_t func, void* arg) {
    //Do something here...
}

我得到这些错误虽然:

‘int dthreads_libinit’ redeclared as different kind of symbol'
error: previous declaration of ‘int dthreads_libinit(dthreads_func_t, void*)

我做的事情出了什么问题?

函数定义的签名中有一个拼写错误

 dthreads_libinit(dthreads_funct_t func, void* arg) {
                            // ^

如果您将此更正为

 dthreads_libinit(dthreads_func_t func, void* arg) {

因为它用于函数声明中的func参数

 extern int dthreads_libinit(dthreads_func_t func, void *arg);

代码编译良好(无论使用g++还是gcc)。