Mixing C and C++ MFC

Hello everyone!

I am working on this project where I have to implement a decoder/video player. I have a decoder project written in raw C. It takes an encoded file as input and generates a decoded file. I have another project, a video player, which is an MFC implemented in C++. What I have to do is incorporate these two projects together. I have to decode a single frame and play it on the spot. Anyways, this introduction is just to get you guys in the mood.

I already read some articles about mixing C and C++. I am still a newbie in Visual C++, but I pretty much understand whats is a MFC and how it works.

Well i managed to fix all the syntax problems that may occur when trying to mix c and c++ files in the same. All i had to do is keep the C files in their .c extension so that the compiler recognizes that it is a c file and compile it accordingly. Other problems had to be fixed like specifying that there is no precompiled header for the C files...etc Now, I know that whenever I want to call a c function from a c++ file, i have to use the extern "C" construct. The only errors I left with are linkage errors such as:

YUVviewer error LNK2019: unresolved external symbol _ue_v referenced in function _FirstPartOfSliceHeader

What does that mean... i know that it has to do with compiler trying to link to C libraries and not being able to... i already tried entering all the required C libraries into the Linker's command line, but still same errors...Any ideas

If not, am i being impossible! Should i follow a different approach, like compiling the C code by itself, generating a .dll and using that in my MFC project

Thanks



Answer this question

