I am a very inexperienced programmer, so apologies if this is a stupid question to be asking. And similarly, if anyone answering could assume that I know basically nothing at all and treat me like an idiot, that'd be great...

I have some code in C++ that is an interface to a physical device. Importantly, part of this is writing images to the device by assigning pixel values for a 512x512 array of pixels, with these values stored using the C++ vector class. The values are written into this by using a pointer that is incremented, and dereferencing the pointer to assign the value.

I also have some code that I've written in C, that calculates what image to actually write. (The code uses Fourier transforms, for which I've used the FFTW library). What I want to do is this: every time in the C++ code I want to write an image to the device, I want to call this C code to calculate what image to write, and get the output of the C code into the C++ code.

My C data has the following form (if I've understood correctly): I have a pointer of type fftw_complex and what I presume is a 1D array of size 512x512x2 (as s=512, and fftw_complex must have size 2 as it's for storing a complex number). In case I've got that wrong, here's the declaration / memory allocation:

fftw_complex *slmPlane slmPlane = (fftw_complex*) fftw_malloc(s*s * sizeof(fftw_complex));

My C code is not C++ compatible. I have arrays defined with sizes given by variables, and there are a lot of issues. I don't have the experience, or really the time, to make this code compile and run with C++. I've seen a lot of articles on calling individual C functions from C++... but what I want to do is call a whole separate bit of code, which is a main function plus about 50 others. And I don't want to have to worry about making the C code C++ compatible.

Ultimately, then, what I need to know is:

  1. Is there an easy way to call a separate piece of code with .c extension, that's not C++ compatible, from withing a C++ (.cpp file) script?

  2. How can I get the output of the C code (a 1D array of length 512x512x2) into the C++ code (where it will be assigned to elements of a vector)?

  3. As a bonus - any issues with linking different libraries from each of these two scripts? (I'm using C libraries that might not be C++ compatible, and vice versa..)

  4. If I'm going about all of this totally the wrong way, could anyone point me in a better direction instead?

  • Try reading this: stackoverflow.com/questions/16850992/… – candied_orange May 31 '16 at 11:28
  • ...which is a main function plus about 50 others. If your C code is a standalone executable, and can output to stdout or an arbitrary file, you could simply fork and exec (or spawn) it from the C++ code, and have the C++ code capture the output or read the file. – 8bittree Jun 1 '16 at 13:19

Is it a way for you, to put the C program in a separate library? I think, that should be the easiest solution.

Another possibility: can you compile your C code with C++ compiler if you put the C-Code inside "extern C"? Like that:

#ifdef __cplusplus
extern "C" {
int _numOfYearDay(int jahr, int monat, int tag)
    int d, e;
    d = (monat+10)/13;
    e = tag + ((611*(monat+2))/20) - (2*d) - 91;
    if(intern_leapYear( jahr ))
        e += d;
    return e;
#ifdef __cplusplus

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.