This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

CODECOMPOSER: Does CCS recognize C++ classes?

Part Number: CODECOMPOSER
Other Parts Discussed in Thread: TM4C123GH6PM

Tool/software:

I'm getting the error: "#20 identifier "class" is undefined" for this class. I'm also getting the error: "#66 expected a ";"" as well in this file. Classes are a normal part of C++. Why is something so simple not working?

Buffer.h:

#ifndef BUFFER_H_
#define BUFFER_H_


#define MAX_BUFFER_SIZE 256

class Buffer
{
private:

    char* items;
    int count;
    int size;
    int head;
    int tail;

public:

    Buffer();
    Buffer(int);
    ~Buffer();
    void wipe();
    bool isEmpty();
    bool isFull();
    char dequeue();
    bool enqueue(char);
};

#endif /* BUFFER_H_ */

  • Nick,

    I believe what is happening here is that the compiler is treating your code as C. 

    You could change the file extensions or you can tell the compiler that you are using C++ via the compiler options.

    I am not sure what device you are using so I am therefore not sure which compiler is being used.  However in general there will be something in the compiler options to control this. Here is what it looks like for the C2000 compiler in CCSv20.

    Regards,

    John

  • I enabled "Treat C files as C++ file" like you said but there is now an error in "tm4c123gh6pm_startup_ccs.c" saying: "#821 badly formed pragma." This was not an error before I enabled the C++ option. This file was generated by the project so I don't think I should change it very much.

     

  • Rename your program with main to x.cpp.

  • You're saying to rename main.c to main.cpp?

    Main.c:

    #include "clock.h"
    #include "uart.h"
    
    
    int main(void)
    {
    	Clock_Init();
    
    	UART0_Init();
    
    	while (1)
    	{
    	}
    }
    

  • The problem with this method ...

    I enabled "Treat C files as C++ file"

    ... is that it presumes all of the files with the extension .c contain C++ code.  Even the files that don't, like tm4c123gh6pm_startup_ccs.c.  

    I'm not sure of the best solution.  One solution is to not use --cpp_default, and change the file extension of all the C++ files to .cpp.  Another solution is to use --cpp_default for the project build, but use the file specific options feature of CCS to disable --cpp_default for each C file that can't use it.

    Thanks and regards,

    -George

  • My only C++ files are Buffer.h and Buffer.cpp. Would you suggest renaming Buffer.h? If I selectively enable "Treat C files as C++ Files" I still get the first error of "#20 identifier "class" is undefined" inside the header file. The problem is not in any of my C source files but rather in the header file Buffer.h.

  • The code in Buffer.h is processed just like the rest of the code in the source files which include it.  With default options, if it is included in a file with the file extension .cpp, then it is processed as C++ code. If it is included in a file with the file extension .c, then it is processed as C code.

    Thanks and regards,

    -George

  • Buffer.cpp and uart.c both include Buffer.h. If I treat uart.c as C++, I get a bunch of "unresolved symbol" errors from TivaWare Peripheral Driver Library functions I call inside uart.c. What would you have me do? Is what I'm doing simply not possible in CCS? 

  • Please see this FAQ page (not from TI) on how to mix C and C++ code.  I'm pretty sure your solution is to add lines in Buffer.h similar to ...

    #ifdef __cplusplus
    /* C++ only lines here */
    #endif

    Thanks and regards,

    -George