Mixing C and C++ MFC

  • Dave of SJNB

    Well hello again. thanks Ben i already fixed those problems.

    At first i was asking to call C functions from my MFC classes, and I managed to do that but using the extern "C" to wrap the declaration of the function in the header file. Now I wish to do the opposite. I have a CMemFile defined in the header file of the main MFC class, and I want to write to it from one of my C functions. I already included the MFC header file in the C file im writing from, now what, do i use the extern "c" contruct too

    Thanks,

    aHAB


  • wdhough

    What you're asking is to use a C++ class in a C program, which of course won't work, C++ is more or less backwards compatible with C, but not the other way around.

    However, you should be able to simply compile the C code as C++ and then you should be alright. Once you have done this you can use extern "c" and other methods of inclusion.

    -Ben



  • TooTallSid

    yea hello again. I already tried this and it works without any compilation errors.

    char *argv = inSeqName[m_iCount];

    char **argiv = &argv;

    ldecod(1, argiv);

    But i am wondering if i messed up some addresses on the way or did some mistake that it is not visible on the compilation level but might create some problems in run-time. Any comments

    Thanks!


  • Sahikon

    hello everyone,

    I am currently working on trying to implement a function in C++ wrapped by extern "C" so that i can call it from my C functions. I am doing this becasue I am trying to write to an MFC object... and since c and c++ are od fifferent syntaxes, i thought the ebst way to go around this is to cast a void pointer to my MFC object and then pass this pointer to my function. Here is my code:

    For the c++ header file that includes the MFC object:
    CFile *m_pFile[36];

    For another header file that is going to be included by my c and c++ files that contians the void pointer:

    void *lp_m_pFile;

    Now inside the MFC onOpenFile() function here is the code for initializing the MFC object and casting a void pointer to it:

    m_pFile[m_iCount] = new CFile();
    lp_m_pFile = (void*) &m_pFile[m_iCount];

    Finally this is the function I am working on:

    extern "C" void writetoMFCObject(char* buff, void* voidPointer){


    voidPointer.Write(buff, 4);

    }

    how do i proceed... i dont think the above syntax is right... if it is the correct syntax, can someone tell me if this work.. i mean will i able to write to the mfc object by using this void pointer from C

    Thanks in advance,
    aHAB

  • mike in nyc

    That's one way. My advice, abandon C as an obsolete language and stick to C++ which is far newer and is a superset of C so here what I suggest. Any Legacy C code can be called using the "extern C" declaration, and you can cut and paste C methods into C++ classes fairly readily. Best is to rewrite the code properly.

    In C data containers are called "struct" and by defaut everything is public. In C++ data containers are called "class" and everthing is private by default. This is the main feature of C++.

    Error handlers are different too meaning its material work to convert many C programs to be compatible with C++ error handlers which have a different way to do things.

    Namepaces are exclusive to C++ there is no direct equivalent in C at all. Namespaces make it possible for dramaticallt larger projects than can be implemented in C.

    Input/Output is different too. C uses "printf" while C++ uses "cout" etc. but C++ supports printf statements when the old headers are inlcuded. We recommend using the C++ methods for new projects.

    Generally, C is suitable for a program up to 15-20 thousand lines before language limitations really start making the project difficult. C++ with namespaces and templates can manage a far more complicated project easily (look at the bloat-itus of software these days).



  • MIkey1969

    The linker error you reference is trying to tell you that in the function _FirstPartOfSliceHeader there is a variable or function call to something called _ue_v and that it is not declared in the same file or any of the files you are currently linking to. You need to find out where _ue_v lives and make sure you are either compiling that in your app, or linking to it.

    Also, you should not run into any problems compiling C code as C++ in most circumstances. You will almost definitely run into problems trying to compile C++ code as C code. So I would recommend that you compile all your code as C++ code. Also, instead of declaring "extern" variables there's probably a header (.h) file you can include in the files you want to reference other variables.

    Writing something as complicated as a video player in C/C++ without knowing C/C++ is probably a pretty tough task. You would be very well served by picking up a few books on C/C++ programming and trying to learn the language before jumping right in.

    Thanks,

    Ben



  • Ade Morgan

    When the compiler sees a *.c file it assume C syntax, and *.cpp files for c++ Programs.

    You can call C++ and C methods etc from C++ program no problem, but you need to save your program as a *.cpp file from now on.


  • Mensana

    C++ will compile C code correctly it accepts the C code without any problems. This is the experience of someone with near to 8 years of C++ programming.

    Yes there are some differences in the syntax but you find that it is due to things added in C++ and also a few things that are required in C are actually optional in C++.

    An example is structures.

    struct ExampleStruct

    {

    int a;

    int b;

    }

    struct ExampleStruct eStruct;

    This above code will complile in both C and C++ with no problems. That is because in C++ the struct keyword before declaring a variable of type struct is optional. There are things like these added to C++ that makes it compatible with C.



  • billmill

    I didnt get it when you said "compile the C code as C++". I understand that visual looks at the extension (.c for instance) and will compile it accordingly (c code). I already did that, compiled everything, everything is set now. I am already at liberty of calling my c function from my MFC classes simply because I included a header in my c++ file having the declarations of the c functions wrapped up by extern "C" consturct.

    Now what I am insterested in is writing to my MFC objects from my c functions. I know i can nto just simply include the c++ header file in my c files, becuase syntax is incompatible , so now i am thinking of putting C wrapper functions around the MemFile object, and make C manipulate the object using those functions. Will this work, if yes, can you please give me an example

    Thanks,

    aHAB


  • wayneacton

    Dont worry, you can migrate easily from C style to C++ classes etc.

  • James Walters

    You cannot just do that!

    You cant just compile c code as c++ code, they are different syntaxes, not compatible...

    Leaving the compiler to default will compile each file according to its extension, which is fine, you can call c functions from c++ files, by using the extern "C" construct, which is also working... now my only prolem is what ive explained before: writing to a c++ object from ur c functions... i know i cant just include header files like i did initially cuz i cant mix the codes (different syntaxes) but i know there is a way around it... which im not really sure of...


  • vasko

    You can use the /Tp compiler option to interpret a *.c file as C++ file.


  • CleverPete

    Well thanks Ben. I already fixed the problems and I am at ease of calling my C functions from my C++ functions. Hello again. Well nevermind the last question, it works now...

    I am havign another problem now. I have a main function in my decoder C file. I changed it to a regular function:

    int ldecod(int argc, char **argv)

    As i said, originally its a main function, it takes arguments from the console. Now I need to pass these arguments from another function. Its an MFC function, onOpenFile(); it is specifies what files to take as valid. It places the valid files and puts it in a CFile array:

    m_pFile[m_iCount] = new CFile();

    char BASED_CODE szFilter[] = "YUV Files (*.yuv)|*.yuv|All Files
    (*.*)
    |*.*||";
    CFileDialog dlg( TRUE, "yuv", NULL, OFN_HIDEREADONLY,szFilter);
    if(dlg.DoModal()!=IDOK) return;

    sprintf( inSeqence[m_iCount], "%s", dlg.GetPathName() );
    getSeqName(inSeqence[m_iCount], inSeqName[m_iCount]);


    then gets the Seqname of the file from its pathname using:

    getSeqName(inSeqence[m_iCount], inSeqName[m_iCount]);

    So inSeqName[m_iCount] si the name of the file I am looking at. It is defined as:

    char inSeqName[36][64];

    How can I pass this filename as the argv of the ldecod function I alreayd tried this:

    ldecod(1, inSeqName[m_iCount]);

    And it is giving me this error:

    error C2664: 'ldecod' : cannot convert parameter 2 from 'char [64]'
    to 'char ** '

    How can i convert from the array type to a double pointer char

    Thanks in advance!

  • Najra

    You have to be cautious with pointers.

  • Mixing C and C++ MFC