Null
Null

Reputation: 389

How to handle rename operation on dependencies in makefile?

I am building a simple C++ app on Linux using make command. I want to test my code with different set of information defined in two different header files: define.h and define.hh.

"define.h" is the only one used in my build system and dependencies are defined properly.

When I edit "define.h", make rebuilds the project.

When I swap the names of "define.h" and "define.hh" using terminal, make doesn't bother to rebuild.

main.cpp:

#include "define.h"
#include <iostream>
int main()
{
    std::cout << MSG << std::endl;
}

define.h:

#define MSG "1"

define.hh:

#define MSG "2"

makefile:

CPPFLAGS:=-g
LDLIBS:=-lstdc++

main: main.o

main.o: main.cpp define.h

After swapping names of "define.h" and "define.hh" using VSCode GUI, make rebuilds the project.

It seems that renaming files in terminal doesn't update their timestamp, but VSCode updates timestamp after rename operation.

Is there a way to handle this situation in my makefile?

Should we expect make to be smarter in the future and handle this situation? (like cashing information about file nodes)

Is this something that should be part of OS standards? (like update timestamp after rename, or define a new timestamp for rename)

Upvotes: 0

Views: 179

Answers (1)

Nellie Danielyan
Nellie Danielyan

Reputation: 1011

You can run touch <filename> after renaming. This should update the modification timestamp.

EDIT: another option to achieve what you want would be to have two files define1.h and define2.h and add an argument to your make command depending on which make will decide which one to take. You could run make like this: make target DEFINEH=1or make target DEFINEH=2 And in your makefile:

ifeq ($(DEFINEH),1) cp define1.h define.h touch define.h else ifeq ($(DEFINEH),2) cp define2.h define.h touch define.h endif

This way you will never forget to touch the file.

Upvotes: 2

Related Questions