1、之前用在Eclipse用添加插件的方法要将ASN.1编译为C,但是 一直觉得不会用,而且有点麻烦。具体步骤参考http://es.asnlab.com/asncc/asnrt.html
2、今天花费一天,在terminal上用命令行编译ASN.1:
(1)、gcc已经安装,不累赘。
(2)、很多教程的编译器是基于github那一版,但是我在运行的时候总出现config status: ...can't find /.../Makefile.in文件;查询资料大家基本用以下方法:
aclocal
libtoolize –force
automake –add-missing
autoconf
autoheader
make clean
但是并没有什么用[捂脸]
(3)、换了个安装包http://lionet.info/asn1c/download.html asn1c-0-9.28
解压
打开install.md---->打开REQUIREMENTS.md---->安装依赖----->automake和bison需要安装源,解压、./configure、make、sudo make install;其他sudo apt-get install----->按照install.md中步骤继续安装
注意:上述所有./confiure 就只是./configure 不需要增加特意指定安装路径(./configure --prefix /.../)
(4)、在asn1c-0-9.28文件中建立新的文件夹Rectangle并在文件夹中建立Rectangle.asn其内容为
RectangleTest DEFINITIONS ::= BEGIN
Rectangle ::= SEQUENCE {
height INTEGER, -- Height of the rectangle
width INTEGER -- Width of the rectangle
}
END
执行命令:asn1c -fnative-types rectangle.asn1
添加mian.c;内容为:
#include#include#includechar tab[8];
/*
* This is a custom function which writes the
* encoded output into a global test table
*/
static int decode_callback(const void *buffer, size_t size, void *app_key)
{
static int i = 0;
memcpy(&tab[i],buffer,size);
i += size;
}
int main()
{
Rectangle_t *rectangle; /* Type to encode */
asn_enc_rval_t ec; /* Encoder return value */
/* Allocate the Rectangle_t */
rectangle = (Rectangle_t*)calloc(1, sizeof(Rectangle_t)); /* not */
if(!rectangle) {
perror("calloc() failed");
exit(71); /* better, EX_OSERR */
}
/* Initialize the Rectangle members */
rectangle->height = 42; /* any random value */
rectangle->width = 23; /* any random value */
/* Encode the Rectangle type as BER (DER) */
ec = der_encode(&asn_DEF_Rectangle,
rectangle, decode_callback, tab);
if(ec.encoded == -1) {
fprintf(stderr,
"Could not encode Rectangle (at %s)\n",
ec.failed_type ? ec.failed_type->name : "unknown");
exit(65); /* better, EX_DATAERR */
} else {
fprintf(stderr, "Created %s with BER encoded Rectangle\n",
"");
}
/* Also print the constructed Rectangle XER encoded (XML) */
xer_fprint(stdout, &asn_DEF_Rectangle, rectangle);
return 0;
}
执行命令: gcc -I.-o recode *.c
出现错误:
/tmp/cc051a95.o: In function `main':
main.c:(.text+0x121): multiple definition of `main'
/tmp/ccYb7ZNs.o:converter-sample.c:(.text+0x1b4): first defined here
/tmp/ccYb7ZNs.o:(.data+0x0): undefined reference to `asn_DEF_PDU'
collect2: error: ld returned 1 exit status
第一个错误,因为converter-sample.c中含有main函数,直接将converter-sample.c删掉,然后第二个错误也没了。
(4)、编译后生成recode文件,输入命令:./recode。
完成。
补充:成熟版本:不需要删除convert.sample文件;不需要补充main.c文件
sudo apt-get insall asn1c
asn1c -fnative-types *.asn
gcc -DPDU=BasicSafetyMessage -I. -o recode *.c /编译
网友评论