X
RSS YouTube LinkedIn Twitter XCore IRC

Search




Post new topic Reply to topic   [ 21 posts ]  Go to page 1, 2, 3  Next
Author Message
PostPosted: Sun Apr 22, 2012 1:45 am 
User avatar
Experienced Member
Joined: Thu Feb 02, 2012 3:32 pm
Posts: 69
the problem i'm having is that when i call a function from XC, it follows the the includes thus trying to parse all of my code, most of which is quite heavy in C++. the C++ capabilities in XC highly limiting and thus it is unusable for my purposes.

is there a way to do use something like extern in XC and then define it with C?

alternatively, is there a way to use straight C/C++ and still do stuff like threading and channel/port io?

i know it's possible to make functions that use inline assembly to do the dirty work but i was hoping this had already been done as the asm resource system is alien to me.

please help. :(


Top
Offline Profile View all posts by this user  
 
PostPosted: Mon Apr 23, 2012 3:02 pm 
User avatar
Experienced Member
Joined: Thu Feb 02, 2012 3:32 pm
Posts: 69
i figured it out. :D

here's how you do it:

in your .xc file declare the function you want to use as extern and in a different header you declare it as extern "C" and then actually define the function in a .cpp file

EXAMPLE:

file main.xc
Code:
extern void myfunc(void);
void main(void)
{
  myfunc();
}


file enternfuncs.h
Code:
extern "C" void myfunc(void);


file enternfuncs.cpp
Code:
void myfunc(void)
{
  // your code here!
}


that's it. main.xc will be compiled in XC mode while enternfuncs.cpp will be compiled in C/C++ mode. this is good because now in the function definition you can use all the C++ you want, templates, inheritance, etc.

if you want, you can get rid of the redundancy by using some macros like this code that does the same thing:

file main.xc
Code:
#include "enternfuncs.h"
void main(void)
{
  myfunc();
}

file enternfuncs.h
Code:
#ifdef __XC__
#define EXTERNAL extern
#else
#define EXTERNAL extern "C"
#endif

EXTERNAL void myfunc(void);

file enternfuncs.cpp
Code:
void myfunc(void)
{
  // your code here!
}


Last edited by Gravis on Tue Apr 24, 2012 8:45 pm, edited 1 time in total.

Top
Offline Profile View all posts by this user  
 
PostPosted: Mon Apr 23, 2012 6:57 pm 
User avatar
XCore Expert
Joined: Sun Jul 11, 2010 1:31 am
Posts: 675
Code:
extern "C"

is not valid C code, it is C++ only. For C, try
Code:
#include <xs1compat.h>

before including your XC headers; and for C++, that would be
Code:
extern "C" {
#include <xs1compat.h>
}


Top
Offline Profile View all posts by this user  
 
PostPosted: Wed May 02, 2012 6:14 am 
Joined: Tue Dec 15, 2009 10:23 pm
Posts: 108
Howdy!

So that (still) doesn't work for me as I get:

../src/main.xc: Error: L00067 Undefined reference to 'test'

what am I doing wrong??? All I have is a single function called 'test', which is nothing more than:

Code:
#include "test.h"
void test (void)
{
}


called from main.xc as:

Code:
#include "test.h"
void main ()
{
    test ();
}


where test.h contains:

Code:
extern void test (void);


both modules compile fine but for some reason the linker doesn't see it...

Rather odd and annoying that...

-Y


Top
Offline Profile View all posts by this user  
 
PostPosted: Wed May 02, 2012 9:29 am 
User avatar
XCore Expert
Joined: Sun Jul 11, 2010 1:31 am
Posts: 675
Your test.cc should be like:
Code:
extern "C" {
#include "test.h"
}

void test (void)
{
}


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 ;-)


Top
Offline Profile View all posts by this user  
 
PostPosted: Wed May 02, 2012 1:55 pm 
Joined: Tue Dec 15, 2009 10:23 pm
Posts: 108
I've tried using 'extern "C" ' but it barfs on the "C" part, so somewhere something's wrong. Note that this is using a brand-new project-from-template in the XDE, so settings weren't messed with *at all* :roll:

-Y

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


Top
Offline Profile View all posts by this user  
 
PostPosted: Wed May 02, 2012 2:47 pm 
User avatar
Experienced Member
Joined: Thu Feb 02, 2012 3:32 pm
Posts: 69
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.

the file is here: http://pastebin.com/raw.php?i=LxRuvp9q
or you can just copy this
Code:
#ifndef GLOBAL_H
#define GLOBAL_H

#include <platform.h>
#include <xs1.h>

#ifdef __XC__
# define EXTERNAL extern
#else
# include <xccompat.h>
typedef chanend chan;
# define streaming
# define in
# define out

# ifndef __cplusplus
#  define EXTERNAL
# else
#  define EXTERNAL extern "C"
# endif // __cplusplus
#endif // __XC__

#endif // GLOBAL_H


here is an example of usage:
Code:
#ifndef ASYNCHRONOUS_H
#define ASYNCHRONOUS_H

#include "global.h"

EXTERNAL void read_channel(chanend ichan,       unsigned char buffer[], const unsigned int length);
EXTERNAL void write_channel(chanend ochan, const unsigned char buffer[], const unsigned int length);

EXTERNAL void forward_channel(chanend ichan, chanend ochan, unsigned int length);

#endif // _ASYNCHRONOUS_H


this header file will work for XC, C++ and C files. in fact, you can put the definition of read_channel() in asynchronous.xc, write_channel() in asynchronous.cpp and forward_channel() in asynchronous.c and it will compile and function properly. all you have to do is include asynchronous.h in each file.

one more thing, note the pointer style of the buffer. dont use a * because XC cant use them.


Top
Offline Profile View all posts by this user  
 
PostPosted: Wed May 02, 2012 2:56 pm 
Joined: Tue Dec 15, 2009 10:23 pm
Posts: 108
I'll give it a whirl later tonight...

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..

Just saying'...

-Y


Top
Offline Profile View all posts by this user  
 
PostPosted: Wed May 02, 2012 7:07 pm 
Joined: Tue Dec 15, 2009 10:23 pm
Posts: 108
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.

the file is here: http://pastebin.com/raw.php?i=LxRuvp9q
or you can just copy this
Code:
#ifndef GLOBAL_H
#define GLOBAL_H

#include <platform.h>
#include <xs1.h>

#ifdef __XC__
# define EXTERNAL extern
#else
# include <xccompat.h>
typedef chanend chan;
# define streaming
# define in
# define out

# ifndef __cplusplus
#  define EXTERNAL
# else
#  define EXTERNAL extern "C"
# endif // __cplusplus
#endif // __XC__

#endif // GLOBAL_H


here is an example of usage:
Code:
#ifndef ASYNCHRONOUS_H
#define ASYNCHRONOUS_H

#include "global.h"

EXTERNAL void read_channel(chanend ichan,       unsigned char buffer[], const unsigned int length);
EXTERNAL void write_channel(chanend ochan, const unsigned char buffer[], const unsigned int length);

EXTERNAL void forward_channel(chanend ichan, chanend ochan, unsigned int length);

#endif // _ASYNCHRONOUS_H


this header file will work for XC, C++ and C files. in fact, you can put the definition of read_channel() in asynchronous.xc, write_channel() in asynchronous.cpp and forward_channel() in asynchronous.c and it will compile and function properly. all you have to do is include asynchronous.h in each file.

one more thing, note the pointer style of the buffer. dont use a * because XC cant use them.


Gave it a quick whirl at work (ahem) and it does what it says on the tin, cheers for that!

As I said earlier, this is something that's fundamental to development and should work without having to wave dead chickens around and what not. Either make it part of the standard include file or modify the tools as people *will* rage-quit over something simple like this...

just my $0.02..

P.S. As a result, I'll be moving to command-line tools :-)


Top
Offline Profile View all posts by this user  
 
PostPosted: Thu May 03, 2012 1:21 am 
User avatar
XCore Expert
Joined: Sun Jul 11, 2010 1:31 am
Posts: 675
yzoer wrote:
I've tried using 'extern "C" ' but it barfs on the "C" part, so somewhere something's wrong.

Strange, that sounds like you are not using a C++ compiler to compile that
C++ file -- but then again, your original error suggests you certainly do.

Actual command lines and error messages would help ;-)

Quote:
Note that this is using a brand-new project-from-template in the XDE, so settings weren't messed with *at all* :roll:

It would be nice if things worked out of the box, sure :-)

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

The linker never sees the source file, just the object file. It could be XDE only knows
what compiler to use for certain extensions, I have no idea, I never use XDE. Common
C++ source file name extensions are .C, .cc, .cxx, .cpp, and they all mean exactly the
same thing.


Top
Offline Profile View all posts by this user  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic   [ 21 posts ]  Go to page 1, 2, 3  Next


Who is online

Users browsing this forum: No registered users and 2 guests



Search for:
Jump to: