2

I am new to C++. What I am doing is creating a callback library file in C. That file will get use in C++ application. C file will have only three functions and FYI I don't want to make another cpp file/class for that because of class overhead and also classes should be in cpp file only. please go through code snippet. C file XYZ.c is using class of ABC.hpp in its function.

Allheader.hpp

#include<iostream>
 .
 .

ABC.hpp

#include "Allheader.hpp"
  .
  .

XYZ.h

func1();
func2();
func3();

XYZ.c

include "XYZ.h"
include "ABC.hpp"

func1()
{
   using class of ABC here
}

func2() {}
func3() {}

MyApplication.cpp

extern "C" { #include "XYZ.h"} 
   .
   .

So This file XYZ.c / XYZ.h will be use in My CPP application as callback.

Compiling with g++

What I have done.

  1. Compiled : It's Showing error, No file iostream.
  2. As I was compiling C file and using C++ inside it started using __BEGIN_DECLS

    and code in XYZ.c file was in

    __BEGIN_DECLS __END_DECLS

    block suggested by a colleague. But still error was the same.

How can I use C++ inside C function? Please Suggest any solution.

Duega
  • 73
  • 1
  • 10
  • You can't compile a `C++` class definition with a `C` compiler even if you put `extern "C" {}` round it. – Galik Nov 30 '18 at 12:34
  • Hi @Galik Thank you for the Comment. If you are aware with my stated problem. I just need to call C++ function from C file. – Duega Nov 30 '18 at 13:16
  • I am not entirely sure what you are asking but this answer may help: https://stackoverflow.com/questions/31903005/how-to-mix-c-and-c-correctly/31903685#31903685 – Galik Nov 30 '18 at 13:18
  • _"I don't want to make another cpp file/class for that because of class overhead"_ What "overhead" are you referring to? – Lightness Races in Orbit Dec 07 '18 at 11:43
  • Hi LRIO Please see comments below. – Duega Dec 07 '18 at 12:38
  • I can't find a comment below that addresses my question. – Lightness Races in Orbit Dec 07 '18 at 13:05
  • We dun want to dedicate a class for these 3-4 from functions . We just want these functions in some file and use it as callback. You please suggest any thing if you gone through situation like this. Overhead means we don't want to create a class for this purpose. – Duega Dec 07 '18 at 13:10

2 Answers2

4

You can't compile C++ code as C code. This C code is invalid:

#include "XYZ.h"
#include "ABC.hpp"  // this is wrong

func1()
{
   using class of ABC here  // this is also wrong (and incomplete...)
}

func2() {}
func3() {}

Again - you simply can not do that. C and C++ are different languages. You might as well try asking "Why can't I compile Java or Python with a C compiler" and the answer would be the same, "Because it's not C."

You can call C++ functions with extern "C" linkage from C.

From a C++ file:

extern "C" void calledFromC()
{
     // C++ code
}

And then, in a C++ header that can be #include'd by C:

#ifdef __cplusplus
extern "C" {
#endif

void calledFromC();

#ifdef __cplusplus
}
#endif
Andrew Henle
  • 32,625
  • 3
  • 24
  • 56
  • Thank you for the answer @Andrew extern "C" is used to tell the compiler that Compiler shouldn't mangle the name of the function. Which is the case when we use C Function in C++ code. right? As I also discussed about __BEGIN_DECLS in the description. Can't We have any way to do it. – Duega Nov 30 '18 at 11:22
  • @Duega *extern "C" is used to tell the compiler that Compiler shouldn't mangle the name of the function* Yes. That's what it does. It makes the C++ code callable from C. It does add some restrictions, though - such functions can't be class methods. – Andrew Henle Nov 30 '18 at 11:24
-1

After few days of searching in parallel to my work. I am able to do it. As I said in my requirement that I need a Callback C file that contains some functions and We need to use it because We don't want a cpp file. (Because in our application cpp file will contain Class and We don't want a class for the callbacks).

Now What I did :

Application uses cmake to build. So I added

set_source_files_properties(${BASE_DIR}/path/to/XYZ.c PROPERTIES LANGUAGE CXX)

in CMakeLists.txt

It makes compiler know that It should compile XYZ.c file like a cpp file.

Basically If you "man g++" then it's written that

C++ source files conventionally use one of the suffixes .C, .cc, .cpp, .CPP, .c++, .cp, or .cxx; C++ header files often use .hh, .hpp, .H, or (for shared template code) .tcc; and preprocessed C++ files use the suffix .ii. GCC recognizes files with these names and compiles them as C++ programs even if you call the compiler the same way as for compiling C programs (usually with the name gcc).

And This is what I need. Just to tell the compiler that Compile like cpp.

Conclusion :

This is working and Yes this is not a way to call C++ class from C. As @Andrew told C and C++ are different languages. It's kind of the requirement that I need to follow and I am new to it so took some time in searching all these.

Thanks @Andrew & @Galik for your time.

Duega
  • 73
  • 1
  • 10
  • So your solution to the problem "how to write C++ in a C program" is "write a C++ program instead". Well, that's the only way, yes. But now you have a file with extension `.c` that contains C++ code, will not compile as C, and requires a C++ compiler to work. All you've done is produce a file with the wrong extension and added a build system hack to work around the incorrect file extension. So why don't you just call it `XYZ.cpp` and be done with it? – Lightness Races in Orbit Dec 07 '18 at 11:38
  • Hi Well the scenario is, I have one working module (Older) and we started for the next version (newer). Some of the features of older module We want to use in newer. older module has 3-4 functions that we want similar function for newer one but with association of different child classes in it. As we are not rewriting whole thing for newer so thinking of why not make dedicated file which would work as a Callback. So whatever library we need to include we will include in that C file and instead of writng whole thing again for newer, CONT. – Duega Dec 07 '18 at 12:06
  • We will include that callback file in older module and pass the callback function to its function pointer. And Also In my application Cpp file containing classes only. We don't want extra class for it so decided to use C file for that to differentiate. Till now we have thought for doing this. Please, any comments or suggestions would be greatly appreciated. – Duega Dec 07 '18 at 12:07
  • But it's not a C file. It's a C++ file, with the wrong extension. – Lightness Races in Orbit Dec 07 '18 at 13:04