【发布时间】:2020-11-10 20:54:16
【问题描述】:
我已阅读此answer,但如果我在 C++ 和 C 中具有相同名称的两个函数,我不知道该怎么办。 C 函数已在头文件中使用 if #ifdef __cplusplus 进行保护。
所以我有 .cpp 文件
foo (int a, int b)
{
//calling from C
foo (int a, int b, int c)
}
Cheader.h
#ifdef __cplusplus
extern "C" {
#endif
void foo(int a,int b, int c);
#ifdef __cplusplus
}
#endif
文件.c
#include "Cheader.h"
void foo(int a,int b, int c)
{
/* ... */
}
C++ 头文件
Cplusplusheader.hpp
void foo(int a, int b);
C++
CplusPlus.cpp
#include "Cplusplus.hpp"
#include "Cheader.h"
void foo(int a, int b)
{
foo(a,b,c); // call C function
// ...
}
【问题讨论】:
-
您能否进一步解释一下您是如何从 C++ 调用 C 函数以及项目的结构?
-
与参考答案相同
-
@HazemAbaza 这不是有用的信息
-
问题是什么?检查C++ namespaces
-
希望是一个明显的注释,但
foo(int a, int b, int c)不是对函数的调用:您可能指的是foo(a, b, 100);之类的东西。请将您的代码更新为您遇到问题的实际代码,以便我们也可以尝试。