2
Good morning, everyone.
I have a main program in C, and I just want to call a function that is in another C++ file and get the return of this function.
How do I do that? I researched and saw that "extern" can help but I’m not succeeding.
Here’s what I want to do: In the program. c:
#include <stdio.h>
#include <stdlib.h>
#include "chain_code.cpp" // Arquivo C++
extern int chains(char*);
int main (int argc, char* argv[]){
// Faz alguma coisa
printf("%d ", chains(argv[1])); // Chama a função que está programada em C++
}
In the file "chaincode.cpp":
#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"
// Outros includes
int chains(char* path){
// Gera histograma da imagem Path
// devolve a media do histograma
I’m new here so I’m sorry if I made reading a little difficult
It is not advisable to call C++ code from C. The C++ compiler does "name mangling" which makes the functions hidden to C. C++ has exceptions in its internal library and the C compiler does not know how to handle it, It is easier to use C based on C++.
– pmg
Hmm.. And how would I do that?
– DEB
"and how would I do that?" Writes the main program in C++. All language mixing problems cease to exist :-)
– pmg
If your routine is not a class, simply put the statement exactly as it is written. The Font should be in the project and not as a include. The compiler is in charge of locating the routine.
– lsalamon
It happens to have other functions already developed in C, and this will not be possible
– DEB