How to use Class in XC? Topic is solved

If you have a simple question and just want an answer.
TMentink
Junior Member
Posts: 7
Joined: Sat Apr 19, 2014 7:52 pm

How to use Class in XC?

Post by TMentink »

I created a class named SPI where i wanted to make an spi class that handels all the spi interfaces.after creating the class i got multiple wierd errors.class spi {public: spi(); virtual ~spi();};I get the following errors:spi.h:24: warning: type defaults to `int' in declaration of `class'spi.h:24: warning: data definition has no type or storage classspi.h:24: error: parse error before "spi"In file included from ../src/main.xc:10:spi.h:24: error: invalid declarator for function declarationspi.h:24: warning: return type defaults to `int'spi.h: In function `spi':spi.h:25: error: `public' undeclared (first use in this function)spi.h:25: error: (Each undeclared identifier is reported only oncespi.h:25: error: for each function it appears in.)spi.h:26: error: parse error before ';' tokenspi.h:27: warning: type defaults to `int' in declaration of `virtual'spi.h:27: warning: data definition has no type or storage classspi.h:27: error: parse error before '~' tokenspi.h:27: warning: return type defaults to `int'spi.h: At top level:spi.h:28: error: parse error before '}' token../src/main.xc:13: error: local variable `spi_if' has type that contains a port../src/main.xc: In function `main':../src/main.xc:24: error: `SPI' undeclared (first use in this function)../src/main.xc:24: error: parse error before "SPI1"../src/main.xc:26: error: parse error before "return"../src/main.xc:27: error: parse error before '}' tokenThanks in advance


View Solution
User avatar
ers35
Active Member
Posts: 62
Joined: Mon Jun 10, 2013 2:14 pm

Post by ers35 »

XC is not compatible with C++ classes. Declare functions with extern "C" in C++ files and call them from XC.

For example:

// xcc -target=XS1-L16A-128-QF124-C10 spi.xc spi.cpp -o spi.xe

// spi.xc
void spi_cpp();
int main()
{
  spi_cpp();
  return 0;
}

// spi.cpp
#include <print.h>
extern "C" void spi_cpp()
{
  printstrln("Hello from C++!");
}