tags:

views:

389

answers:

3

Hello, example use of "xargs" application in Unix can be something like this:

ls | xargs echo

which is the same as (let's say I have "someFile" and "someDir/" in the working dir):

echo someFile someDir

so "xargs" take its "input" and place it "at the end" of the next command (here at the end of echo).

But sometimes I want xargs to place its input somewhere "in the middle" of next command. For example:

find . -type f -name "*.cpp" -print | xargs g++ -o outputFile

so if I had in the current directory files "a.cpp" "b.cpp" "c.cpp" the output would be the same as with the command:

g++ -o outputFile a.cpp b.cpp c.cpp

but I want to have something like this:

g++ a.cpp b.cpp c.cpp -o outputFile

Is there a way to do it?

P.S.: I need it in some cases, because e.g.:

i586-mingw32msvc-g++ -o outputFile `pkg-config --cflags --libs gtkmm-2.4` a.cpp b.cpp c.cpp

doesn't work but this one works fine:

i586-mingw32msvc-g++ a.cpp b.cpp c.cpp -o outputFile `pkg-config --cflags --libs gtkmm-2.4`

Thanks.

Petike

+4  A: 

If your version of xargs doesn't include the -I feature, an alternative is to write a little shell script containing the command you want to execute:

#!/bin/sh
exec i586-mingw32msvc-g++ "$@" -o outputFile...

Then use xargs to run that:

find . -type f -name "*.cpp" -print | xargs my_gcc_script
Kenster
+3  A: 

You do not need xargs for this. Just use:

g++ `find . -type f -name '*.cpp'` -o outputFile
mark4o
I'd say use $() over back ticks, but it works so I can't complain.
scragar
I used backtick because he didn't say what shell he was using, and backtick works with more of them (e.g. tcsh).
mark4o
A: 

GNU Parallel http://www.gnu.org/software/parallel/ would be a solution too:

find . -type f -name "*.cpp" -print | parallel -X g++ {} -o outputFile
Ole Tange