Re: Automatically generate variables



Mark McIntyre wrote:
On 13 Feb 2007 08:35:59 -0800, in comp.lang.c , "Nate"
<nverbeek@xxxxxxxxxxxxx> wrote:


Hello,

I am looking for a method to automatically declare variables in C.
I'm not sure if there is a good way to do this,


There isn't - you can't define object names at runtime in C.



Wrong

#include <stdio.h>
#include <windows.h>
int main(void)
{
FILE *f = fopen("dynamic.c","w");
void (*fn)(void);
void *ptr;

// 1 Define some structure for instance
fprintf(f,"struct dynamicobject { char *name;int len; };\n");
// 2 Define an object of that type
fprintf(f,"struct dynamicobject var = {\"NoName\",6};\n");
// 3 Define an exported function in a shared object that returns the
// address of the created object
fprintf(f,"void * __declspec(dllexport) \n");
fprintf("GetDynamicObject(void)\n\treturn &var;}\n");
// Done Close the file
fclose(f);
// Compile it. The compiler can change of course :-)
system("lcc dynamic.c");
// Link it into a shared object
system("lcclnk -dll dynamic.obj");
// Open the shared object (dlopen under Unix)
void *h = LoadLibrary("variables.dll");
// Get the address of the created function in the shared object
fn = (void (*fn)(int))GetProcAddress("GetDynamicObject");
// Call the function we just compile
ptr = fn();
// And now, ladies and gentlemen
// Here I have a pointer to a dynamically created object
}

Most of my customers buy lcc-win32 as a "Just in time compiler", that
allows them to do this much more efficiently than what is shown here

A JIT compiler is specialized in generating code dynamically. For instance, if you are modelling molecule interaction you can develop
a special language that is task oriented, compile it to C, then
just JIT compile it into a shared object that you run on the fly.

Of course what you generate is an object, not NAMES...
In this sense the OP is completely wrong of course.


.



Relevant Pages

  • Re: Getting a C++ Application to compile on AIX 5.1 VACPP
    ... The current output of the compile is as follows. ... INSERT: Shared object libC.a: 398 symbols imported. ... RESOLVE: 0 of 4863 symbols were kept. ... MISMATCH: No type mismatches exist. ...
    (comp.unix.aix)
  • problems with gcc after running up2date
    ... When trying to compile a newer version of CVS I got the following errors. ... ent size in shared object, consider re-linking ...
    (comp.os.linux.setup)
  • vtable?
    ... however i cant get the program to link with the shared object i create. ... undefined reference to `vtable for SWindow' ... p.s. when i compile the library by itself it compiles fine with no errors. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: wrong ELF class: ELFCLASS32
    ... ldd /usr/ucblib/librpcsoc.so ... As you can see the message you are getting shows that you are pointing ... The solution is to insure you compile with a compiler that is 64bit ... capable or any shared object is 64 bit format. ...
    (comp.unix.solaris)