Add a #include line to all c/c++ plus files

R

RedDevilDan

I am new to Perl. In linux/unix, is there anyway to add the #include
line to all *.c or *.cpp files automatically?

I have a large project which contains thousands of .c or .cpp source
files. However, I decided to #include a common header file into all
source files in order to config them globally. Is there anyway to do
that easily?
 
K

krakle

I am new to Perl. In linux/unix, is there anyway to add the #include
line to all *.c or *.cpp files automatically?

I have a large project which contains thousands of .c or .cpp source
files. However, I decided to #include a common header file into all
source files in order to config them globally. Is there anyway to do
that easily?

Yea. With Perl.

#!/usr/bin/perl -w

use strict;

That'll get you started :)

Then you COULD loop through an array of file names (for *.c and *.cpp)
opening and writing to the top of each file then closing to save.
That's a very basic and easy approach to accomplish what you wish to
do. Even with very little Perl programming experience you should be
fine.
 
J

Josef Moellers

Yea. With Perl.

#!/usr/bin/perl -w

use strict;

That'll get you started :)

Then you COULD loop through an array of file names (for *.c and *.cpp)
opening and writing to the top of each file then closing to save.
That's a very basic and easy approach to accomplish what you wish to
do. Even with very little Perl programming experience you should be
fine.

NBTNDT, but

* have a look at "perldoc -q insert"

* File::Find might be interesting, if the thousands of source files do
not reside in a single directory.

Josef
 
M

Martijn Lievaart

I am new to Perl. In linux/unix, is there anyway to add the #include
line to all *.c or *.cpp files automatically?

I have a large project which contains thousands of .c or .cpp source
files. However, I decided to #include a common header file into all
source files in order to config them globally. Is there anyway to do
that easily?

Probably. Something along the lines of (greatly untested)

$ find . -name "*.c" -o -name "*.cpp" | while read f ; do
perl -p -i'.bak' -e 'BEGIN { print "include <myheader.h>\n"; }' "$f"
done

Assuming you are on Unix. If on Windows, install Cygwin to get roughly the
same benefits as we Unix users have from a great commandline that lets us
automate such tasks easily.

M4

(No seriously, this question is one of those that can be easily solved on
Unix, but is pretty hard in plain Windows, even with Perl installed. I use
both every day intensively and know cmd.exe tricks that make even Windows
experts pause. Still after a (admittedly steep) learning curve, the Unix
command prompts are just so powerful, you'll never go back.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,787
Messages
2,569,631
Members
45,338
Latest member
41Pearline46

Latest Threads

Top