Home > Software engineering >  Is it possible to insert a piece of code into an already existing C program?
Is it possible to insert a piece of code into an already existing C program?

Time:11-04

One of my programs generates a large file with c code. Is there a way to call from another C class to insert the generated code into it?

Here is a small example to make it clear what I am trying to achieve.

Generated file example:

FirstClass first = FirstClass();
first.add(*some data1*)
first.add(*some data2*)
...
first.add(*some dataN*)

SecondClass second = SecondClass();
second.add(*some data1*)
second.add(*some data2*)
...
second.add(*some dataM*)

Main example:

int main() {
    std::string generated_file_path = ".../.../........./generated_file.txt";
    
    do_cpp_code_from_file(generated_file_path); //this function is what I need

    MyClass obj = MyClass();
    obj.use(first, second); // I want to use generated objects
}

Is this possible?

CodePudding user response:

This is exactly the reason why #include exists: you write a piece of code in another file (functions, class definitions, constants, ...) and use them in another file.

More information can be found in this reference document.

CodePudding user response:

You cannot insert some code in a running program, but you can have a code that combines two pieces of c code.

With stdin you can read a file, with stdout you can write one.

Here is a guide to stdin and stdout:

https://en.cppreference.com/w/cpp/io/c/std_streams

CodePudding user response:

In order to "insert" foreign code you want to use a microservice architecture. The "drawback" is that you need to predict what can be called in advance, but it's also a "better" feature in terms of security.

The caller do not need to know the details of the implementation of the callee, and both can be deployed independently.

CodePudding user response:

I'm not sure I understood your needs, but probably what you need is just to move some code into a shared library that you can invoke in your main. If that's not the case, even though C is not an interpreted language, but a compiled one, you might find usefull to evaluate the usage of this open source C interpreter https://github.com/root-project/cling

CodePudding user response:

Remember that you compile your C code into machine code, and then the computer runs the machine code. The computer doesn't actually know how to run C code.

So if you have a bunch of C code and you want to run it, your options are: run a C compiler to convert it to machine code, then run the machine code, or change the requirements so that instead of having a bunch of C code you'll have a bunch of machine code. In either case, the machine code will take the form of a DLL (Windows) or .so (Linux) which is basically a block of code designed to be loaded into another program at runtime.

Actually, a third option is to use a C interpreter (thanks to elefanxp's answer). I suspect you would have trouble getting the interpreted code to interface with the non-interpreted code properly.

A fourth option is to give up on C and use an interpreted scripting language such as Lua, which is designed for basically this purpose, but it's not C . (You have the same interfacing problems but at least the interpreter is more reliable. C is a complicated language)

  •  Tags:  
  • c
  • Related