# Generating C++ Code

Involved files:
- [W05_Lib/W05_Lib.h](https://github.com/vczh-libraries/Release/blob/master/Tutorial/Console_Workflow/W05_Lib/W05_Lib.h)
- [W05_Lib/W05_Lib.cpp](https://github.com/vczh-libraries/Release/blob/master/Tutorial/Console_Workflow/W05_Lib/W05_Lib.cpp)
- [W05_Compile/Main.cpp](https://github.com/vczh-libraries/Release/blob/master/Tutorial/Console_Workflow/W05_Compile/Main.cpp)
- [W05_StaticRun/Main.cpp](https://github.com/vczh-libraries/Release/blob/master/Tutorial/Console_Workflow/W05_StaticRun/Main.cpp) Generated files:
- [W05_StaticRun/Generated/W05ScriptIncludes.h](https://github.com/vczh-libraries/Release/blob/master/Tutorial/Console_Workflow/W05_StaticRun/Generated/W05ScriptIncludes.h)
- [W05_StaticRun/Generated/W05Script.h](https://github.com/vczh-libraries/Release/blob/master/Tutorial/Console_Workflow/W05_StaticRun/Generated/W05Script.h)
- [W05_StaticRun/Generated/W05Script.cpp](https://github.com/vczh-libraries/Release/blob/master/Tutorial/Console_Workflow/W05_StaticRun/Generated/W05Script.cpp)
- [W05_StaticRun/Generated/W05ScriptReflection.h](https://github.com/vczh-libraries/Release/blob/master/Tutorial/Console_Workflow/W05_StaticRun/Generated/W05ScriptReflection.h)
- [W05_StaticRun/Generated/W05ScriptReflection.cpp](https://github.com/vczh-libraries/Release/blob/master/Tutorial/Console_Workflow/W05_StaticRun/Generated/W05ScriptReflection.cpp)

Workflow script can be converted to C++ and save to files. If you don't want reflection, you still need to link **VlppReflection.cpp**, but by adding **VCZH_DEBUG_NO_REFLECTION** to the precompile definitions list, many reflection related code are removed in C++ files.

In this case, you can either link or not link to **W05ScriptReflection.cpp**. When **VCZH_DEBUG_NO_REFLECTION** is on, you get an empty **LoadW05ScriptTypes** function. This helps you to write code that can be compiled regardless whether **VCZH_DEBUG_NO_REFLECTION** is on or off.

**W05_Lib/W05_Lib.h** and **W05_Lib/W05_Lib.cpp** is a good example. When **VCZH_DEBUG_NO_REFLECTION** is on, reflection related codes are removed. This makes them be able to use in both **W05_DynamicRun** and **W05_StaticRun**.

You need the semantic analyzing result in order to generate C++ code, so the compiling code is slightly different. After you get the assembly, you can use **WfCppInput** class and **GenerateCppFiles** function to translate Workflow to C++.
```
auto cppInput = Ptr(new WfCppInput(L"W05Script"));
cppInput->comment = L"THIS FILE IS GENERATED BY W05_COMPILE";
cppInput->headerGuardPrefix = L"W05_SCRIPT_";
cppInput->normalIncludes.Add(L"../../W05_Lib/W05_Lib.h");
cppInput->normalIncludes.Add(L"VlppWorkflowLibrary.h");

auto cppOutput = GenerateCppFiles(cppInput, &manager);
```
 All configuration is necessary.

After calling **GenerateCppFiles**, you can a **WfCppOutput** object, containing C++ codes. You need to write them to files manually.
```
for (vint i = 0; i < cppOutput->cppFiles.Count(); i++)
{
    auto key = cppOutput->cppFiles.Keys()[i];
    auto value = cppOutput->cppFiles.Values()[i];
    Console::WriteLine(L"    " + key);
    File(cppFolder / key).WriteAllText(value, false, BomEncoder::Utf8);
}
```

Then you get all generated files! All Workflow classes now become C++ classes:
```
#include "Generated/W05ScriptIncludes.h"

using namespace myscript;

int main()
{
    // VCZH_DEBUG_NO_REFLECTION is on
    // so we don't need, and are not allowed, to start reflection
    auto myapp = Ptr(new MyApp);
    myapp->CreateScripting()->Execute(L"Gaclib");

    // we need to call this because we use generated code from Workflow script
    FinalizeGlobalStorage();
}
```

