Use NMAKE to make all source in a directory?

后端 未结 6 1416
盖世英雄少女心
盖世英雄少女心 2021-01-21 15:57

Using nmake, is it possible to have the makefile build all the .cpp files in the current directory automatically, without having to specify them individually?

So, instea

相关标签:
6条回答
  • 2021-01-21 16:19

    I think this can be done with wild cards in GNU make (and IIRC you can run it on windows). Aside from that, I'm sorry but I don't known nmake.

    0 讨论(0)
  • 2021-01-21 16:24

    You can use a rule like this:

    {src\mystuff}.c{tmp\src\mystuff}.obj::
        $(CC) /nologo $(CFLAGS) /c /Fotmp\src\mystuff\ $<
    

    which will find and compile all the .c files in src\mystuff and put the object files in tmp\src\mystuff. Substitute .cpp for .c in your case.

    Note that the first character on the second line should be a tab, not spaces.

    Also, $(CC) is predefined by nmake to be cl, and you can add any compiler flags you need to $(CFLAGS), hard-code them in the rule or add a different variable there, as you prefer.

    0 讨论(0)
  • 2021-01-21 16:26

    One possibility is to wrap the make in a script.

    Either use a for loop, calling make on each .cpp file or construct a list of cpp files and use it as a parameter to the makefile.

    0 讨论(0)
  • 2021-01-21 16:33

    You can run a shell command during nmakefile preprocessing, and then !include the output. Naturally the output needs to be in nmake format. Something like:

    !if [bash -c "echo O = *.cpp" >sources.mak]
    !error Failed to generate source list
    !endif
    !include sources.mak
    
    all: $(O:.cpp=.obj)
    
    .cpp.obj:
        ---COMPILE $< HERE---
    

    I suspect that most people would not use bash to create sources.mak, but you get the idea.

    0 讨论(0)
  • 2021-01-21 16:37
    .cpp.obj:
        $(CC) $(CFLAGS) $*.cpp
    

    is a default rule that will automatically resolve .obj dependencies from .cpp files...

    0 讨论(0)
  • 2021-01-21 16:45

    Just

    main.exe: *.c resource.RES
        @$(MAKE) $(**:.c=.obj)
        link $(**:.c=.obj) /OUT:$@
    

    and you don't need to add anything else! Every time you nmake, all *.c will be checked if it is compiled or not.

    Where $** for the dependencies, $@ for the target.

    $(**:.c=.obj) is a string substitute, replacing the .c suffix with .obj. And when it comes to resource.RES, it will not replace anything.

    Note that there is a recursive call. This is the most elegant approach, otherwise you can call cmd to print all *.c filenames to a text and load from it.

    0 讨论(0)
提交回复
热议问题