int main(int argc, char *argv[])
{
Tcl_Interp *tclinterp = Tcl_CreateInterp();
if (Tcl_EvalFile(tclinterp, argv[1]) != TCL_OK)
fprintf(stderr, "Error: %s\n", tclinterp->result);
return 0;
}
----
When I try to compile with
gcc test.c -o test
the system says
--
/usr/bin/ld: Undefined symbols:
_Tcl_CreateInterp
_Tcl_EvalFile
collect2: ld returned 1 exit status
----
Any hints?
In the system there's tcltk libraries.
Regards.
ALEX
That's promising, but you need to tell gcc which these are. And if they
aren't on the standard search path, you also need to tell gcc where
exactly it ought to look for them.
Try:
# if the Tcl library is on the standard search path
gcc test.c -o test -ltcl8.4
# if the Tcl library is not on the standard search path
gcc test.c -o test -L/your/lib/dir -ltcl8.4
(adapt this for your version of Tcl)
If this succeeds, you will find that your executable's don't run as
expected, because of some programming mistakes. The manual pages for
Tcl_Main() and Tcl_AppInit() tell you why.
HTH,
Erik Leunissen
> Regards.
> ALEX
--
leunissen@ nl | Merge the left part of these two lines into one,
e. hccnet. | respecting a character's position in a line.
> Hi all
> I'd like to compile this simple c source to read and execute a tcl file
> --
> #include <tcl.h>
>
> int main(int argc, char *argv[])
> {
> Tcl_Interp *tclinterp = Tcl_CreateInterp();
>
> if (Tcl_EvalFile(tclinterp, argv[1]) != TCL_OK)
> fprintf(stderr, "Error: %s\n", tclinterp->result);
>
> return 0;
> }
> ----
> When I try to compile with
> gcc test.c -o test
-ltcl84
But it won't work. You must call Tcl_FindExecutable() beforehand. Look
at the source for tclsh to see what is necessary.
Christian
You're missing two critical things, being the initialization of the Tcl
library itself, and the instructions to the compiler to use the Tcl
library when building your example. You've got a few lesser errors too.
> #include <tcl.h>
>
> int main(int argc, char *argv[])
> {
> Tcl_Interp *tclinterp = Tcl_CreateInterp();
>
> if (Tcl_EvalFile(tclinterp, argv[1]) != TCL_OK)
> fprintf(stderr, "Error: %s\n", tclinterp->result);
>
> return 0;
> }
This should be:
#include <tcl.h>
int main(int argc,char**argv) {
Tcl_Interp *interp; /* the conventional name */
Tcl_FindExecutable(argv[0]); /* Init the library itself */
interp = Tcl_CreateInterp();
if (Tcl_EvalFile(interp, argv[1]) != TCL_OK) {
fprintf(stderr, "Error: %s\n",
/* interp->result is deprecated */
Tcl_GetStringResult(interp));
return 1; /* surprisingly important in practice! */
}
return 0;
}
Of course, almost all of that (and more) is done for you by the
Tcl_Main() function, but it doesn't allow so much fine control over
other things that a real app might need.
> When I try to compile with
> gcc test.c -o test
You've not told the compiler to use Tcl in there! C compilers are
*stupid* and must be told such things by hand, using the -l option to
the compiler (or linker) like this:
gcc test.c -o test -ltcl
You might need to use a different name for the Tcl library (e.g.
including the version number) and you might also need to tell the
compiler where to look for the library (using the -L option, which must
come *before* the -l option) resulting in something more like this:
gcc test.c -o test -L/usr/local/tcl/libs/here -ltcl8.4
As a final note, it's best to not call the program you build 'test'; any
system with gcc will probably have a system binary called that too. :-)
Donal.
And depending on the type of system you are using, the way you
compiled Tcl, etc. you may need to try something like this:
gcc mytest.c -o mytest -L/path/name -R/path/name -ltcl
Larry W. Virden wrote :
>
> And depending on the type of system you are using, the way you
> compiled Tcl, etc. you may need to try something like this:
>
> gcc mytest.c -o mytest -L/path/name -R/path/name -ltcl
>
Also note that, whereas gcc on Solaris accepts -R and automatically
passes it to linker, this option is not a standard GCC option, and isn't
supported as is on most systems, including Linux. Instead, you need to
explicitely pass RPATH value to linker. On Linux systems, one would use
something like:
gcc mytest.c -o mytest -L/some/path -Wl,-rpath,/some/path -ltcl
or
gcc mytest.c -o mytest -L/some/path -Wl,-rpath -Wl,/some/path -ltcl
Last, MacOSX linker (which seems to be OP system) have no rpath support
at all.
Eric