0

I'm trying to link all my files with object *.o extension in my directory. I tried to use:

for i in $(find . -name "*.o" -type f); 
    do
        echo $i >> myFiles  
    done

Then I need:

gcc -o myFile <myFiles

gcc: fatal error: no input files

compilation terminated.

n. m. could be an AI
  • 112,515
  • 14
  • 128
  • 243
user3193813
  • 147
  • 1
  • 1
  • 8

2 Answers2

1

I see several problem in your approach:

find should not be used in a loop, but rather with a -fprint <file>. So in your case:

find . -name "*.o" -type f -fprintf myfiles

Secondly, redirecting your file to gcc stdin will not work as you think, as it uses the input as the source of the code: see this question. What you want instead is to expand the list of objects to a list of arguments:

cat myfiles | xargs gcc -o myFile

xargs does it nicely. But as @n.m. mentioned, you could do everything at once with a command substitution:

gcc -o myfile $(find . -type f -name *.o -print0)

The only difference I suggest is to use a -print0 so that find put a \0 at the end of a find instead of a \n.

Good Luck

Community
  • 1
  • 1
jraynal
  • 507
  • 3
  • 10
1

Try this one Then you will be able to complile the code.

gcc -o (filename filename.c)

example gcc -o nikesh nikesh.c