Skip to content

dmvassallo/cgrep

Repository files navigation

cgrep - simple console grep

This utility shows how to use C and C++ together in a program. It is a simple console-based grep utility, like egrep but less powerful. The specific scenario this demonstrates is calling C++ code from C code.

It is equally important to be able to call C code from C++ code, but that isn't covered here, because it's simpler and less in need of demonstration. One need not (hardly ever) write wrapper functions to allow C code to be consumed in C++, because C++ is mostly compatible with C and supports most of its type system.

The C code here is compatible with C89.

License

0BSD. See LICENSE.

Building

You can build with CMake, or manually, on both Unix-like systems and Windows. If your IDE supports CMake (this includes VS Code with the recommended C++ extensions), then you don't need to use the command-line. If you build manually, the specific commands vary depending on your compiler.

See building.md for build instructions.

Parts of this program

In C:

  • cgrep.c contains the program entry point (main function), and basic logic to parse command-line arguments to report results and errors.
  • For other functionality, the C code calls into C++.

In C++:

The header files—match.h, noreturn.h, and readln.h—are written to be usable, without modification, in either C or C++ (and are used in that manner).

About

Demonstration of combining C and C++ code

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages