0

I m trying to write a very simple C program who send an ASN1 number throught sockets. this is the ASN.1 definition

Module DEFINITIONS ::= BEGIN

  M1 ::= SEQUENCE
    {
      number INTEGER  --  integer to send
    } END

I generate the C classes. Now I think I have to create a number in the main class and encode it and decode it in the other side. But if I trie to create an M1 object

M1 a;
or 
M1 *a;

I got this error Type M1 could not be solved.

user567
  • 3,712
  • 9
  • 47
  • 80
  • which is your ASN.1 compiler? Have you included the header file that it generates? – jsantander May 09 '14 at 12:04
  • I compiled it online http://lionet.info/asn1c/download.html and yes I included the Header – user567 May 09 '14 at 12:09
  • 1
    "I generate the *C classes*"? – Werner Henze May 09 '14 at 13:50
  • Who says "M1 could not be solved"? Where is your code? – Werner Henze May 09 '14 at 13:51
  • @WernerHenze The OP starts with an [ASN.1](http://en.wikipedia.org/wiki/Abstract_Syntax_Notation_One) definition. ASN.1 is a language to define types (you can think of it as playing the role IDL plays for CORBA). ASN.1 is part of the OSI standard and it is extensively used in Telecom protocols, LDAP, SNMP and also in the encoding of security certificates.... So, in order to use it with your favourite language you must generate some code from that definition. Apparently the OP is using a nifty online compiler. – jsantander May 09 '14 at 15:28

1 Answers1

3

The problem is that you're compiling with a C-compiler.... so struct's must be referred with a full struct Name (or by the typedef'ed name if you have done so... as the generated code does).

I pasted your ASN.1 module using the URL you provided, downloaded the generated code and tried to use it.

Note that the generated definition contains (in M1.h)

#ifdef __cplusplus
extern "C" {
#endif

/* M1 */
typedef struct M1 {
    long     number;

    /* Context for parsing across buffer boundaries */
    asn_struct_ctx_t _asn_ctx;
} M1_t;

/* Implementation */
extern asn_TYPE_descriptor_t asn_DEF_M1;

#ifdef __cplusplus
}
#endif

Now I tried with mymain.C

#include "M1.h"

int main() {
   M1 a;
   struct M1 b;
   M1_t c;
}

compiling with g++ (i.e. as a C++ program), there's no complain

Now if I compile (same source) mymain.c with gcc, then I get:

x.c: In function ‘main’:
x.c:6:4: error: unknown type name ‘M1’
    M1 a;

Removing that line (i.e. not using M1, but using either struct M1 or M1_t to refer to the type), everything compiles fine.

In summary:

  • Choose your poison, either C or C++
  • If you're in C++, use M1 or M1_t (or struct M1)
  • If you're in C, use struct M1 or M1_t

For all the in and outs check:

Community
  • 1
  • 1
jsantander
  • 4,972
  • 16
  • 27