0
votes

I have a project with several folders:

folderA: header1.h header2.h
folderB: header1.h(modified) file1.cpp (includes header1.h and header2.h)

I want to write a Makefile that uses the header1.h from folderB and header2.h from folderA.

But if I write my Makefile this way:

LOCAL_C_INCLUDES += \
folderA\

Then I have errors at compiling saying some values are defined twice, even if the header files start with #pragma once.

Any idea how to achieve that?

I have also tried listing explicitly the header files in the makefile but it does not seem to work.

1
If your compiler support #pragma once the head file should not be included twice, I don't think it's the makefile reason, did you check your source code?A Lan
Which toolchain is this for: gcc or MS? Is modifying the sources an option? If so you could ignore the local includes and just change the source to #include "folder A/header2.h" or "folder B/header1.h".cup
u can try the #include "../FolderA/header2.h" (and remove the includes in the makefile)!hich9n
I smell a XY problem: Why would you want to create such a surprising setup? Usually the goal is to achieve a minimum of surprise. Are you perhaps trying to solve a different problem?DevSolar

1 Answers

0
votes

Regarding including the files I tend to just use the -Ipath/to/folder to each of the folder containing the includes. so in this case that'd be -Ipath/to/folderA -Ipath/folderB and then you can just write #include "header1.h" #include "header2.h" in your code.

include guards(both #ifndef and #pragma) doesn't work across compilation units, which is why you should never ever ever ever ever(seriously, it will only cause you pain the long run) define anything in a header file, only declare them.

This will get rid of the multiple definition problem. Why?

A compilation unit is one .cpp-file and all included headers. Each .cpp create an object file containing a middle stage binary representation of the code, this is the compilation stage. These object files are then linked together in the linking stage. Since each .cpp is handled separately in c++ if you have "float foo;" in header.hpp and both a.cpp and b.cpp include header.hpp, how would the compiler know which foo you mean when running the application?