Question

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.

Was it helpful?

Solution

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:

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top