Calling a function in a .cpp file from a .c file

baronzo1

I've been struggling to solve a problem with a project I'm working on. Lets assume I have a few files:

reflection.h 
sll_insert.c
record_sll.c
io_recorder.cpp

all of the above mentioned cpp and c files use:

#include "reflection.h"

and in "reflection.h" are the following declarations:

extern void io_recorder_fun_entry(char * name, TypedObject args[], int len);

extern void io_recorder_fun_exit(char * name, TypedObject args[], int len);

These two functions are implemented in "io_recorder.cpp" and they use another function from "io_recorder.cpp" called io_record.

now, in "io_recorder.cpp", the two functions are as follows:

extern "C" void io_recorder_fun_entry(char * name, TypedObject args[], int len) {
    cout << "Entering function "<< name << endl; 
    io_record(args, len);
}

extern "C" void io_recorder_fun_exit(char * name, TypedObject args[], int len) {
    cout << "Exiting function "<< name << endl; 
    io_record(args, len);

}

When in io_record, there are calls being made to several functions declared in reflection.h and implemented in"record_sll.c"`.

I have 2 questions:

  1. The connection between C and C++ is not working for me in that case (i have experimented with it previously with other files and it seemed to work before). When trying to compile I receive errors like:

    io_recorder.cpp: In function ‘void io_recorder_fun_entry(char*, TypedObject*, int)’:

    io_recorder.cpp:61:79: error: conflicting declaration of ‘void io_recorder_fun_entry(char*, TypedObject*, int)’ with ‘C’ linkage tern "C" void io_recorder_fun_entry(char * name, TypedObject args[], int len) {

    In file included from io_recorder.cpp:4:0: reflection.h:32:13: note: previous declaration with ‘C++’ linkage extern void io_recorder_fun_entry(char * name, TypedObject args[], int len);

Obviously I did something wrong and I can't figure out what. What am I supposed to change in order for it to compile properly?

  1. In previous errors, it appeared that io_record did not recognize the functions from record_sll when I used them. Does it have anything to do with the errors from question 1? If not, what should I do to make sure io_record knows them.

Thanks in advance for the help, I hope i can fix this soon.

Christophe

From the C++ point of view, you have conflicting definition:

  • in your header you declare both function as a normal c++ function (i.e. no specific calling conventions)
  • in the body you define the fuction as using the C calling convention

This is a problem, because in all the compilation units using your header, the compiler will generate code using the c++ calling sequence, not knowing that your function bodies use another calling sequence. Hence the error message.

To solve the issue, you must declare the functions in both cases as extern "C".

From the C point of view however, the extern "C" syntax is not recognized. So if you want to use the same header for both C++ and C, some extra gym with conditional compilation is required:

#ifdef __cplusplus
extern "C" {  // this line will be compiled only if included in a .cpp file and not in a .c
#endif
    // your c function declaration go here without the extern "C"
#ifdef __cplusplus
}
#endif

You'll find additional info on mixing C and C++ on this FAQ page.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From

Calling a function from another file fails

From Dev

Calling a Python function from another file

From Dev

Octave calling function from another m file

From Dev

Python Calling Function from Another File

From Dev

Calling Dispatch function from a blank javascript file

From Dev

Error when I call cpp function from another .cpp file

From Dev

Calling a function from HTML to Python file

From Dev

Calling a function from another file to use in templates

From Dev

C++ Using a reference function from header file in the cpp?

From Dev

Calling ViewController function from another .swift file

From Dev

Calling function in main file from component

From Dev

Linker error calling .mm function from .cpp file

From Dev

Python: calling function from imported file

From Dev

C++ Calling set function from another class in file reader

From Dev

Calling a function from another file into another function C++

From Dev

Calling function in a C++ source file from main

From Dev

Calling an output from a function from a different file?

From Dev

Calling a function in an external C file from a cuda file

From Dev

C++ call inline function from another cpp file

From Dev

How do I use a variable from a function of a cpp file on another function in anotehr cpp file?

From Dev

PHP calling function from another file not working

From Dev

LNK2019: Calling C function from CPP code

From Dev

How to call a function from a separate .cpp file?

From Dev

Calling a function from a different react file

From Dev

Calling a template function from a C file

From Dev

Calling C++ function from a C file in Android

From Dev

Calling an assembly function from another file

From Dev

calling .c file inside .cpp

From Dev

Calling an Objective-C function defined in a .h file from Swift

Related Related

  1. 1

    Calling a function from another file fails

  2. 2

    Calling a Python function from another file

  3. 3

    Octave calling function from another m file

  4. 4

    Python Calling Function from Another File

  5. 5

    Calling Dispatch function from a blank javascript file

  6. 6

    Error when I call cpp function from another .cpp file

  7. 7

    Calling a function from HTML to Python file

  8. 8

    Calling a function from another file to use in templates

  9. 9

    C++ Using a reference function from header file in the cpp?

  10. 10

    Calling ViewController function from another .swift file

  11. 11

    Calling function in main file from component

  12. 12

    Linker error calling .mm function from .cpp file

  13. 13

    Python: calling function from imported file

  14. 14

    C++ Calling set function from another class in file reader

  15. 15

    Calling a function from another file into another function C++

  16. 16

    Calling function in a C++ source file from main

  17. 17

    Calling an output from a function from a different file?

  18. 18

    Calling a function in an external C file from a cuda file

  19. 19

    C++ call inline function from another cpp file

  20. 20

    How do I use a variable from a function of a cpp file on another function in anotehr cpp file?

  21. 21

    PHP calling function from another file not working

  22. 22

    LNK2019: Calling C function from CPP code

  23. 23

    How to call a function from a separate .cpp file?

  24. 24

    Calling a function from a different react file

  25. 25

    Calling a template function from a C file

  26. 26

    Calling C++ function from a C file in Android

  27. 27

    Calling an assembly function from another file

  28. 28

    calling .c file inside .cpp

  29. 29

    Calling an Objective-C function defined in a .h file from Swift

HotTag

Archive