I am doing a C project which involves lots of static linking.

To get rid of "implict function declaration" errors I will use a simple solution - a header file with function prototypes.

I wrote this script:

for file_to_parse in `find -type f -name "*.c"`; do
    cproto $file_to_parse > proto.h 2> /dev/null

It should create a file called proto.h and store function prototypes there.


find -type f -name "*.c" this command returns the pathnames of all c files in the directory of this script.

eg ./folder1/file1.c

cproto <i>file</i> => this creates function prototypes from functions found in file.

eg <code>double myfunction(int a,char b);


The code generates an empty file.

What is wrong with this bash script?


There are surely many ways to change it. Here is but one:

 if [ ! -f proto.h ]; then
       touch proto.h
 for file_to_parse in `find -type f -name "*.c"`; do
    cproto $file_to_parse >> proto.h 2> /dev/null

The error is in the redirection in the cproto... line: the simple > first wipes the destination file, then writes into it. Instead, >> appends to the destination file without destroying it. The if loop is there just to make sure that at the first pass you will not be appending to a non-existent file: there is nothing wrong with appending to a non-existent file, no error is thrown; it's just that I like to make sure it is a simple, regular file, before writing to it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.