Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8579751
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T20:40:37+00:00 2026-06-11T20:40:37+00:00

I have a Tcl code that is being sourced from a C application using:

  • 0

I have a Tcl code that is being sourced from a C application using:

Tcl_Eval(tcl_interp, "source nmsp.tcl")

Everything runs fine.
However, the namespace scope isn’t preserved. For example, the following file:

#!/bin/sh

# namespace evaluation
namespace eval bob {
    namespace eval joe {
        proc proc1 {} {}
    }
    proc proc2 {} {
        puts "proc2"
    }
    proc ::proc3 {} {
        puts "proc3"
    }
    proc joe::proc4 {} {
        puts "proc4"
    }
}

puts "Namespace calling [info procs ::bob\::*]"

when run by itself will produce this output:

Namespace calling ::bob::proc2

But when sourcing from Tcl_Eval will not print anything. In fact, the proc2 procedure can be called by itself fine without any namespace designation.

Anyone knows what may be causing it? I really like the encapsulation that namespaces provide.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T20:40:39+00:00Added an answer on June 11, 2026 at 8:40 pm

    Seems fine to me.

    I created the following Tcl extension to perform your Tcl_Eval:

    #include <tcl.h>
    
    static int
    DotestCmd(ClientData clientData, Tcl_Interp *interp,
              int objc, Tcl_Obj *const objv[])
    {
        return Tcl_Eval(interp, "source test_namespace.tcl");
    }
    
    int DLLEXPORT
    Testnamespace_Init(Tcl_Interp *interp)
    {
        if (Tcl_InitStubs(interp, "8.4", 0) == NULL) {
            return TCL_ERROR;
        }
        Tcl_CreateObjCommand(interp, "dotest", DotestCmd, NULL, NULL);
        return Tcl_PkgProvide(interp, "testnamespace", "1.0");
    }
    

    Being on windows, I compiled this using:

    cl -nologo -W3 -O2 -MD -DNDEBUG -DUSE_TCL_STUBS -I\opt\tcl\include -c test_namespace.c
    link -dll -release -out:testnamespace.dll test_namespace.obj \opt\tcl\lib\tclstub85.lib
    

    and then created a test_namespace.tcl file with the content you posted above. Running this produces the following:

    C:\opt\tcl\src>tclsh
    % load testnamespace.dll Testnamespace
    % dotest
    Namespace calling ::bob::proc2
    %
    

    and further introspection shows things are as I would expect from that script:

    % namespace children ::
    ::platform ::activestate ::bob ::tcl
    % namespace children ::bob
    ::bob::joe
    %
    

    You probably are doing something weird in your C code first if this is really not working for you.

    Update

    The above example is for extending tcl with a compiled package. Apparently the OP is embedding Tcl into some other application. A trivial example of doing this is provided here which also runs the same command to the same effect as above. In reality when embedding Tcl into an application the code should use the tclAppInit.c file and provide its own Tcl_AppInit function. By running the usual Tcl_Main you get the full capabilities for processing events (needed for fileevents or after commands) and the interactive shell. An example of that follows the trivial version:

    /* trivial embedding Tcl example */
    #include <tcl.h>
    #include <locale.h>
    
    int
    main(int argc, char *argv[])
    {
        Tcl_Interp *interp = NULL;
        int r = TCL_ERROR;
    
        setlocale(LC_ALL, "C");
        interp = Tcl_CreateInterp();
        if (interp != NULL) {
            Tcl_FindExecutable(argv[0]);
            r = Tcl_Eval(interp, "source test_namespace.tcl");
            if (TCL_OK == r)
                r = Tcl_Eval(interp, "puts [namespace children ::bob]");
            Tcl_DeleteInterp(interp);
        }
        return r;
    }
    

    Running the above:

    C:\opt\tcl\src>cl -nologo -W3 -O2 -MD -I\opt\tcl\include test_namesp_embed.c -link -subsystem:console -release -libpath:\opt\tcl\lib tcl85.lib
    test_namesp_embed.c
    
    C:\opt\tcl\src>test_namesp_embed.exe test_namespace.tcl
    Namespace calling ::bob::proc2
    ::bob::joe
    

    A better embedding scheme that uses tclAppInit to extend a stock Tcl interpreter:

    #include <tcl.h>
    #include <locale.h>
    
    #define TCL_LOCAL_APPINIT Custom_AppInit
    int
    Custom_AppInit(Tcl_Interp *interp)
    {
        return Tcl_Eval(interp, "source test_namespace.tcl");
    }
    
    #include "/opt/tcl/src/tcl.git/win/tclAppInit.c"
    

    Building and running this also produces the same output as previous versions:

    C:\opt\tcl\src>cl -nologo -W3 -O2 -MD -I\opt\tcl\include test_namesp_embed.c -link -subsystem:console -release -libpath:\opt\tcl\lib tcl85.lib
    C:\opt\tcl\src>test_namesp_embed.exe
    Namespace calling ::bob::proc2
    % namespace children ::bob
    ::bob::joe
    % exit
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a small shell application that embeds Tcl to execute some set of
I am using the following tcl code to store a file from my deskstop
Currently I have some code that is doing an SFTP via expect/tcl. Its something
I have the following piece of TCL code: #wrong format: set in_val 12 0
I have a question about the following code in TCL/EXPECT: expect { timeout {
I have a question about variable scope in TCL, I have the following code:
I have a Tcl/Tk app that generates many forms and would like to be
I have parsed a file in tcl and i read the line like that
I have a question about upvar command in TCL. Using upvar command, we have
In order to pass some code to an application created with C++ I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.