compile C extension at runtime? 
Author Message
 compile C extension at runtime?

One of the Really Neat(tm) features of Tcl is that you can create new
code at runtime.

Has anybody ever tried to compile and link C code at runtime?  Imagine,
if you will, a procedure which accepts C code as an argument, writes it
to a ".c" file, compiles the file into a shared library, and loads a new
command.  

It would be a little more complicated than that.  Might use SWIG to
generate the interface code, and I can only imagine how difficult it
would be to reliably invoke autoconf/cc.  But has anyone ever tried?

Bob
--

Mayo Foundation                                 (507) 538-5495
200 First St. SW                            FAX (507) 284-9171
Rochester MN, 55901  USA             http://www.*-*-*.com/



Mon, 18 Aug 2003 21:45:52 GMT  
 compile C extension at runtime?

Quote:

> Has anybody ever tried to compile and link C code at runtime?  Imagine,
> if you will, a procedure which accepts C code as an argument, writes it
> to a ".c" file, compiles the file into a shared library, and loads a new
> command.

I didn't get that far, but my Xmas 2000 fun project "C code generators",
http://www.mini.net/cgi-bin/wikit/1119.html (all sources on the Wiki)
made three steps into that direction:

- cproc: make a tiny C executable, to be called with 'exec', from Tcl

- cserver: make a C 'server' that communicates via stdin/out, so it
remains alive between calls, to be called with 'open |'

- cxtend: make an extended tclsh/wish with the desired custom C
commands, might be called with 'exec cxwish&;exit'

All three took C codelets as arguments, wrapped that as necessary,
generated a C source, exec'ed gcc. It's just that I never managed to
create a DLL (Xmas vacations were over...)

Quote:
> It would be a little more complicated than that.  Might use SWIG to
> generate the interface code, and I can only imagine how difficult it
> would be to reliably invoke autoconf/cc.  But has anyone ever tried?

My experience is that with reasonably well-behaved source, gcc is
sufficient all the way to an executable, so no make, much less autoconf
required. That was with Cygwin on Win 95. When I again get some more
time, I hope to tackle the DLL creation bit too... but don't know yet
when that will be. Anybody interested, feel free to grab that code from
the Wiki, improve on it, put it back to the Wiki!
--
Schoene Gruesse/best regards, Richard Suchenwirth - +49-7531-86 2703
RC DT2, Siemens ElectroCom, Buecklestr.1-5, D-78467 Konstanz,Germany
Personal opinions expressed only unless explicitly stated otherwise.


Mon, 18 Aug 2003 22:42:31 GMT  
 compile C extension at runtime?

Quote:

>One of the Really Neat(tm) features of Tcl is that you can create new
>code at runtime.

>Has anybody ever tried to compile and link C code at runtime?  

apart from that this would hardly be platform-compatible
(or is there a gcc for mac ?),  something like this
appeared in this newsgroup a few months ago.

though I don't remember any  searchable keywords, it contained snippets
like:
  xyz {
     /* some C-code */
     j*=i+++++j;
  }
which would embed it in a C-function, compile it and run it, or maybe
load it and create a new tcl-command for it, ... whatever.

might be in the wiki.



Mon, 18 Aug 2003 22:50:11 GMT  
 compile C extension at runtime?

Quote:

> Has anybody ever tried to compile and link C code at runtime?  Imagine,
> if you will, a procedure which accepts C code as an argument, writes it
> to a ".c" file, compiles the file into a shared library, and loads a new
> command.

Richard Suchenwirth did explore the issue:
http://mini.net/cgi-bin/wikit/1118.html

Miguel



Mon, 18 Aug 2003 22:48:44 GMT  
 compile C extension at runtime?

                        .
                        .
                        .
Quote:
>My experience is that with reasonably well-behaved source, gcc is
>sufficient all the way to an executable, so no make, much less autoconf
>required. That was with Cygwin on Win 95. When I again get some more

                        .
                        .
                        .
Me, too.  That is, I've done experiments in
this direction, and never been tempted by
autoconf.

However, this was mostly at a time when I
was also working on alternative languages to
make and autoconf, so perhaps others wouldn't
see it the same way.
--


Business:  http://www.Phaseit.net
Personal:  http://starbase.neosoft.com/~claird/home.html



Mon, 18 Aug 2003 23:09:07 GMT  
 compile C extension at runtime?


:Has anybody ever tried to compile and link C code at runtime?  

Yes.

<URL: http://www.purl.org/NET/Tcl-FAQ/part4.html>

