Sensor library (OOP)

Technical questions regarding the XTC tools and programming with XMOS.
extremeRider
Member++
Posts: 19
Joined: Tue Jun 16, 2015 10:50 pm

Sensor library (OOP)

Post by extremeRider »

Hi,
from a previous work, I have a library of classes for managing different type of sensors.
I would like to re-use this library on an XMOS project, but as far as I understood, there is no support for OOP in XC. Is this right?
I was thinking about writing wrapper functions for connecting the xc code to the cpp code in order to solve the problem but it seems that XC doesn't recognize classes at all:

Code: Select all

10:16:58 **** Incremental Build of configuration Default for project Lab10-CLASSES ****
xmake CONFIG=Default all 
Checking build modules
No build modules used.
Analyzing Lab10-CLASSES.xc
In file included from ../src/Lab10-CLASSES.xc:14:
../src\Sensor.h:1:1: warning: type defaults to `int' in declaration of `class'
class Sensor {
^~~~~
../src\Sensor.h:1:1: warning: data definition has no type or storage class
class Sensor {
^~~~~
../src\Sensor.h:1:7: error: parse error before "Sensor"
class Sensor {
      ^
../src\Sensor.h:1:7: error: invalid declarator for function declaration
class Sensor {
      ^~~~~~
../src\Sensor.h:1:7: warning: return type defaults to `int'
class Sensor {
      ^~~~~~
../src\Sensor.h:3:1: error: use of undeclared identifer `public'
public:
^~~~~~
../src\Sensor.h:4:2: error: use of undeclared identifer `bool'
        bool isEnabled;
        ^~~~
../src\Sensor.h:4:7: error: parse error before "isEnabled"
        bool isEnabled;
             ^
../src\Sensor.h:19:2: warning: type defaults to `int' in declaration of `bool'
        bool _isEnabled();
        ^~~~
../src\Sensor.h:19:2: warning: data definition has no type or storage class
        bool _isEnabled();
        ^~~~
../src\Sensor.h:19:7: error: parse error before "_isEnabled"
        bool _isEnabled();
             ^
../src\Sensor.h:19:7: warning: return type defaults to `int'
        bool _isEnabled();
             ^~~~~~~~~~~~
../src\Sensor.h:20:1: error: parse error before '}' token
};
^
../src\Sensor.h:19:2: warning: unused variable `bool'
        bool _isEnabled();
        ^~~~
xmake[1]: *** [.build/src/Lab10-CLASSES.xc.pca.xml] Error 1
xmake: *** [analyze] Error 2

10:16:59 Build Finished (took 1s.505ms)

You can find attached the test project I'm working on.

Is there a way to fix the problem or should I find another way?
You do not have the required permissions to view the files attached to this post.


richard
Respected Member
Posts: 318
Joined: Tue Dec 15, 2009 12:46 am

Post by richard »

Calling C++ from xC is much the same as calling C++ from C. You'll need to write wrapper functions that are implemented in C++ that have C linkage (i.e. are tagged with extern "C"). For more details see:

https://isocpp.org/wiki/faq/mixing-c-and-cpp

There's an simple example which calls between C++ and xC on github.

https://github.com/rlsosborne/app_calling_cxx_from_xc

You might also want to take a look at the following post:

https://www.xcore.com/forum/viewtopic.php?f=26&t=3017
Guest

Post by Guest »

Worth adding this info in the Q&A section for the benefit of other users
extremeRider
Member++
Posts: 19
Joined: Tue Jun 16, 2015 10:50 pm

Post by extremeRider »

Ok,
thanks! I'll take a look on the links that you provided.

@jangala: I don't know how to move the thread to Q&A. So, feel free to move it if you can. Thanks.