xC: build fails when optimization is turned off

Technical questions regarding the XTC tools and programming with XMOS.
Endre
Active Member
Posts: 38
Joined: Fri Jan 01, 2016 10:13 am

xC: build fails when optimization is turned off

Post by Endre »

Hi,

I am working on an USB CDC project. To get more verbose exception messages, I have turned optimization off. This caused build failure.
I have managed to reproduce it with AN00124_CDC_VCOM_class[2.0.2]:
Makefile:

Code: Select all

-XCC_FLAGS_U     = -Wall -O3 -report -DXUD_SERIES_SUPPORT=XUD_U_SERIES -g
+XCC_FLAGS_U     = -Wall -O0 -report -DXUD_SERIES_SUPPORT=XUD_U_SERIES -g
build error:

Code: Select all

Creating app_usb_cdc_demo_U.xe
../src/xud_cdc.xc: Error: Undefined reference to 'XUD_SetReady_In' (possible inline definition without external definition)
../src/xud_cdc.xc: Error: Undefined reference to 'XUD_SetReady_Out' (possible inline definition without external definition)
xmake[1]: *** [bin/U/app_usb_cdc_demo_U.xe] Error 1
xmake: *** [bin/U/app_usb_cdc_demo_U.xe] Error 2
My xTIMEcomposer Version: Community_14.1.2 (build 17961, Dec-04-2015)


henk
Respected Member
Posts: 347
Joined: Wed Jan 27, 2016 5:21 pm

Post by henk »

Hmm yes, I guess that the USB library is not designed to run without optimisation - it probably relies on it, in that it won't meet the real-time requirements of USB when compiled with different -O.

By the looks of it, to get it to compile you may have to remove the inline in front of those two functions, or add an externed one. But I don't know whether it will run correctly.

Cheers,
Henk
Endre
Active Member
Posts: 38
Joined: Fri Jan 01, 2016 10:13 am

Post by Endre »

henk wrote:Hmm yes, I guess that the USB library is not designed to run without optimisation - it probably relies on it, in that it won't meet the real-time requirements of USB when compiled with different -O.
Hi,

I guess even if it doesn't work without optimization, it should compile.
I have never seen an ordinal C/C++ compiler which couldn't handle inline functions, defined in headers, without optimization.
henk
Respected Member
Posts: 347
Joined: Wed Jan 27, 2016 5:21 pm

Post by henk »

I completely agree with your sentiment. However compiling a tiny C program using a standard C compiler on my laptop disagrees with both of us:

Code: Select all

12:51:02 % cc t.c
Undefined symbols for architecture x86_64:
  "_f", referenced from:
      _main in t-04b1ce.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
12:51:04 % cc -O3 t.c
12:51:11 % 
Ie, this is the way C is; which is what XC follows closely. Arguably, the code should be written so that it does compile with and without optimisation. Something to be fixed.

For info, in the above code t.c contains the following:

Code: Select all

#include "a.h"

int main() {
  f();
  return 0;
}
a.h contains the following:

Code: Select all

inline void f() { }
I used LLVM version 7.3.0 (clang-703.0.29)
Endre
Active Member
Posts: 38
Joined: Fri Jan 01, 2016 10:13 am

Post by Endre »

henk wrote:I completely agree with your sentiment. However compiling a tiny C program using a standard C compiler on my laptop disagrees with both of us
I have also tried with gcc:
hello.h:

Code: Select all

#ifndef _HELLO_H
#define _HELLO_H

#include <stdio.h>

inline void hello() {
	printf("hello\n");
}

#endif
hello.c:

Code: Select all

#include "hello.h"

int main(int argc, char ** argv) {
	hello();
	return 0;
}

Code: Select all

[endre@localhost InlineTest]$ gcc -O0 hello.c -o hello
[endre@localhost InlineTest]$ ./hello 
hello
[endre@localhost InlineTest]$ gcc --version
gcc (GCC) 4.8.3 20140911 (Red Hat 4.8.3-7)
Copyright (C) 2013 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

[endre@localhost InlineTest]$

This compile failure seems to be a clang "feature", maybe placing "static" before the "inline" would help (I haven't tried yet.)
henk
Respected Member
Posts: 347
Joined: Wed Jan 27, 2016 5:21 pm

Post by henk »

Yes - they behave differently.
The following line fixes it for Clang/xcc:

Code: Select all

extern void f();
in a file that includes the include file with the inline definitions.