2

I am trying to control a drone using image processing. The image capture from the drone is an opencv C++ program. The drone control program is C program. Now I want to integrate both the programs so that I run a single program by which I can get both images and control the drone. How do I do that? My C program is a socket program to send commands to drone and the C++ program to capture image stream from drone from a tcp socket. Is there any way to do it or should I convert one whole program into another language? Anyone help.

regards, shiksha

wireless_lab
  • 177
  • 1
  • 4
  • 18
  • 1
    You'll probably need to either merge both programs into one program in one language or turn one (or both) of the programs into a DLL. – cf- Mar 10 '14 at 03:45
  • 1
    what OS, Environnement ? – Joe DF Mar 10 '14 at 03:46
  • 1
    I would compile the C program as a static lib and link it to the C++ code. To access C++ code from C, use extern "C" in the header. Don't allow any unhandled exceptions from C++ functions callable from C. You can also probably just port the C code to C++ with little effort. – VoidStar Mar 10 '14 at 03:49
  • i am working on linux platform – wireless_lab Mar 10 '14 at 05:48
  • Why do you need a single program? At what point do these two independent pieces of functionality need to communicate? – CB Bailey Mar 10 '14 at 06:32
  • i want the image grabbing an control parameters in a loop. the control parameters are fed back to stabilise the system. so i want both the programs to run in the same loop. – wireless_lab Mar 10 '14 at 11:03

2 Answers2

3

You have a number of options. The easiest may be to simply rename your *.c files to *.cpp - they may compile directly in C++. You may then have some issues linking with external libraries that were built using a C compiler. These linking issues can often be resolved by guarding each header include with

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

If this isn't feasible (e.g. if your C project consists of a lot of files, or you want to keep compiling in C), you may want to build the C part as a static library and link it with your C++ program. You could go in the other direction - build your C++ component as a static library and link it with the C program. The downside to this is that you would have to write a C wrapper for your C++ code which could take some effort (or be impossible, depending on which features of C++ you use in your API). Care must be taken to ensure the wrapper is linked as C so that the symbols will be compatible with what C produces.

Chris McGrath
  • 1,936
  • 16
  • 17
  • System libraries generally are already protected like that. – vonbrand Mar 10 '14 at 04:02
  • That is true, but user-written libraries aren't always protected. It's something to be mindful of nonetheless. Particularly if his code is being run on an embedded system with sloppily written headers. – Chris McGrath Mar 10 '14 at 04:05
  • thanks for the reply. i converted my c code to cpp program. it executes fine. but when i combine both the programs it is giving a lot of errors like template with c linkage – wireless_lab Mar 10 '14 at 05:44
  • That sounds like you're trying to compile some code which uses the C++ feature templates but are telling the compiler to use C linkage with the extern "C" trick. This post discusses it: http://stackoverflow.com/questions/4877705/why-cant-templates-be-within-extern-c-blocks - I'd suggest seeing if you can avoid that feature in the header. – Chris McGrath Mar 10 '14 at 06:05
2

Using C libs in C++ programs is a common way to reuse the functionality. These languages can be built together. Usually C++ code calls C- module API which provide required functions.

In such case you can just add C files into the C++ project and build a single executable.

You only should consider the different linkage in C and C++. C function declarations should be compiled with extern "C" standard linkage specification. C-libraries usually already have such code in the headers (if not - add it):

#ifdef __cplusplus
    extern "C" { 
#endif

and in the end of the file

#ifdef __cplusplus
    }
#endif

extern "C" prevents changing function names by C++ compiler. As C++ supports overloading, C++ compiler adds information about function parameters into the function name, so called name mangling. This should not be done for C-library code.

Spock77
  • 3,256
  • 2
  • 30
  • 39