= Generating Code and Building

This section describes the steps needed to integrate ASN.1 modules into an existing {cpp} project using the TITAN Standalone ASN.1 Encoder.

NOTE: The instructions and examples in this section assume that the environment variable `$TTCN3_DIR` contains the installation path of TITAN.

== Compiling ASN.1 Modules and Generating Code

The Standalone ASN.1 Encoder uses the same `compiler` for ASN.1 modules as the TTCN-3 Toolset.

The command line option `--asn1enc` instructs the ASN.1 compiler to only generate the {cpp} code necessary for ASN.1 encoding and decoding. It is followed by the list of files containing ASN.1 modules.

Example:

[source]
----
$TTCN3_DIR/bin/compiler --asn1enc MyModule1.asn MyModule2.asn
----

This command analyses the ASN.1 modules `MyModule1.asn` and `MyModule2.asn`, and generates the {cpp} headers `MyModule1.hh` and `MyModule2.hh`, and the {cpp} source files `MyModule1.cc` and `MyModule2.cc` (if no syntax or semantic errors were found).

There are also command line options for disabling the generation of encoder/decoder functions for each codec. These are: `-b` for `BER`, `-j` for `JSON`, `-o` for `OER`, and `-p` for `PER`. These can only be added before the list of ASN.1 modules.

Example:

[source]
----
$TTCN3_DIR/bin/compiler --asn1enc -j MyModule1.asn MyModule2.asn
----

Similar to the previous example, except no `JSON` encoder/decoder functions are generated in this case.

== {cpp} Compilation and Linking

When compiling the generated {cpp} code, and any user source files that include headers generated by the ASN.1 compiler, the following pre-processor flags must be set:

- `-DTITAN_ASN1ENC`: this is needed by TITAN runtime headers;
- `-DSOLARIS`, `-DSOLARIS8`, `-DLINUX`, `-DFREEBSD`, `-DWIN32` or `-DINTERIX`: this indicates which platform the project is built on;
- `-I$TTCN3_DIR/include`: this sets the path to the TITAN runtime headers.

The following flags must be used when linking the final project:

- `-lasn1enc` or `-lasn1enc-dynamic`: links the static or dynamic Standalone ASN.1 Encoder base library;
- `-lcrypto`: links an OpenSSL library needed by TITAN;
- `-L$TTCN3_DIR/lib`: this sets the path to the TITAN base libraries.

Example:

[source]
----
g++ -c -DTITAN_ASN1ENC -DLINUX -I$TTCN3_DIR/include -o MyModule1.o MyModule1.cc
g++ -c -DTITAN_ASN1ENC -DLINUX -I$TTCN3_DIR/include -o MyModule2.o MyModule2.cc
g++ -c -DTITAN_ASN1ENC -DLINUX -I$TTCN3_DIR/include -o OtherUserCode.o OtherUserCode.cc

g++ -o MyExecutable MyModule1.o MyModule2.o OtherUserCode.o -lasn1enc -lcrypto -L$TTCN3_DIR/lib
----
