0

I am currently working on an embedded project for the stm32f3. In this project I want to use both .cpp and .c files: The C files are for the UART/I2C... initialisation files while the C++ ones are for a more object oriented solution. Therefore I really need to keep both of them. My question is how to make it work properly? I have seen a few answer on the web but it all seem very confusing to me.

I already had a project with uart.c/h files inside a C++ project (the board was working just fine), but as I added a i2c.c/h files to the project, a lot of linkage errors appeared.

Example = Error[Li005]: no definition for "I2C_StructInit" [referenced from C:\Users\IAR Embedded Workbench\STM32F3-Discovery_FW_V1.1.0\Project\I2C Interface CPU\Debug\Obj\i2c.o]

Should the main be a C or C++ file?

Should I use Language\Auto in the project setting, or specify for each file to compile in C or C++ ?

When do I need to use extern "C"?

I am using IAR Embedded Workbench IDE. Any other advices are welcome.

3 Answers 3

3

I'm not familiar with this platform, but the problems sound pretty general.

Should the main be a C or C++ file?

C++ would be the safer choice because main relates to program initialization, and C++ implements dynamic initialization unlike C.

When do I need to use extern "C"?

This is important, and a likely source of your problem. You need extern "C" to decorate every declaration used in C++ whose definition is provided in C.

If your C headers lack extern "C", you can in a pinch add it for them:

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

Putting a #include directive inside anything is generally a very bad idea, but this is relatively harmless because extern "C" {} doesn't introduce a new, nested declarative scope, aside from modifying the linkage of declarations contained within.

As Mine suggests, you could also modify the header files to contain extern "C" when compiling in C++ mode. My advice is based on the idea that modifying a library is the greater evil. In my experience, embedded drivers may be dynamically generated for the platform, or in any case you might move later to a slightly different platform. You should use the idiom in that answer for your own headers, but personally I'd avoid editing i2c.h at all costs.

3

In your .h files, you should probably write like this:

#ifdef __cplusplus
extern "C" {
#endif

xxxxx // Your header file content

#ifdef __cplusplus
}
#endif

Then you can include such .h files in both .c and .cpp code without problem.

Note the trick is, all these functions are without c++ decorations, so both c and c++ see the same function names.

3
  • If I understand correctly all my .h files should be included in this frame right? It doesn't matter if I declare a C++ functions inside extern "C" {} ? Dec 10, 2013 at 8:27
  • @AntoineAndrieu Only C declarations should go inside extern C block.
    – user694733
    Dec 10, 2013 at 8:36
  • 1
    @AntoineAndrieu Yes, because #ifdef __cplusplus makes sure only c++ sees the extern "C", so you can write all .h files this way.
    – Mine
    Dec 10, 2013 at 9:51
2

About the IAR specific stuff:

Language\Auto makes compiler determine language from file extension .c/.cpp. Be aware though that IAR (5.4 at least) only supports subset of C++, and not all features are available.

For everything else you asked: see Potatoswatters answer about the extern and main.

2
  • Should I therefore use language\C++ ? Dec 10, 2013 at 8:25
  • 1
    @AntoineAndrieu No, if you use both languages, then leave it on Auto and use correct file extensions to select language for the file.
    – user694733
    Dec 10, 2013 at 8:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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