Problem with compiling example for inline C-code

Discussion of issues related specifically to writing code for native mode devices. This includes ZBasic code as well as assembly language code and C code, both inline and standalone.
Post Reply
kranenborg
Posts: 57
Joined: 27 July 2009, 14:20 PM
Location: Groningen, The Netherlands
Contact:

Problem with compiling example for inline C-code

Post by kranenborg »

Hello,

As a preparation to using inline C-code on native mode devices I tried to compile the example program in Section 4.1 of the manual for a ZX328L:

Code: Select all

Dim b as Byte 

#c 
char ch;
#endc 

Sub Main()
b = 5 

#c
ch = zv_b; 
#endc 
End Sub
With the verbose option set I get the following message:
avr-gcc -c -DF_CPU=14745600UL -Dzx328nu -mmcu=atmega328p -I"C:/Program Files/ZBasic/zxlib" -I. -gdwarf-2 -Os -Wall -funsigned-char -fpack-struct -Wstrict-prototypes -std=gnu99 -fgnu89-inline testdouble.c -o testdouble.o
testdouble.c: In function 'zf_Main':
testdouble.c:44: error: 'zv_b' undeclared (first use in this function)

It is not clear to me why running the exact example does not work, maybe someone can be of help here?

Best regards,
Jurjen
dkinzer
Site Admin
Posts: 3120
Joined: 03 September 2005, 13:53 PM
Location: Portland, OR

Re: Problem with compiling example for inline C-code

Post by dkinzer »

kranenborg wrote:It is not clear to me why running the exact example does not work, maybe someone can be of help here?
The example was correct at one time but is now out of date. The naming convention for private module-level variables was changed to avoid a possible conflict with a public module-level variable of the same name defined in another module (a perfectly legal but probably inadvisable arrangement).

Currently, private module-level variables have the prefix "mzv_" instead of just "zv_" as before. Consequently, the example should be changed thusly:

Code: Select all

Dim b as Byte 
#c 
char ch;
#endc 

Sub Main()
  b = 5 
#c
  ch = mzv_b; 
#endc 
End Sub
Alternately, the example could read:

Code: Select all

Public b as Byte 
#c 
char ch;
#endc 

Sub Main()
  b = 5 
#c
  ch = zv_b; 
#endc 
End Sub
- Don Kinzer
kranenborg
Posts: 57
Joined: 27 July 2009, 14:20 PM
Location: Groningen, The Netherlands
Contact:

Post by kranenborg »

Thanks Don, that worked out very well.

The simple example allows for some experimentation; moving the initialization of b inside the C-code required the use of the "Used" attribute in order to compile because b seems to be not used from the ZBASIC point of view (this took me a while, being focused on the volatile attribute instead). For what it is worth:

Code: Select all


Public b as Byte Attribute(Used)

#c 
char ch; 
#endc 

Sub Main() 

#c 
  zv_b = 5; 
  ch = zv_b; 
#endc 

End Sub

kranenborg
Posts: 57
Joined: 27 July 2009, 14:20 PM
Location: Groningen, The Netherlands
Contact:

Post by kranenborg »

Continuing further with C code inlining I found that the very commonly used include file "stdio.h" (quite empty in the AVR version but not completely void) gives problems (all other include files that come with the Zbasic installatiion appear to work well):

Code: Select all


Option TargetCPU ZX328nu
Sub Main()

#c
#include <stdio.h>
#endc

End Sub

Compiling this one gives the following message:

>"C:\Program Files\ZBasic\zbasic.exe" --target-device=ZX328nu --directory="C:\Documents and Settings\sejukra\Desktop\Jurjen\Electronics\projecten\ZBASIC/" --project="HelloWorld.pjt"
make: Entering directory `C:/Documents and Settings/sejukra/Desktop/Jurjen/Electronics/projecten/ZBASIC/zx_x3nfdM'
avr-gcc -c -DF_CPU=14745600UL -Dzx328nu -mmcu=atmega328p -I"C:/Program Files/ZBasic/zxlib" -I. -gdwarf-2 -Os -Wall -funsigned-char -fpack-struct -Wstrict-prototypes -std=gnu99 -fgnu89-inline test.c -o test.o
In file included from test.c:39:
c:/program files/zbasic/winavr/bin/../avr/include/stdio.h: In function 'zf_Main':
c:/program files/zbasic/winavr/bin/../avr/include/stdio.h:939: error: invalid storage class for function 'fflush'
make: *** [test.o] Error 1
I have also WinAvr installed, the corresponding version of stdio.h is identical with the Zbasic version but compiles without problems on that platform (which has a newer version of gcc). Does the Zbasic gcc version need to be upgraded?

/Jurjen
dkinzer
Site Admin
Posts: 3120
Joined: 03 September 2005, 13:53 PM
Location: Portland, OR

Post by dkinzer »

kranenborg wrote:Continuing further with C code inlining I found that the very commonly used include file "stdio.h" [...] gives problems
There isn't anything in stdio.h that is useful in ZBasic code so there's not much point in including it. In any event, such includes should be done at the module level, i.e. outside of any procedures.

Code: Select all

#c
#include <stdio.h>
#endc
Sub Main&#40;&#41;
End Sub
If you look at the file zxlib.h (part of the ZBasic native mode files), you'll see that a number of common include files are already included in every application. On occasion, there may be others that you need for special purposes.
kranenborg wrote:Does the Zbasic gcc version need to be upgraded?
No. Although future releases of ZBasic may use a newer version of the avr-gcc compiler, it is inadvisable to attempt to use a newer version than the one supplied, not least because it hasn't been tested and there may be compile-time, link-time or run-time issues.
- Don Kinzer
kranenborg
Posts: 57
Joined: 27 July 2009, 14:20 PM
Location: Groningen, The Netherlands
Contact:

Post by kranenborg »

Thanks Don, that explained all I needed. Diving into zxlib.h

/Jurjen
Post Reply