Search code examples
batch-filevisual-c++cmdcl

How to exclude files from compilation when using msvc (cl.exe)


I'm wondering how I would exclude a file from being built within my batch file. Currently, my cl.exe compiliation line within the batch file looks like this:

cl /c ..\source\*.c

so how would I say I would like to build all files EXCEPT "file_not_needed.c" from the build?


Solution

  • The file(s) with the file extension c to exclude can be temporarily renamed with changing the file extension to exclude them.

    A single command line for a single C source code file for usage in a command prompt window or a batch file:

    ren ..\source\file_not_needed.c file_not_needed.c.tmp & cl.exe /c ..\source\*.c & ren ..\source\file_not_needed.c.tmp file_not_needed.c
    

    A multi-line solution for multiple C source code files for usage in a batch file:

    for %%I in (file_not_needed "one more file not needed") do ren "..\source\%%~I.c" "%%~I.c.tmp"
    cl.exe /c ..\source\*.c
    for %%I in ("..\source\*.c.tmp") do ren "%%I" "%%~nI"
    

    All the files to exclude must be specified inside the round brackets of first FOR loop using a space, comma or semicolon as separator between the file names without file extension.