Author Topic: Mix of C and Cpp  (Read 11008 times)

Offline eckard_klotz

  • Almost regular
  • **
  • Posts: 194
Mix of C and Cpp
« on: July 31, 2012, 02:59:13 pm »
Hello All.

My original project is written in c. Normaly I use Microchip MPLAB for compiling and CodeBlocks just to edit the sources. Thus I had never problems in the past. But now I started to use UnitTest++ (like described by Killerbot in his tutorial) and this is written in  c++.

Originaly all my sources have the attachment ".c" and in the advanced file-property a "CC" is set. The new sources used to define the tests are written in c++ have the attachment ".cpp" and the advanced file-property a "CPP".
If I build this with the default GNU-MINGW compiler (gcc) all sources will be compiled. But the linker is not able to use the content of the c-objects.

So I have to change the advanced file-property from "CC" to "CPP". Then the c-files will be compiled as c++ files and the linker is able to use the object-files. But for some reasons the compiler finds in the c++ mode some errors wich will not be seen in the c-mode. At the end it may be a good idea to solve those problems by changing the problematic parts of the sources but since this project is an older one I have to do this step by step to be able to check if the target-system (a dsPIC microcontroler )is still working as before or has with some of those changes problems.

My question is now if it is possible to keep the advanced file-property a "CC" for the c-sources so that they are compiled in c-mode but to tell the linker to use this together with the test-file objects created from c++ sources. Is there a possibility to configure this in the build-options?

Best regards,
             Eckard Klotz.

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 13413
    • Travis build status
Re: Mix of C and Cpp
« Reply #1 on: July 31, 2012, 03:22:29 pm »
One way to fix this problems is to make your c header file c++ compatible, by using extern "C" if __cplusplus is defined.

See here for example: http://developers.sun.com/solaris/articles/mixing.html
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

Offline eckard_klotz

  • Almost regular
  • **
  • Posts: 194
Re: Mix of C and Cpp
« Reply #2 on: July 31, 2012, 04:00:50 pm »
Hello.

Thank You for the fast reply. It was great help especialy this:
Quote
extern "C" {
    #include "header.h"
}

Once I used this aruond my c-header includes in my "test.cpp" I was able to configure the advanced file-property back to "CC" again and it works.

Best Regards,
                   Eckard.