2

I have a C++ file and its header file. I need to include this header file in a C code and use the functions in it.

When the cpp.h file is compiled through main.c, compilation fails because of the C++ linkage.

On using the macro __cplusplus stream and string are not resolved, is there some way to compile the cpp.h file through and execute?

I have given a outline of my code only.

C++ header file cpp.h:

struct s1
{
string a;
string b;
};
typedef struct s1 s2;
class c1
{
public:
void fun1(s2 &s3);
private: 
fun2(std::string &x,const char *y);
};

C++ file cpp.cpp:

c1::fun1(s2 &s3)
{
 fstream file;

}

c1::fun2(std::string &x,const char *y)
{

}

C file main.c:

#include "cpp.h"
void main()
{
 c1 c2;
 s1 structobj;
 c2.fun1(&structobj);
 printf("\n value of a in struct %s",structobj.a);

}
4
  • 10
    You can't use C++ classes (and many other things) in C. Commented Apr 23, 2016 at 4:40
  • Can you compile main.c using the C++ compiler instead? Commented Apr 23, 2016 at 4:42
  • No... there is no possibility of compiling main.c using c++ compiler because of dependency Commented Apr 23, 2016 at 12:08
  • Does call back solves this problem ? By introducing another cpp file to handle call back Commented Apr 23, 2016 at 12:08

2 Answers 2

9

Basically, you can't. You need to put only C functions in your header file. You put them in a extern "C" block this way:

#ifdef __cplusplus
extern "C"
{
#endif

extern void myCppFunction(int n);

#ifdef __cplusplus
}
#endif

The extern "C" block is not recognized by a C compiler, but the C++ compiler need it to understand he have to consider functions inside as C functions.

In your cpp file you can define myCppFunction() so that she uses any C++ code, you will get a function C code can use.

Edit: I add a full example of how to link a program with a C main() using some C++ functions in a module.

stackoverflow.c:

#include "outputFromCpp.h"

int main()
{
    myCppFunction(2000);

    return 0;
} 

outputFromCpp.h:

#ifndef OUTPUT_FROM_CPP_H
#define OUTPUT_FROM_CPP_H

#ifdef __cplusplus
extern "C"
{
#endif

extern void myCppFunction(int n);

#ifdef __cplusplus
}
#endif

#endif

outputFromCpp.cpp:

#include "outputFromCpp.h"

#include <iostream>

using namespace std;

void myCppFunction(int n)
{
    cout << n << endl;
}

Compiling and linking:

gcc -Wall -Wextra -Werror -std=gnu99 -c stackoverflow.c
g++ -Wall -Wextra -Werror -std=c++98 -c outputFromCpp.cpp
g++ -o stackoverflow.exe stackoverflow.o outputFromCpp.o -static

You cannot link such a program with gcc. If you want to link with gcc you need to put all the C++ code in a shared library, I don't put an example as it would be a bit platform dependent.

Sign up to request clarification or add additional context in comments.

4 Comments

If your program includes any C++ code or libraries, link with the C++ compiler. It's also sensible to make the main() program into C++ code, even if it is written as: int main(int argc, char **argv) { return real_main(argc, argv); } (or, if the code ignores command line arguments, then int main() { return real_main(); }) where, in each case, real_main() is the C function that would be main() if you weren't using a minimal C++ main() instead.
@JonathanLeffler: The toolchain is providing a wrapper main for you anyway, that knows about C++ globals initialization.
Is that possible to include a callback by introducing another c++ file. where the new cpp file uses the function to extract value and using call back from c file to the new c++ file.
@user4402433 I am not sure I understand your question. In my example I can put any C++ code in myCppFunction(), including creating C++ objects with new and deleting them with delete, or calling code from another C++ module. Does that answer your question?
3

This can be done by introducing a wrapper to c++ function. The C function calls the wrapper function which inturn calls the desired C++ function (including member functions). More details are available here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.