【发布时间】:2017-09-12 06:44:47
【问题描述】:
我一直在努力解决我正在从事的项目的问题。 假设我有几个文件:
reflection.h
sll_insert.c
record_sll.c
io_recorder.cpp
上面提到的所有 cpp 和 c 文件都使用:
#include "reflection.h"
"reflection.h" 中有以下声明:
extern void io_recorder_fun_entry(char * name, TypedObject args[], int len);
extern void io_recorder_fun_exit(char * name, TypedObject args[], int len);
这两个函数在"io_recorder.cpp" 中实现,它们使用"io_recorder.cpp" 中的另一个函数io_record。
现在,在"io_recorder.cpp",两个函数如下:
extern "C" void io_recorder_fun_entry(char * name, TypedObject args[], int len) {
cout << "Entering function "<< name << endl;
io_record(args, len);
}
extern "C" void io_recorder_fun_exit(char * name, TypedObject args[], int len) {
cout << "Exiting function "<< name << endl;
io_record(args, len);
}
当在io_record, there are calls being made to several functions declared in reflection.h and implemented in"record_sll.c"`时。
我有两个问题:
-
在这种情况下,C 和 C++ 之间的连接对我不起作用(我之前已经用其他文件尝试过它,它似乎以前也可以工作)。 尝试编译时,我收到如下错误:
io_recorder.cpp: In function ‘void io_recorder_fun_entry(char*, TypedObject*, int)’:io_recorder.cpp:61:79: error: conflicting declaration of ‘void io_recorder_fun_entry(char*, TypedObject*, int)’ with ‘C’ linkage tern "C" void io_recorder_fun_entry(char * name, TypedObject args[], int len) {在来自
io_recorder.cpp:4:0: reflection.h:32:13: note: previous declaration with ‘C++’ linkage extern void io_recorder_fun_entry(char * name, TypedObject args[], int len);的文件中
显然我做错了什么,我不知道是什么。 为了正确编译,我应该改变什么?
- 在之前的错误中,我使用
io_record时似乎无法识别来自record_sll的函数。 它与问题1中的错误有什么关系吗?如果没有,我应该怎么做才能确保io_record认识他们。
提前感谢您的帮助,我希望我能尽快解决这个问题。
【问题讨论】:
-
您的标头声明中需要
extern "C"。 -
我做了,但我得到了一个不同的错误。 reflect.h:32:8: 错误:字符串常量 extern "C" void io_recorder_fun_entry(char * name, TypedObject args[], int len) 之前的预期标识符或‘(’;
-
这是一个可能有用的工作示例:stackoverflow.com/questions/31903005/…