0

I want to generate my obj files in a subfolder, I have tried this:

lib/*.o: source/*.cpp                                                                                         
         clang++ $(CC_FLAGS) -c -Iinclude source/*.cpp  

But it still generates the obj files in the project root and not in the lib/

The project tree that I'm trying to have:

Project/
        source/(cpp files)
        include/(header files)
        lib/(obj files)
aqww
  • 75
  • 6
  • 2
    you can use the -o option to specify where you want them to end up, http://stackoverflow.com/questions/14639794/getting-make-to-create-object-files-in-a-specific-directory – hurnhu Dec 22 '15 at 15:48
  • I already tried but can't succeed to get it work.. It tells me that -o doesn't work for multiple files clang: "error: cannot specify -o when generating multiple output files" when I add "-o $@" at the end. I also tried "-c $< -o $@" and all it does it creates an unique obj named "*.o" in my lib/ directory – aqww Dec 22 '15 at 16:00

1 Answers1

0

You don't show your current makefile, but my suspicion is that it's wrong. However as we can't see it, we'll leave that alone.

The compiler does not support writing multiple object files to a different directory. If you pass multiple source files along with the -c flag then it will write out multiple object files but only to the current directory... as you've discovered the -o flag can't be specified on compile lines which generate multiple output files.

You can change your recipe to look like this:

cd lib && clang++ $(CC_FLAGS) -c -I../include ../source/*.cpp

this will cause all the object files to be written to the lib directory because it's now the current directory.

However, putting this into a makefile is not simple, because make itself is designed to have a single recipe create a single target. However you have this problem with your existing makefile which you don't show, as well.

MadScientist
  • 92,819
  • 9
  • 109
  • 136