< Back To ASN1C Support

ASN1C C/C++ Tutorial

Getting Started with the ASN1C C / C++ Command Line

This tutorial will help users use the ASN1C complier using the terminal shell prompt. Please note that tutorial assumes you are operating in a Linux system.

Change directory to one of the employee sample directories.

cd c/sample_ber/employee

Change the PATH variable by typing:

PATH=$HOME/asn1c-v65x/bin:$PATH
export PATH

Note: This assumes ASN1C was installed into the login root directory. Also, x is to be replaced by a minor version number.

Run ASN1C to compile the files.

To compile the included employee example, type the following on the command line:

For C:

asn1c employee.asn -c -ber

For C++:

asn1c employee.asn -c++ -ber

Run the C / C++ compiler.

Note: This tutorial shows how to use the GNU gcc/g++ compiler, but the procedure for other compilers would be similar.

At a minimum, the -c and -I options are necessary to compile the employee.c (or employee.cpp) file. -I is used to specify include directories. For BER, the rtsrc and rtbersrc directories would need to be included, therefore, the command line argument would be:

For C:

gcc -c -I $(HOME)/asn1c-v65x/rtsrc -I $(HOME)/asn1c-v65x/rtbersrc employee.c

For C++:

g++ -c -I $(HOME)/asn1c-v65x/rtsrc -I $(HOME)/asn1c-v65x/rtbersrc employee.cpp

The same command would be required to compile the reader.c and writer.c programs.

Link the compiled code with the runtime libraries.

For C:

gcc -o writer writer.o employee.o -L$(HOME)/asn1c-v65x/c/lib -lasn1ber -lasn1rt
gcc -o reader reader.o employee.o -L$(HOME)/asn1c-v65x/c/lib -lasn1ber -lasn1rt

For C++:

g++ -o writer writer.o -L$(HOME)/asn1c-v65x/cpp/lib -lasn1ber -lasn1rt
g++ -o reader reader.o -L$(HOME)/asn1c-v65x/cpp/lib -lasn1ber -lasn1rt
Note: A makefile and a Visual Studio project file (Microsoft Windows only) contain all of these commands. These files come with the ASN1C compiler.