What: Embedded C (EC)
Where: <URL: ftp://ftp.reed.edu/pub/users/greaber/ec-0.1.tar.gz>
Description: Allows you to include C code in your Tcl scripts, compiling
        and dynamically loading it on the fly.  The code will also be
        cached so the next time you run the program, you don't wait for
        it to compile.  Known to work on DEC OSF/1 V3.2 and SunOS 5.5.
        Not yet ported to Macintosh or Windows.
Updated: 09/1996

--
--
"See, he's not just anyone ... he's my son."  Mark Schultz

Even if explicitly stated to the contrary, nothing in this posting



Tue, 19 Aug 2003 02:33:09 GMT  
 compile C extension at runtime?

Quote:

> Has anybody ever tried to compile and link C code at runtime?  Imagine,
> if you will, a procedure which accepts C code as an argument, writes it
> to a ".c" file, compiles the file into a shared library, and loads a new
> command.

kt2c does this.  Basically, it takes the bytecode of an existing proc and
converts it to C statements.  The test harness for kt2c also compiles the
code, loads the dll/so, and performs a speed and functionality comparison
against the original code.

--
David Cuthbert



Tue, 19 Aug 2003 08:41:14 GMT  
 compile C extension at runtime?

Quote:

>One of the Really Neat(tm) features of Tcl is that you can create new
>code at runtime.

Could you please explain the benefits to use Tcl in this way?

Quote:
>Has anybody ever tried to compile and link C code at runtime?  Imagine,
>if you will, a procedure which accepts C code as an argument, writes it
>to a ".c" file, compiles the file into a shared library, and loads a new
>command.

Is that make has done? so why not use exec make ...?

Quote:
>It would be a little more complicated than that.  Might use SWIG to
>generate the interface code, and I can only imagine how difficult it
>would be to reliably invoke autoconf/cc.  But has anyone ever tried?

Chang
Quote:
>Bob
>--

>Mayo Foundation                                 (507) 538-5495
>200 First St. SW                            FAX (507) 284-9171
>Rochester MN, 55901  USA            http://www.mayo.edu/sppdg/



Tue, 19 Aug 2003 09:50:24 GMT  
 compile C extension at runtime?
Being able to compile/link/autoload C code a runtime would be a huge
plus for Tcl if it existed. One advantage would be that Tcl could then be
written in Tcl. A second major advantage would be the distribution
of packages.

If C were to be included into Tcl, my preference would
be to include it into Itcl which already has syntax for loading C libraries.
A minor extension to this syntax would allow C code to be written in-place,
compiled and loaded as a method. The difficulty, as I see it, would be
to define a portable platform definition that would take care of the autoConf
problem. My idea is that when Tcl is installed it would use an autoConf
configure file to write configuration info that would then be used the compiler
that
was run to do in-place compilation. Once a users configuration was established
packages would not have to worry about dealing with this problem.
Platform specific calls would of course still be an issue.
-tjk

Quote:


> >One of the Really Neat(tm) features of Tcl is that you can create new
> >code at runtime.

> Could you please explain the benefits to use Tcl in this way?

> >Has anybody ever tried to compile and link C code at runtime?  Imagine,
> >if you will, a procedure which accepts C code as an argument, writes it
> >to a ".c" file, compiles the file into a shared library, and loads a new
> >command.

> Is that make has done? so why not use exec make ...?

> >It would be a little more complicated than that.  Might use SWIG to
> >generate the interface code, and I can only imagine how difficult it
> >would be to reliably invoke autoconf/cc.  But has anyone ever tried?

> Chang

> >Bob
> >--

> >Mayo Foundation                                 (507) 538-5495
> >200 First St. SW                            FAX (507) 284-9171
> >Rochester MN, 55901  USA            http://www.mayo.edu/sppdg/



Thu, 21 Aug 2003 03:08:19 GMT  
 
 [ 9 post ] 

 Relevant Pages 

1. CS Refactoring Browser Extensions

2. to CS: or not to CS: in F-PC assembler

3. Starting a application file by its extension and a runtime problem

4. Ruby-Postgres extension runtime error

5. Runtime error importing extension modules

6. Is it possible so write part of code that will be compiled runtime

7. Runtime compiling of variables

8. Programmically mass compile under LV runtime

9. Runtime compiling?

10. Warnings at compile time for possible runtime pattern matching failure

11. c2ada compilinG/runtime problem

12. runtime errors with lammpi compiled by intel compilers

 

 
Powered by phpBB® Forum Software