eliben / pycparser

:snake: Complete C99 parser in pure Python
Other
3.26k stars 612 forks source link

undefined reference to `assert' #495

Closed nguyenthanhvuh closed 1 year ago

nguyenthanhvuh commented 1 year ago

Hi,

this is my test code, which compiles fine with gcc

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>

int mainQ(int x, int y){
    assert(x >= 1 && y >= 1);

    return 0;
}

void main(int argc, char **argv){
    mainQ(atoi(argv[1]), atoi(argv[2]));
}

but then after parsing its ast with

    ast_instr = parse_file(filename, use_cpp=True,  cpp_path='gcc',
                    cpp_args=['-E', r'-I../EXTERNAL_FILES/pyparser/utils/fake_libc_include'])
    generator = c_generator.CGenerator()
    instr = generator.visit(ast_instr)

compiling the contents of instr gives an error about undef ref to assert.

The contents of instr looks something like

....
typedef _Atomic intmax_t atomic_intmax_t;
typedef _Atomic uintmax_t atomic_uintmax_t;
typedef struct atomic_flag
{
  atomic_bool _Value;
} atomic_flag;
typedef enum memory_order
{
  memory_order_relaxed,
  memory_order_consume,
  memory_order_acquire,
  memory_order_release,
  memory_order_acq_rel,
  memory_order_seq_cst
} memory_order;
int mainQ(int x, int y)
{
  assert((x >= 1) && (y >= 1));
  return 0;
}

void main(int argc, char **argv)
{
  mainQ(atoi(argv[1]), atoi(argv[2]));
}

did I use preprocessing incorrectly somehow?

eliben commented 1 year ago

Note that the C generator is not meant to generate back proper compilable code, since there's a lot of loss in the process. It can - at best - be used for rewriting small code snippets or generating self-contained code samples that don't rely on the standard library.