0

I have read this answer but I do not know what to do if I have the two functions in C++ and C have the same name. The C function is already guarded with if #ifdef __cplusplus in the header file. so I have in .cpp file

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

file.c

#include "Cheader.h"

void foo(int a,int b, int c)
{
    /* ... */
}

C++ header

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
    // ...
}

Hazem Abaza
  • 331
  • 4
  • 21

1 Answers1

1

Well, I just made an example to test this out and you'd call it as any other function. The compiler seems to understand that the C foo is just an overload of your C++ foo. Here's an example:

main.cpp

//main.cpp

#include <iostream>
#include "header.h"

//c++ foo
void foo(int num1, int num2)
{
    foo(num1);
    foo(num2);
    std::cout << "Sum: " << num1 + num2 << std::endl;
}

int main()
{
    foo(10, 10);
    return 0;
}

header.h

//header.h
#ifndef HEADER_H
#define HEADER_H
#include <stdio.h>
#include <stdlib.h>

#ifdef __cplusplus
extern "C"
{
#endif

//c foo
void foo(int number);

#ifdef __cplusplus
}
#endif
#endif

source.c

//source.c

#include "header.h"

//c foo
void foo(int number)
{
    printf("The number is: %i\n", number);
}

Output:

The number is: 10
The number is: 10
Sum: 20
rdbo
  • 144
  • 10