Home > Archive > Tcl > March 2006 > Compilation error:
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Compilation error:
|
|
| eruisi 2006-03-25, 4:01 am |
| I wanna build a new simple tcl command in C like this:
#include <tcl.h>
#include <stdlib.h>
#include <stdio.h>
int RandomCmd(ClientData clientData, Tcl_Interp *interp, int argc, char
*argv[]);
int Tcl_AppInit(Tcl_Interp *interp) {
if(Tcl_Init(interp)==TCL_ERROR) {
return TCL_ERROR;
}
Tcl_CreateCommand(interp, "random", RandomCmd, (ClientData)NULL,
(Tcl_CmdDeleteProc *)NULL);
/* tcl_RcFileName = ""; */
return TCL_OK;
}
main(int argc, char *argv[]) {
Tcl_Main(argc,argv,Tcl_AppInit);
exit(0);
}
int RandomCmd(ClientData clientData, Tcl_Interp *interp, int argc, char
*argv[]) {
int rnd, error;
int limit = 0;
if( argc > 2) {
interp->result = "Usage: random range";
return TCL_ERROR;
}
if( argc == 2) {
error = Tcl_GetInt(interp, argv[1], &limit);
if( error != TCL_OK ) {
return error;
}
}
srand( (unsigned)time(NULL) );
rnd = rand();
if(limit != 0 ) {
rnd = rnd % limit;
}
sprintf(interp->result,"%d", rnd);
return TCL_OK;
}
--------------------------------------------------------------------------------------------------------
Makefile:
INC = -I/usr/include
LIBS = -L/usr/lib -ltcl -lm
CFLAGS = $(INC)
main.o : main.c
gcc -c main.c
mytcl : main.o
gcc -o mytcl main.o $(LIBS)
They run correctly on red hat 7.3, but not on FC2. The gcc report this:
main.c:11: warning: passing arg 3 of `Tcl_CreateCommand' from
incompatible pointer type
if I change this line:
Tcl_CreateCommand(interp, "random", RandomCmd, (ClientData)NULL,
(Tcl_CmdDeleteProc *)NULL);
to:
Tcl_CreateCommand(interp, "random", (Tcl_CmdProc *)RandomCmd,
(ClientData)NULL, (Tcl_CmdDeleteProc *)NULL);
gcc doesn't report any error, but the execution generates a core dump.
Can anyone give me some hints on this problem?
| |
| Gerald W. Lester 2006-03-25, 4:01 am |
| eruisi wrote:
> I wanna build a new simple tcl command in C like this:
>
> ...
> int RandomCmd(ClientData clientData, Tcl_Interp *interp, int argc, char
> *argv[]);
>...
While not addressing your question directly, did you know that Tcl already
has a random function in the expr command?
--
+--------------------------------+---------------------------------------+
| Gerald W. Lester |
|"The man who fights for his ideals is the man who is alive." - Cervantes|
+------------------------------------------------------------------------+
| |
| Neil Madden 2006-03-25, 4:01 am |
| eruisi wrote:
[...]
>
> They run correctly on red hat 7.3, but not on FC2. The gcc report this:
> main.c:11: warning: passing arg 3 of `Tcl_CreateCommand' from
> incompatible pointer type
Most likely you need to add a CONST to the argv parameter:
int RandomCmd(ClientData cdata, Tcl_Interp *interp, int argc, CONST char
*argv[]);
More generally, the code you posted contains a few things which are not
considered good practice in modern Tcl. In particular writing directly
to interp->result (or any other field in any Tcl structure) is not a
good thing to do. Also, you may prefer to make your code a loadable
extension rather than a custom tclsh so that it can be loaded into any
compatible Tcl interpreter (tclsh, wish, expect, etc). A bare-bones
modern C extension is described at http://wiki.tcl.tk/11153 . It uses
the Tcl_Obj APIs which are more complicated (but more flexible) than the
older char * ones.
-- Neil
|
|
|
|
|