0

I have some c files that I want to compile separately and link them together using a bat file. I will compile/link them using gcc.

The problem is that new c files might be added and I want the bat file to compile/link everything without knowing the exact number of files(without modifying the bat file).

Something like :

for X=1:number_of_c_files
    gcc -c sourceX.c

and then link them together.

All the files are named compile_to_cX.c , where X is a value from 1 to the number of c files from the directory.

Thanks in advance !

Jabberwocky
  • 40,411
  • 16
  • 50
  • 92

1 Answers1

2

It's quite trivial.

for %%i in ( *.c ) do (
    gcc -c "%%i"
)

ETA

I don't remember if you can specify *.o to gcc, but I think you can, in which case it would be something like:

gcc *.o [options]

Another option would be to accumulate the .o file names into an environment variable.

setlocal ENABLEDELAYEDEXPANSION
set O_FILES=
for %%i in ( *.o ) do (
    set O_FILES=!O_FILES! %%i
)

gcc %O_FILES% [other options]

Really, you should probably use a makefile though.

Rob K
  • 8,326
  • 2
  • 29
  • 33