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/