How to ensure that the target runs before all other build rules in the makefile?

I have a C ++ project that contains a generated file that all other C ++ files depend on. I am trying to get this file to be generated and compiled before starting any other compilation. Usually it would be as simple as moving this file to the target all:, but the complication is that my Makefile is also generated by the build system, and I can only add fragments to the Makefile and not edit it at all.

So, is there a way to force the created landing page of a file to force using dependencies or otherwise? I was thinking of using something like this:

cpp_files := $(wildcard src/*.cpp)
$(cpp_files): generated_file.cpp
generated_file.cpp:
    # generate the file here

But that does not work. For reference, my original dir structure looks like this, so I need to compile cpp files recursively:

src/
|---file1.cpp
|---file2.cpp
|---subdir1/
    |---file3.cpp

gen/
|---generated_file.cpp
+5
source share
2 answers

If you are sure that what you really want is one of the ways to do this: there is a rule for a dummy file that the makefile will have include.

.PHONY: dummy
dummy:
    # generate the file here

-include dummy

No matter what target you specify, Make first starts the rule dummyand then restarts itself.

+17
source

Actually, other files are cppnot dependent on the generated one (in terms of Make rules). The dependency graph is still as follows:

program
 |
 ^- file1.o
 |   ^- file1.c
 |
 ^- file2.o
 |   ^- file2.c
 |
 ^- generated_file.o
     ^- generated_file.c
         ^- <generator prerequisites>

- ( , #include d ).

cpp, makefile:

srcs := $(wildcard src/*.cpp src/*/*.cpp)

gen_file := gen/generated_file.cpp
srcs += $(gen_file)

objs := $(srcs:.cpp=.o)

.PHONY : all
all : program

program : $(objs)
    @$(LD) ...

%.o : %.c
    @$(CC) ...

$(gen_file) :
    # generate the file here

UPD.

, -.

-include generator-task
.PHONY : generator-task

generator-task : $(gen_files)

$(gen_files) : $(gen_prerequisites)
    # generate the file here

, Make, , .

+2

All Articles