combining XC and C++

Technical questions regarding the xTIMEcomposer, xSOFTip Explorer and Programming with XMOS.
User avatar
segher
XCore Expert
Posts: 843
Joined: Sun Jul 11, 2010 1:31 am
Contact:

Re: combining XC and C++

Postby segher » Thu May 03, 2012 1:29 am

yzoer wrote:FWIW, this kind of stuff should be a no-brainer to get this to work. Anyone new to XMOS and familiar with C/C++ is going to run into this problem and shouldn't have to go anywhere (i.e. here) to find answers..

Anyone familiar with C++ knows you cannot call a C++ function from C (or the
other way around) without doing exactly the same stuff! (i.e., tell the C++
compiler to use the C ABI for those functions).

The C and XC ABIs are almost identical.
yzoer
XCore Addict
Posts: 133
Joined: Tue Dec 15, 2009 10:23 pm

Postby yzoer » Thu May 03, 2012 3:58 am

Yes, but that's not the point I was trying to make...

Anyone new to XMOS development will most likely have a C/C++/Assembler background and will be familiar with gcc/VC++ or what have you. The fact that something as trivial as this doesn't work out of the box shows the tools are still immature and need work.

-Y
yzoer
XCore Addict
Posts: 133
Joined: Tue Dec 15, 2009 10:23 pm

Postby yzoer » Thu May 03, 2012 4:15 am

Alright, more fun.. So calling a single function that doesn't call anything else works fine. However, calling a function that calls anything from within that function results in:

../src/main.xc:(.text+0x32): Error: Meta information ("run.nstackwords") for function "run" cannot be determined.
../src/main.xc:(.text+0x32): Error: lower bound could not be calculated (function is recursive?).
../src/main.xc:(.text+0x36): Error: Meta information ("run.nstackwords") for function "run" cannot be determined.
../src/main.xc:(.text+0x36): Error: lower bound could not be calculated (function is recursive?).
../src/main.xc:(.text+0x42): Error: Meta information ("run.nstackwords") for function "run" cannot be determined.
../src/main.xc:(.text+0x42): Error: lower bound could not be calculated (function is recursive?).
../src/main.xc:(.text+0x46): Error: Meta information ("run.nstackwords") for function "run" cannot be determined.
../src/main.xc:(.text+0x46): Error: lower bound could not be calculated (function is recursive?).

which looks like it's missing information about the depth of the C stackframe. Easy to do in an assembler file, not so much in a C file, or is it?

-Y
yzoer
XCore Addict
Posts: 133
Joined: Tue Dec 15, 2009 10:23 pm

Postby yzoer » Thu May 03, 2012 4:33 am

Aaaaand I figured it out by sleuthing through the forum again. For future reference, you can allocate extra stack-space ( necessary for calling other functions / local variables ) using:

#pragma stackfunction [size]

Meanwhile, back at the farm....

-Y
User avatar
segher
XCore Expert
Posts: 843
Joined: Sun Jul 11, 2010 1:31 am
Contact:

Postby segher » Thu May 03, 2012 9:30 am

yzoer wrote:Yes, but that's not the point I was trying to make...

So what was the point?

Anyone new to XMOS development will most likely have a C/C++/Assembler background and will be familiar with gcc/VC++ or what have you. The fact that something as trivial as this doesn't work out of the box shows the tools are still immature and need work.

A program with an error as trivial as this does not work on those other toolchains
either, failing in quite the same way.
User avatar
segher
XCore Expert
Posts: 843
Joined: Sun Jul 11, 2010 1:31 am
Contact:

Postby segher » Thu May 03, 2012 9:41 am

yzoer wrote:Aaaaand I figured it out by sleuthing through the forum again.

You didn't find it in the documentation? The usual complaint about the docs is
that there isn't nearly enough of it, but the good thing about that is that you can
read all of it in an afternoon (or a few perhaps).

Maybe you want a guide for people in your exact situation (using C++ code
in an XC project). You could write one, there's a wiki for that :-)

For future reference, you can allocate extra stack-space ( necessary for calling other functions / local variables ) using:

#pragma stackfunction [size]

That tells the checker that this function uses [size] stack words. For better
control over the "extra" space, see #pragma stackcalls.

The probable reason the checker needs this for your program is you did an
indirect call, and the checker cannot resolve the target for that.
richard
Respected Member
Posts: 318
Joined: Tue Dec 15, 2009 12:46 am

Postby richard » Thu May 03, 2012 11:33 am

segher wrote:Also, you might need to link using the C++ compiler, not the XC compiler.
You didn't tell us what commands you used (hint hint), so that's about as detailed
as I can go ;-)

With the XMOS tools you should always use xcc. This links in all the necessary libraries by default and works equally well for XC / C / C++ programs.

Gravis wrote:I've made a header file to make function definition compatible for XC, C++ and C with ease. Using this header will allow you to call functions from XC, C++ and C.


Another way to make C / XC header file callable from C++ is too put the following at the top of the header:

Code: Select all

#ifdef __cplusplus
extern "C" {
#endif


And then the following at the bottom of the header:

Code: Select all

#ifdef __cplusplus
}
#endif


See http://www.parashift.com/c++-faq-lite/mixing-c-and-cpp.html#faq-32.4. This is the pattern that is used in all the standard headers included with the tools to allow the functions declare there to be called from C++.
richard
Respected Member
Posts: 318
Joined: Tue Dec 15, 2009 12:46 am

Postby richard » Thu May 03, 2012 11:59 am

yzoer wrote:Only difference I can spot right now is that my file is a .cpp and yours is .cc, which might call a different linker?

xcc treats files with the following extensions as c++ files:

    .cc
    .cp
    .c++
    .C
    .cxx

However it appears that for XDE projects (which use the xcommon build system) c++ source files must have have the extension .cpp (.cc files will be ignored). Maybe this contributed to the problems you were having in the XDE? I've filed a bug about this:

https://github.com/xcore/xcommon/issues/12

Until this is fixed you'll need to use the .cpp extension if you are building XDE projects.
yzoer
XCore Addict
Posts: 133
Joined: Tue Dec 15, 2009 10:23 pm

Postby yzoer » Thu May 03, 2012 2:28 pm

Thanks for all the help guys!

Segher, my point was that I shouldn't have to dig through documentation and post on forums to fix, as you stated yourself, a simple error. Yes, mixing languages inherently brings problems but it's an essential part of XMOS development as people will bring legacy code with them and don't want to spend time rewriting them in XC.

Anyway, I'll get off my soapbox... Again, thanks to all of you who contributed!

-Y
User avatar
segher
XCore Expert
Posts: 843
Joined: Sun Jul 11, 2010 1:31 am
Contact:

Postby segher » Thu May 03, 2012 5:19 pm

yzoer wrote:Segher, my point was that I shouldn't have to dig through documentation and post on forums to fix, as you stated yourself, a simple error.

If you do neither, should the problem fix itself perhaps? :-)

I think suggestions on how the documentation could be improved (so it is easier
to find what you are looking for, for example) will be welcome.

Who is online

Users browsing this forum: No registered users and 71 guests