Build Shared Library File from String of Code?

Suppose I have a C++ code excerpt literal saved in a std::string variable, as such:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>

int main()
{
    std::string code = "";
    code += "#include <iostream>\n";
    code += "\n";
    code += "int test()\n";
    code += "{\n";
    code += "   std::cout << \"test\";\n";
    code += "\n";
    code += "   return 0;\n";
    code += "}\n";

    // Save "code" to DLL...

    return 0;
}


Where the code local variable stores a C++ function called test() that just prints test to the console.

Is it possible to now compile the code stored in the code string variable into a shared library DLL file and save it?

Thanks for reading my post, any guidance is appreciated.
Last edited on
I think it's a bit more complicated than how you're laying it out, but sure, if I'm understanding you correctly, you're just adding another layer of indirection by having a program that spits out the source code to part of the actual program that you are then compiling into a shared library DLL.

First, I would try to make a shared library the "normal" way so you understand the process. Then, figure out how to generate code like this, if that's what you really want. (I really don't see how this is beneficial. Why would writing a bunch of concatenated string literals in a .cpp file that then gets written to another source file be better than just writing that other source file?)

So once you run your program, and say, redirect it a file, e.g.
myprogram > generated_test.cpp

Or, you might need another layer, e.g.
myprogram | turn_function_into_shared_library_boilerplate > generated_test.cpp

Strictly speaking you don't need a header file, so you could just then compile that generated_test.cpp file and have it spit out the DLL.

You'd probably want to make some sort of batch/shell script to run the series of commands you'd need to make the DLL (and then presumably link to that DLL in a third program).

https://learn.microsoft.com/en-us/cpp/build/walkthrough-creating-and-using-a-dynamic-link-library-cpp
https://www.transmissionzero.co.uk/computing/building-dlls-with-mingw/
Last edited on
You can not just "save" the C/C++ code to a DLL file. That is because, after all, C/C++ code is just text. In order to create a proper DLL (or EXE), you have to compile your source code, by using a compiler, such as MSVC, GCC or Clang – which will translate the source code into binary machine code for the target CPU type (e.g. x86, arm64, etc.). Also, the binary machine code needs to be linked with additional code (e.g. C/C++ runtime) and wrapped in the proper file format (e.g. PE) in order to form a fully-fledged DLL or EXE file.

Usually the compiler and the linker are invoked as separate programs. For example, MSVC uses cl.exe and link.exe programs. In theory, you could invoke those (or whatever compiler you intend to use) from your own "main" program.

How to start a sub-process (program) in Windows:
- https://learn.microsoft.com/en-us/cpp/c-runtime-library/spawn-wspawn-functions?view=msvc-170
- https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-createprocessw

How to use cl.exe and link.exe:
- https://learn.microsoft.com/en-us/cpp/build/reference/compiler-command-line-syntax?view=msvc-170
- https://learn.microsoft.com/en-us/cpp/build/reference/linking?view=msvc-170#linker-command-line-syntax

In general, the procedure would be something like this, I suppose:
1. Save C/C++ source code to a .cpp file on the disk
2. Invoke compiler (e.g. cl.exe) to compile the source code, as a sub-process generates .obj file from .cpp
3. Wait for the compiler process to finish, make sure it succeeded
4. Invoke linker (e.g. link.exe) to link the object file, as a sub-process generates .dll or .exe file from .obj
5. Wait for the linker process to finish, make sure it succeeded


BTW, if you don't want to launch the compiler as a separate process, it may be possible to use LLVM/Clang as a library:
https://eli.thegreenplace.net/2014/samples-for-using-llvm-and-clang-as-a-library/
Last edited on
Topic archived. No new replies allowed.