Search code examples
c++makefilecompilationnmake

Multiple source paths in batch-mode rule [nmake]


Im using the batch-mode rule for my makefile. Currently i have the following targets:

DIR_SRC = src
DIR_INCLUDE = include
DIR_LIB = lib
DIR_BIN = bin\x64
DIR_BUILD = build\x64    

{$(DIR_SRC)}.cpp{$(DIR_BUILD)}.obj ::
            @echo Compiling...
cl /c /EHsc /Fo$(DIR_BUILD)\ /MD /I$(DIR_INCLUDE) $<
    
$(EXECUTABLE_NAME) : $(DIR_BUILD)\*.obj
   @echo Linking $(EXECUTABLE_NAME)...
   link /out:$(DIR_BIN)\$(EXECUTABLE_NAME) $(DIR_BUILD)\*.obj

The "bin/x64/*.obj" target uses all cpp files inside the "src" folder. Is it possible to add another source path to it?

I want something like this

{$(DIR_SRC) $(ANOTHER_DIR)}.cpp{$(DIR_BUILD)}.obj ::

Solution

  • If you add a second build directory to match your second source directory, you can get this to work. For example, if we modify your makefile to be:

    DIR_SRC = src
    DIR_SRC2 = another
    DIR_INCLUDE = include
    DIR_BIN = bin\x64
    DIR_BUILD = build\x64
    DIR_BUILD2 = build_another\x64
    EXECUTABLE_NAME = foo.exe    
    
    {$(DIR_SRC)}.cpp{$(DIR_BUILD)}.obj ::
        @echo Compiling...
        cl -nologo /c /EHsc /Fo$(DIR_BUILD)\ /MD /I$(DIR_INCLUDE) $<
        
    {$(DIR_SRC2)}.cpp{$(DIR_BUILD2)}.obj ::
        @echo Compiling...
        cl -nologo /c /EHsc /Fo$(DIR_BUILD2)\ /MD /I$(DIR_INCLUDE) $<
        
    $(DIR_BIN)\$(EXECUTABLE_NAME) : $(DIR_BUILD)\*.obj $(DIR_BUILD2)\*.obj
        @echo Linking $(EXECUTABLE_NAME)...
        link -nologo /out:$@ $**
    

    then we will get:

    nmake -nologo
    
    Compiling...
            cl -nologo /c /EHsc /Fobuild\x64\ /MD /Iinclude src\*.cpp
    s1.cpp
    s2.cpp
    Generating Code...
    Compiling...
            cl -nologo /c /EHsc /Fobuild_another\x64\ /MD /Iinclude another\*.cpp
    a1.cpp
    a2.cpp
    Generating Code...
    Linking foo.exe...
            link -nologo /out:bin\x64\foo.exe build\x64\*.obj build_another\x64\*.obj
    

    where src contains s1.cpp and s2.spp, and where another contains a1.cpp and a2.cpp.