NXC: no redefining of API function printf possible

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
Post Reply
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

NXC: no redefining of API function printf possible

Post by HaWe »

heya,
I wrote my own stdio.h file containing a new API function printf() featuring "\n" and many more things.

Including it into a main file and trying to compile/run, I get the error
# Warning: Redefinition of 'printf' is not identical
File "G:\Akten\Programmierung\NXC\Test\.\stdio.h" ; line 101
#
#----------------------------------------------------------
# Error: No task named "main" exists
File "G:\Akten\Programmierung\NXC\Test\.\stdio.h" ; line 439, position 1
#
#----------------------------------------------------------
my question:
1) why can't I redefine an API function?
2) the 2nd statement makes no sense as .h files never have a main task, so why this error msg?

BTW, if I rename printf into printf_ everythings running fine, so it's supposed to be a compiler issue.
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: NXC: no redefining of API function printf possible

Post by HaWe »

ps:
if I copy my redefined printf() into the main file, compiling is fine (the compiler beefs about the "Redefinition of 'printf' is not identical", but compiles though, and the program runs fine on the NXT)
but back in the header file instead the program doesn't compile and can not be downloaded to the NXT just as described above.

why not?
afanofosc
Site Admin
Posts: 1256
Joined: 26 Sep 2010, 19:36
Location: Nashville, TN
Contact:

Re: NXC: no redefining of API function printf possible

Post by afanofosc »

This code compiles fine (with a warning):

Code: Select all

#include "foo.h"

task main()
{
  TextOut(0, LCD_LINE1, "test");
  prinft("amazing");
}

Code: Select all

// foo.h

#define printf(_x) TextOut(0, LCD_LINE1, _x)
So I will need a simplified form of a main .nxc file and a .h header file which reproduces the compiler error. I only get the error about "no task named main exists" if I try to launch the compiler while the foo.h editor window is active.

John Hansen
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: NXC: no redefining of API function printf possible

Post by HaWe »

thx for your reply!
I'll try:

Code: Select all

// stdio-test.h

#ifndef __STDIO_H_
   #define __STDIO_H_
  


/*****************************************************************************/
// Display output

char LCDline[]={56,48,40,32,24,16,8,0};

int _cur_x_=0, _cur_y_=56;      // cursor home for NXC  = upper left = (0,56)
int _tab_width_=24;             // tab width by default = 24 = 4* 6-point letter

unsigned long _TEXTSTYLE_ = DRAW_OPT_NORMAL;   // text style by default


#define scrclr()          {  ClearScreen();  _cur_x_=0; _cur_y_=56; }
#define cls ()            {  ClearScreen();  _cur_x_=0; _cur_y_=56; }

#define curhome ()        { _cur_x_=0; _cur_y_=56; }  // move cursor home
#define settabwidth( t )  { _tab_width_ = t; }        // redefine tab width
#define settextstyle( t ) { _TEXTSTYLE_ = t; }        // redefine text style
//*********************************************

inline string strsplit(string &src, string mark) {
  string _sret="";
  int p=-1, l;
  p=Pos(mark, src);
  if (p>=0) {
    l=strlen(mark);
    _sret= SubStr(src, 0, p);
    src=SubStr(src, p+l, strlen(src)-p);
  }
  return _sret;
}

//*********************************************

string strexch(string src, string ex, string ch) {
  string _sst;
  _sst=strsplit(src,ex);
  if (_sst !="") return (StrCat(_sst,ch,src));
  else return("");
}


// printfxy()
// featuring "\i" for writing inverted
//******************************************************************************
#define printfxy( _x_, _y_, _f_, _v_) { \
  _cur_y_=_y_;   string  _s2, _sv;      \
  _s2=_f_;                              \
  if (Pos("\i",_s2)>=0) {               \
    _TEXTSTYLE_= DRAW_OPT_INVERT;       \
    _s2=strexch(_s2,"\i","");           \
  }                                     \
  int len=0;                            \
  if (Pos("%",_s2)==-1)  { _sv=_s2; }   \
  else { _sv = FormatVal(_s2, _v_);  }  \
  TextOut(_x_, _y_, _sv, _TEXTSTYLE_);  \
  len=strlen(_sv);                      \
  _cur_x_=_x_+6*(len);                  \
  _TEXTSTYLE_= DRAW_OPT_NORMAL;         \
}


// printfEx redefined as printf()
// featuring \n, \t, and "\i" for writing inverted
//******************************************************************************
#define printf(_fmt, _val) {             \
  int _x=_cur_x_; int _y=_cur_y_;        \
  string _sf, _s;                        \
  _sf=_fmt;                              \
  while (Pos("\n",_sf)>=0) {             \
    _s=strsplit(_sf,"\n");               \
    while (Pos("\t",_s)>=0) {            \
      _x=(1+_x/_tab_width_)*_tab_width_; \
      _s=strexch(_s, "\t", ""); }        \
    printfxy( _x, _y, _s, _val);         \
    _x=0;  _y-=8;                        \
  }                                      \
  while (Pos("\t",_sf)>=0) {             \
     _x=(1+_x/_tab_width_)*_tab_width_;  \
     _sf=strexch(_sf, "\t", ""); }       \
     if(_x>=96) {_x=0; _y-=8;}           \
     if(_y<0) {scrclr(); _y=56;}         \
  printfxy( _x, _y, _sf, _val);          \
}

//*********************************************

#endif

Code: Select all

// test: printf()

#include "stdio-test.h"

task main() {

  printf("%s\n", "Hello World");
  printf("%s\n", "by John Hansen");
  
  Wait(3000);

}
if I try ctrl+F5 or F6 nothing happens except the error msg :?
(in the top drop-down-menus Compile->Download+run ctrl+F5 and Compile->Download F6 are disabled!)

ps
strange:
if I close all files, terminate BCC, start anew, load the main file: everything works!
:o
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: NXC: no redefining of API function printf possible

Post by HaWe »

pps:
ps
strange:
if I close all files, terminate BCC, start anew, load the main file: everything works!
:o
anyhow, by pressing ctrl+F5 it's been downloaded, but it's not startet though!
afanofosc
Site Admin
Posts: 1256
Joined: 26 Sep 2010, 19:36
Location: Nashville, TN
Contact:

Re: NXC: no redefining of API function printf possible

Post by afanofosc »

It would appear that compiler warnings cause the IDE to not continue with the "run" portion of the Ctrl+F5 Download+Run action. Fortunately, you can turn off compiler warnings using -w- in the Switches field on the NBC/NXC tab on the Compiler page of the Preferences dialog.

John Hansen
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: NXC: no redefining of API function printf possible

Post by HaWe »

thank you, -w- works fine!
Post Reply

Who is online

Users browsing this forum: Semrush [Bot] and 13 guests