将 C++ 代码放入 C 时未定义对“function()”的引用
undefined reference to `function()' while putting C++ code inside C
我有点像这里描述的逆问题:
Combining C++ and C - how does #ifdef __cplusplus work?
整个应用程序都是用 C 代码编写的,现在我需要在那里添加一些 C++ 函数。
但是在这样做的时候我得到了这个错误:
/tmp/cczmWtaT.o: In function `aocl_utils::_checkError(int, char const*, int, char const*, ...)':
/home/harp/host/../common/src/AOCLUtils/opencl.cpp:245: undefined reference to `cleanup()'
/tmp/ccrmKQaT.o: In function `main':
/home/harp/host/src/main.c:165: undefined reference to `harp_setup()'
/tmp/ccGKataf.o: In function `solver_propagate(solver_t*)':
/home/harp/host/src/solver.c:751: undefined reference to `launch_kernel()'
collect2: error: ld returned 1 exit status
我试过:
#ifdef __cplusplus
extern "C" {
#endif
<C code>
#ifdef __cplusplus
}
#endif
但是显示同样的错误。
我正在做的是在 C 文件中包含一个 C++ 头文件,其中包含我需要的外部函数。
示例:
solver.c
#include "HarpBuffers.h"
...
HarpBuffers.h
extern void harp_setup();
extern void setup_buffers(cl_int a, cl_int b, int **h_clause, unsigned int **h_assigns, int **h_target);
extern void launch_kernel();
extern void cleanup();
你的函数声明应该在 extern "C"
块内,并且 C++ 编译器在遇到函数定义之前必须看到它们。
(extern "C"
是 C++ 编译器不会破坏函数名称的原因。
#ifdef __cplusplus
使代码对 C 编译器不可见。)
像这样:
HarpBuffers.h:
#ifdef __cplusplus
extern "C" {
#endif
void harp_setup();
void setup_buffers(cl_int a, cl_int b, int **h_clause, unsigned int **h_assigns, int **h_target);
void launch_kernel();
void cleanup();
#ifdef __cplusplus
}
#endif
(函数默认具有外部链接;无需在 header 中添加混乱。)
solver.c:
#include "HarpBuffers.h"
/* Use functions */
HarpBuffers.cpp:
#include "HarpBuffers.h"
// Define functions
您是说要在 C 代码中添加 C++ 代码,但 extern "C"
是在 C++
应用程序中添加 C
代码,因此在该部分中声明的代码不应该考虑进行名称修改以避免链接错误。所以声明 extern "C" 对 C 编译器没有任何意义。同样在你的代码中你有像 #ifdef __cplusplus
这样的预处理器指令,这个预处理器指令只会被 C++
编译器而不是 C
编译器考虑。因此,您的 C
编译器将简单地忽略该部分,并且不会在代码中添加 extern "C"
声明。因此,该声明对 C
编译器没有任何意义。它仅对 C++
编译器有意义,因为 C++
编译器会在编译期间将预处理器指令 #ifdef __cplusplus
下声明的那部分代码添加到 C++
代码中。请正确阅读预处理器指令的含义以及编译器在编译期间如何处理这些指令。另请阅读编译步骤。当您在 C++
代码中添加 C
代码时,您需要为 C++
编译器声明该指令。现在你只有一个选择,你必须用 C++
编译器而不是 C
编译器来编译整个代码。
我有点像这里描述的逆问题: Combining C++ and C - how does #ifdef __cplusplus work?
整个应用程序都是用 C 代码编写的,现在我需要在那里添加一些 C++ 函数。 但是在这样做的时候我得到了这个错误:
/tmp/cczmWtaT.o: In function `aocl_utils::_checkError(int, char const*, int, char const*, ...)':
/home/harp/host/../common/src/AOCLUtils/opencl.cpp:245: undefined reference to `cleanup()'
/tmp/ccrmKQaT.o: In function `main':
/home/harp/host/src/main.c:165: undefined reference to `harp_setup()'
/tmp/ccGKataf.o: In function `solver_propagate(solver_t*)':
/home/harp/host/src/solver.c:751: undefined reference to `launch_kernel()'
collect2: error: ld returned 1 exit status
我试过:
#ifdef __cplusplus
extern "C" {
#endif
<C code>
#ifdef __cplusplus
}
#endif
但是显示同样的错误。
我正在做的是在 C 文件中包含一个 C++ 头文件,其中包含我需要的外部函数。
示例:
solver.c
#include "HarpBuffers.h"
...
HarpBuffers.h
extern void harp_setup();
extern void setup_buffers(cl_int a, cl_int b, int **h_clause, unsigned int **h_assigns, int **h_target);
extern void launch_kernel();
extern void cleanup();
你的函数声明应该在 extern "C"
块内,并且 C++ 编译器在遇到函数定义之前必须看到它们。
(extern "C"
是 C++ 编译器不会破坏函数名称的原因。
#ifdef __cplusplus
使代码对 C 编译器不可见。)
像这样:
HarpBuffers.h:
#ifdef __cplusplus
extern "C" {
#endif
void harp_setup();
void setup_buffers(cl_int a, cl_int b, int **h_clause, unsigned int **h_assigns, int **h_target);
void launch_kernel();
void cleanup();
#ifdef __cplusplus
}
#endif
(函数默认具有外部链接;无需在 header 中添加混乱。)
solver.c:
#include "HarpBuffers.h"
/* Use functions */
HarpBuffers.cpp:
#include "HarpBuffers.h"
// Define functions
您是说要在 C 代码中添加 C++ 代码,但 extern "C"
是在 C++
应用程序中添加 C
代码,因此在该部分中声明的代码不应该考虑进行名称修改以避免链接错误。所以声明 extern "C" 对 C 编译器没有任何意义。同样在你的代码中你有像 #ifdef __cplusplus
这样的预处理器指令,这个预处理器指令只会被 C++
编译器而不是 C
编译器考虑。因此,您的 C
编译器将简单地忽略该部分,并且不会在代码中添加 extern "C"
声明。因此,该声明对 C
编译器没有任何意义。它仅对 C++
编译器有意义,因为 C++
编译器会在编译期间将预处理器指令 #ifdef __cplusplus
下声明的那部分代码添加到 C++
代码中。请正确阅读预处理器指令的含义以及编译器在编译期间如何处理这些指令。另请阅读编译步骤。当您在 C++
代码中添加 C
代码时,您需要为 C++
编译器声明该指令。现在你只有一个选择,你必须用 C++
编译器而不是 C
编译器来编译整个代码。