Forum Discussion

Altera_Forum's avatar
Altera_Forum
Icon for Honored Contributor rankHonored Contributor
20 years ago

c and cpp living together?

Hello forum,

I am working on a project using c and c++ code with eCos. I notice that eCos is written mainly in c++, but many other components in my system is written with c. This leads me to suspect that mixing c and c++ works OK if you know how. I have no trouble running an example project written in c, but when I start to incorporate cxx code files, things start to look bad.

Before someone says, 'post to the eCos forum', my question is more fundamental - how do you go about mixing c and cxx code in the context of gcc and the NiosII IDE?

Thanks in advance for any replies.

3 Replies

  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    Hi,

    You can find much info from google.com.cn by using key words "mixing C and C++".

    when using C++ style function in *.c file, you'll need to do a "C"-style function in the CPP file in order to to link it.

    e.g. in CPP

    extern "C" void CreateForm (void)

    {

    Form *frm = new Form();

    }

    then in main.c

    extern void CreateForm (void);

    int main ()

    {

    CreateForm ()

    }

    FYI.

    David
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    They can life together well.

    Just make sure that all C++ is in a .cpp file.

    The compiler uses the file extention to change its mode