I have a task of developing source analysis tool for C programs, and I need to pre-process code before the analysis itself. I was wondering what is the best library for this. I need something light-weight and portable.
1 Answer
Instead of rolling out your own, why not use cpp that's part of the gcc suite: http://gcc.gnu.org/onlinedocs/gcc-4.6.1/cpp/
CPP(1) GNU CPP(1) NAME cpp - The C Preprocessor SYNOPSIS cpp [-Dmacro[=defn]...] [-Umacro] [-Idir...] [-iquotedir...] [-Wwarn...] [-M|-MM] [-MG] [-MF filename] [-MP] [-MQ target...] [-MT target...] [-P] [-fno-working-directory] [-x language] [-std=standard] infile outfile Only the most useful options are listed here; see below for the remainder. DESCRIPTION The C preprocessor, often known as cpp, is a macro processor that is used automatically by the C compiler to transform your program before compilation. It is called a macro processor because it allows you to define macros, which are brief abbreviations for longer constructs. 5 Comments
Kijan
This was also my basic idea, but i was wondering if there was any way to do it, without using
cpp.Brian Gerard
I guess I'm left wondering why you would need to do it without using
cpp. Ie - there exists a mature solution for the problem you posed. What is it about that solution that is insufficient to the task?Kijan
I was just wondering if there was a way to do it, other than
cpp, maybe something more efficient. Anyway, i will probably use cpp, because of its portability.sigjuice
@Kijan what part of your cpp did you find inefficient?
Jordan
Regardless of efficiency, using a C preprocessor as a library from inside another application is a legitimate use-case. Let's say I get a C file as a buffer and I want to preprocess it. Writing it to a file and calling a command-line tool on it is way overkill.