I'm trying to link all *.o files from another folder and would like to in my Makefile do something like;
g++ *.o (folder/*.o and not folder/main.o) -o bin
Is there someway I can use somekind of simple regex here?
I'm trying to link all *.o files from another folder and would like to in my Makefile do something like;
g++ *.o (folder/*.o and not folder/main.o) -o bin
Is there someway I can use somekind of simple regex here?
Try g++ *.o $( echo folder/*.o |tr ' ' "\n" | egrep -v folder/main.o )
, assuming none of folder/*.o
have SPACES in their names.
Here's how it works:
echo folder/*.o
produces a space-separated list of the filenames, with the directory.
tr ' ' "\n"
changes all the spaces to newlines (giving us one filename per line, which makes egrep
happy.
egrep -v folder/main.o
eliminates the undesired filename from this list.
$()
puts the rest of the filenames on the command line. Since $IFS
defaults to SPACE, TAB, NEWLINE, we don't have to tr
them back to spaces.
g++ *.o $( ls folder/*.o | egrep -v main.o )
for ls separates each item with a newline already.
Jan 16, 2012 at 18:19
You should use makefiles, or even better autotools, but you can do something like this:
ls folder/ | grep "*.o" | grep -v "main.o" | while read -r file; do g++ $file -o bin; done
This question would be better answered in a bash related stack site anyway haha.
This should work:
ls -1 folder/*.o | grep -v main.o | xargs -I{} g++ {} -o bin
Explanation:
ls -1 folder/*.o -> list all .o files (one per line) in folder folder
grep -v main.o -> exclude main.o from the list
xargs -I{} g++ {} -o bin -> execute "g++ file -o bin"