I put a simple class in a file named "test.h". The class is as follows:
class a { public: int b; };
I am using the Keil MCBSTM32 board (STM32F103RB processor).
I place this file in "C:\Keil\ARM\Boards\Keil\MCBSTM32\Timer". I then open the timer project.
If I include the file in timer.c I get the following error:
Build target 'MCBSTM32' compiling Timer.c... test.h(2): error: #20: identifier "class" is undefined test.h(2): error: #65: expected a ";" Target not created
Why does it give me an error? I thought the compiler ssuported C++?
Many compilers look at the file type (extension) to decide whether they should be treated as C++ or ANSI 'C'
Commonly, .c is taken to mean ANSI 'C' - so you would expect to get errors from C++ stuff...
What does the Manual say?
C++ is supported by the compiler.
Take a look at the File naming convention: http://www.keil.com/support/man/docs/armcc/armcc_babbjcee.htm
There are also some examples that use C++ in the MDK installation: Keil\ARM\Examples\C++
you probably need to instruct the compiler to switch to C++ mode by the compiler option
--cpp
but, this is indeed the default for .cpp file. did you mix things up?
He isn't using a *.cpp file. The source file in question is *.c, which defaults to compiling as C.
As far as I could determine the Manual or nothing I was able to locate with a search in this forum said anything on C++ compilation, therefore the question. I did not realize that there was a possibility that the extension had anything to do with the compilers ability to recognise C++. I have worked with compilers where you could intermix C and C++ in ".c" file. Most of the time I have worked exclusively in either C or C++. I must admit, I have never tried this particular scenario.
<sigh> I am not trying to be curt, but if I have any information on any subject, I am happy to share it. Some of us are pressured to be productive on day one and any delay will put us closer to the unemployment line. I am sorry if I offend some people by asking questions that may be in a manual. Before asking I do look to see if I can find an answer on my own. I ask publicly when I have reached a point where I feel I have spent too much time searching.
Thank you for the reference. I was looking for the compiler manual and somehow never came across it. This will definitely help me move forward.
Most compilers make initial decision of C or C++ based on extension.
If you use the gcc front-end you will get the same result. If you use the g++ front-end, then your front-end choice specifies C++ compilation.
It is important to note that not all compilers even have a switch to override selected language. Several compilers only allows the file extension to select between C and C++ so it is always best to use the "normal" extensions when programming in C or C++.
I did not realize that there was a possibility that the extension had anything to do with the compilers ability to recognise C++.
As already pointed out, often a toolchain will make the decision whether to compile code as 'C' or 'C++' based on the extension as a default. I'm not familiar with the ARM toolchain, but you may well find that there is an option (probably available in the project options as well as a command line switch) to select 'C' or 'C++' compilation on a per-file basis, irrespective of extension.
but you may well find that there is an option (probably available in the project options as well as a command line switch)
Absolutely. As mentioned before, the
-cpp
and
-c90
compiler switches for the RealView compiler.