Translating Dependent Console Application
Translating Dependent Console Application
The following example demonstrates how to translate two C# projects one being a console application C# project and another – a library project on which the console application C# project depends. We’ll use pre-existing projects from DependentConsoleApp example located here.
This example consists of two C# projets – CommonLib and DependentConsoleApp.
Translating CommonLib
CommonLib is a library project consisting of a single .cs source file SomeClass.cs and a project file CommonLib.csproj. This project does not have any special dependenies on other projects or 3-rd party assemblies.
Also CommonLib project directory contains precreated configuration file CommonLib.translator.config
. Let us have a closer look at the configuration file.
CommonLib.translator.config is quite simple. It begins with an XML declaration, which specifies that the file contains an XML document.
Then goes the XML root element <translator> which is mandatory for Translator configuration XML document
<translator>
Next, the default Translator configuration file is imported using <import> element. The default configuration assigns default values to all configuration options.
<import config="translator.config"/>
And the XML document is finished with closing tag of the root element <translator>:
</translator>
This example assumes that C# CommonLib library project should be translated into a C++ static library project, which is a default setting.
With C# project and configuration file ready, we can convert the project.
In order to covert BaseLibrary project we run CMD and navigate to the directory with translator binary:
>cd C:\CodePorting.Translator_Cs2Cpp_19.4\bin\translator
And run Translator:
>CodePorting.Translator.Cs2Cpp.exe -c C:\DependentNUnitTest\CommonLib\CommonLib.translator.config C:\DependentNUnitTest\CommonLib\CommonLib.csproj C:\output
Translator will print some logs of the translating process to the console window and when it finishes translating, directory C:\output
will contain a directory named CommonLib.Cpp
containing the generated C++ source files and Cmake configuration files.
Now we want to use Cmake to generate makefile/project files. Let it be a Visual Studio 2017 x86 project file. In CMD we navigate to the C:\output\CommonLib.Cpp
directory
>cd C:\output\BaseLibrary.Cpp
And run Cmake in configuration mode:
>Cmake --G "Visual Studio 15 2017" .
And now we can build the sources using either Cmake or Visual Studio. Let us use Cmake:
>Cmake --build . --config Release
The library is built.
Translating DependentConsoleApp
DependentNUnitTest is a console application C# project that consists of a single .cs source file Program.cs
and a project file DependentConsoleApp.csproj
. This project has a dependency on previously translated project CommonLib. This dependency has to be reflected in the DepedentConsoleApp project’s configuration file. In our example this configuration file is pre-created, its name is DepedentConsoleApp.translator.config
and it is located in the project’s directory DependentConsoleApp
. Let us have a closer look at the configuration file.
DependentConsoleApp.translator.config begins with an XML declaration, which specifies that the file contains an XML document
Then goes the XML root element <translator> which is mandatory for Translator configuration XML document
<translator>
Next, the default Translator configuration file is imported using <import> element. The default configuration will assign default values to all configuration options
<import config="translator.config"/>
Also we need to import a configuration file include_map.config from translated CommonLib project that maps public types exported by CommonLib library to generated C++ header files in which these types are declared. include_map.config is generated by Translator for each project it translates. Thus, before translating DependentConsoleApp project, CommonLib project has to be translated first so that Translator generates include_map.config. This is how include_map.config is included in DependentConsoleApp.translator.config
<import config="../../output/CommonLib.Cpp/include_map.config" />
Here ../../output is a directory that was passed as an output directory to Translator when CommonLib project was translated.
Next, we want Translator to add some commands to the output CMakeLists.txt. We do that by adding <cmake_commands> element to the configuration file containing raw Cmake commands
<cmake_commands>
<![CDATA[
The first command sets the output directory for the executable binary by setting the corresponding property on the target ${PROJECT_NAME}
set_target_properties(${PROJECT_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/../bin")
Here ${PROJECT_NAME} is the name of the Cmake project that is equal to the name of the main Cmake executable target.
Then the <cmake_commands> element is closed
]]>
</cmake_commands>
Then, we need to tell Translator that DependentConsoleApp project depends on CommonLib library. We do it using <lib> element:
<lib name="CommonLib.Cpp" csname="CommonLib">
<cmake_link_template>
<![CDATA[
find_package(CommonLib.Cpp REQUIRED CONFIG PATHS "${CMAKE_CURRENT_SOURCE_DIR}/../CommonLib.Cpp" NO_DEFAULT_PATH)
target_link_libraries(${PROJECT_NAME}_dependencies INTERFACE CommonLib.Cpp)
]]>
</cmake_link_template>
</lib>
Here ${PROJECT_NAME}dependencies is the name of the Cmake Interface Library target that is defined in the output _CMakeLists.txt file and is linked to main executable target ${PROJECT_NAME}. Thus libraries linked to ${PROJECT_NAME}_dependencies get automatically linked to ${PROJECT_NAME} target.
Finally the XML document is finished with closing tag of the root element <translator>
</translator>
With the C# project at hand and configuration file ready, we can convert the project.
In order to covert DependentConsoleApp project we run CMD and navigate to the directory with translator binary:
>cd C:\CodePorting.Translator_Cs2Cpp_19.4\bin\translator
And run Translator:
>CodePorting.Translator.Cs2Cpp.exe -c C:\DependentConsoleApp\DependentConsoleApp\DependentConsoleApp.translator.config C:\DependentConsoleApp\DependentConsoleApp\DependentConsoleApp.csproj C:\output
Translator will print some logs of the translating process to the console window and when it finishes translating, directory C:\output
will contain a directory named DependentConsoleApp.Cpp
containing the generated C++ source files and Cmake configuration files.
Now we want to use Cmake to generate makefile/project files. Let it be a Visual Studio 2017 x86 project file. In CMD we navigate to the C:\output\DependentConsoleApp.Cpp
directory
>cd C:\output\DependentConsoleApp.Cpp
And run Cmake in configuration mode:
>Cmake --G "Visual Studio 15 2017"
And now we can build the sources using either Cmake or Visual Studio. Let us use Cmake:
>Cmake --build . --config Release
When build finishes, directory D:\output\bin\Release should contain two files: DependentConsoleApp.Cpp.exe
, which has just been built from C++ sources, and aspose_cpp_vc140.dll
, which was copied from Translator installation directory during a post-build step. When we run DependentConsoleApp.Cpp.exe its output to the Console window should be similar to the output of the original C# application project we translated.