How to combine multiple small C programs together? [closed] How to combine multiple small C programs together? [closed] unix unix

How to combine multiple small C programs together? [closed]


Depends on what level you're viewing this from. If you want to combine several pre-built programs at the command line, you can use a pipe like with Unix, e.g. dir | sort.

If you're developing and want to reuse existing code, you can link the existing functionality to your application as libraries, or simply reuse existing classes.


You mentioned git, which is known for originally being a collection of small tools each performing a relatively small operation on the repository, and a set of scripts (shell and Perl) that use the tools. For example, you can take a look at the code of git pull (which is actually a shell script) and see how it calls different git programs that most git users don't know about.

Generally, if you want to write a part of your program as a filter, let it simply read the input from stdin (using fread, fgets, fscanf, etc) and write the output to stdout (fprintf, fwrite, etc). Then you can call your filter in a shell script using the pipe.

Another way of combining programs is via bidirectional interprocess communication, that is, not via pipe in a shell script but using e.g. sockets. You can split the program to two parts, a server and a client, which communicate with each other but have separate objectives. For example the X system and FreeCiv are written this way.

There are programs which aren't easily decomposable to multiple smaller programs and filters. In that case, it's usually best to decompose the program to libraries, which is also part of Unix philosophy as the libraries can also be reused by other programs.

I'd also recommend looking at The Art of Unix Programming, which goes into more detail on software engineering and the Unix philosophy.