Parse error when including time.h

Technical questions regarding the XTC tools and programming with XMOS.
Post Reply
User avatar
aneves
Experienced Member
Posts: 93
Joined: Wed Sep 16, 2015 2:38 pm

Parse error when including time.h

Post by aneves »

I have a demo project I'm working on where I've included the time.h standard header. When I build my project, I get the following error:

Code: Select all

In file included from ../src/main.xc:11:
In file included from C:\Program Files (x86)\XMOS\xTIMEcomposer\Community_14.1.0\target/include/xc\time.h:4:
C:\Program Files (x86)\XMOS\xTIMEcomposer\Community_14.1.0\target/include\time.h:47:30: error: parse error before "void"
clock_t    _EXFUN(clock,    (void));
                             ^
C:\Program Files (x86)\XMOS\xTIMEcomposer\Community_14.1.0\target/include\_ansi.h:75:35: note: expanded from macro '_EXFUN'
#define _EXFUN(name, proto)             name proto
                                             ^
xmake[1]: *** [.build_X200/src//main.xc.pca.xml] Error 1
xmake: *** [analyze] Error 2
When I open the time.h header, I see the offending line:

Code: Select all

clock_t	   _EXFUN(clock,    (void));
which after macro expansion becomes a function forward declaration:

Code: Select all

clock_t __clock_t(void);
I can't see why the parser would complain about this. The function name is legal. I've searched all of the other headers in the xTime include folders and __clock_t is not being used or defined anywhere else. I can work around this by commenting that line out since I'm not using that function.

When I compare to time.h headers found on Windows and OS X, the function name is clock as opposed to __clock_t found here.

I know that the clock() function in C just returns the tick count since the program launched. Perhaps XC is attempting to override it since it has it's own set of timer/clock hardware features?

Anyone else experience this issue?

Any insight would be appreciated!


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

Post by segher »

Since "clock" is such a common name, it might be defined to something else already.
The error message does not say (does not say much useful at all).
User avatar
Folknology
XCore Legend
Posts: 1274
Joined: Thu Dec 10, 2009 10:20 pm
Contact:

Post by Folknology »

I (like segher) can definitely recall some naming overlaps between Xmos timer code/headers and some of the standard c header files, however I have not been successful in tracking down any of the relevant Xmos documentation about this.

(Prob worth a quick recursive grep on your XMOSLIBS/target/include/*)

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

Post by richard »

<xs1.h> defines a clock type which represents a clock block resource. This clock "type" is actually a macro that expands to __clock_t which recognized as a builtin type by the xC compiler. The C standard library header <time.h> defines the clock() function, as required by the C89 standard. If you include (either directly or indirectly via other header files) <xs1.h> and <time.h> in the same file you get a clash. Normally the macro in the <time.h> header would expand to:

Code: Select all

clock_t clock(void);
However if <xs1.h> is included first then it will expand to:

Code: Select all

clock_t __clock_t(void);
Since __clock_t is a builtin type this is a syntax error, in the same way that trying to declare a function called "int" would result in syntax error.

To avoid this clash you should avoid including both headers in the same file - i.e. try to move the code that uses <time.h> to a separate file that doesn't include <xs1.h>
User avatar
segher
XCore Expert
Posts: 844
Joined: Sun Jul 11, 2010 1:31 am
Contact:

Post by segher »

Or include <xs1.h> last, does that work?
Post Reply