There is a tree of files:

folder |-src |-myfile.cpp |-myfile.h |-bin |-Makefile 

I want to write a Makefile so that the files in the src directory are compiled, but the object files are created in the current one (in the folder). And then, so that they link into one file in the bin directory.

At the moment I have a non-working Makefile. Swears at the lack of rules for myfile.o.

 TARGET=myfile OBJECTS=myfile.o XX=g++ CXXFLAGS=-std=c++11 all: ${TARGET} ${TARGET}: ${OBJECTS} ${XX} -o ${TARGET} ${OBJECTS} .co: ${XX} ${CXXFLAGS} -c "${INCLUDE}$<" -o "$@" 

How to properly implement the task?

  • Try to put the files file.cpp and file.h in an empty directory and execute make CXXFLAGS="-std=c++11" file in this directory. Perhaps the result will amaze you, and make you think that what you have in mind, in a sense, can be called "inventing problems out of the blue." - aleksandr barakin
  • Swears at the lack of rules for myfile.o - but it really is not. at least I don't see it, just like the make program. Please explain, at least in words, how myfile , myfile.o , file.cpp and file.h are related to each other. - aleksandr barakin
  • @alexanderbarakin here is more an experiment, not a fictional problem. I want to leave the directory with the source files as intact as possible. I understand that you can do it the way you showed it. But I specifically gave a simple example, so that the explanation could be simple. I am interested in how to specify in a Makefile separately where to get and where to throw. I don't just need to make the executable file. Everything is written in the question. - AccumPlus
  • @alexanderbarakin corrected the names. myfile.o is obtained by compiling myfile.cpp. myfile.h is obviously the header of this myfile.cpp. myfile is an executable file obtained by linking object files (in this case it is only myfile.o). - AccumPlus

2 answers 2

so that they link into one file in the bin directory

it means that you need to collect not myfile , but bin/myfile . I'm TARGET about the TARGET variable.

object files were created in the current

and indicate the purpose and prerequisites accordingly:

 %.o: src/%.cpp $(CXX) $(CXXFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c $(OUTPUT_OPTION) $< 

here the second line, the recipe, is taken from implicit rules , which can be seen in the output of the command make -p


it remains only to explain to the compiler where to look for the header file myfile.h (if, of course, the compiler itself does not “guess”). here I, as a non-programmer, is unlikely to help. unless I assume that I need to add the option -I src to the value of the variable CPPFLAGS or CXXFLAGS .

  • It works, thanks! - AccumPlus
  • one
    @AccumPlus: Then do not forget to mark the answer with a green bird (left). - VladD
  • @AccumPlus: Well, tick ... :) - val

Gnu make has an interesting vpath directive that allows you to specify where to look for certain sources. In your case, just add the Makefile to the beginning:

 vpath %.cpp src vpath %.h src CXXFLAGS+=-Isrc 

And implicit rules then you can not rewrite.