简体   繁体   中英

(make/g++) include full path of auto-generated dependency targets? (or workaround)

[short version] I can't have two source files with the same name in my unit tested c++ project even though they are in different folders. g++ and make are grumpy with each other. (Or maybe I'm being stupid)

[long version] I've run into a problem with my make/g++/gtest setup. When g++ auto-generates dependencies, it generates something like this:

event_handler.o: src/os/event_handler.h src/os/event.h

But I need something like this (full path of target):

src/os/event_handler.o: src/os/event_handler.h src/os/event.h

The reason is when I have a file like this:

src/os/event_handler.cpp // contains EventHandler class

I have a companion file like this:

test/src/os/event_handler.cpp // contains EventHandlerTest unit test class

... so their .o files both just show up as event_handler.o in the auto-generated dependency list. Is there a way to force g++ to give the full path or do I need to change the names of my test files to something like:

test/src/os/event_handler_test.cpp

I wasn't able to find anything online or in the documentation.

There are two solutions.

  1. Use -MMD to compile and create dependencies in one step, which will generate .d files corresponding to the object files.
  2. Use -MT or -MQ to specify the path to the object when generating prerequisites.

Personally I prefer the MMD solutions, as it also tends to work better with buggy header files. See http://gcc.gnu.org/onlinedocs/gcc/Preprocessor-Options.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM