Is it possible to insert a piece of code into an already existing C ++ program?

Viewed 416

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?

4 Answers

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.

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

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)

Edit: Yes, there is #include, which works at compile time. #include tells the compiler to also read the code from another file and include it into your program. It doesn't work if you want to generate the file after compiling the program.

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

Related