-5

How do compile 2 .cpp files and 1 .h file in a makefile? I am noob with makefile but I don't want to do it in the command line all the time (Because I need to add libraries and linker options etc. but I know how to do that).

I've done a ton of research, including the manual, but I still don't get it.

Here's the makefile I wrote:

HEADERS = program.h headers.h

default: program

program.o: program.cpp $(HEADERS)
    g++ -c program.cpp -o program.o

program: program.o
    g++ program.o -o program

clean:
    -rm -f program.o
    -rm -f program

The makefile I have is for 1 file. How do I do it for 3 files (2 .cpp, 1 .h)?

ANY help is much appreciated.

CATspellsDOG
  • 109
  • 1
  • 2
  • 10
  • 4
    Start with the [manual](https://www.gnu.org/software/make/manual/). –  Feb 15 '15 at 14:04
  • @remyabel don't worry :) I'm not that dumb – CATspellsDOG Feb 15 '15 at 14:08
  • The manual is actually a pretty good resource. Tutorials can be kind of all over the place. –  Feb 15 '15 at 14:08
  • 3
    Post the makefile you've tried to write. Describe what doesn't work or what parts you can't figure out. We'll answer specific questions about specific problems, but we won't just write your code for you. That's not what SO is about. – MadScientist Feb 15 '15 at 14:09
  • 1
    Well, cat, all we know about you is this very very basic question. There are only two possibilities, and remyabel was so kind to choose the more flattering one (but everything is relative): That you actually didn't bother to *read* the manual. Because if you had: Why would you ask this really really basic question? The only conceivable answer to that would be the even less flattering assumption. – Peter - Reinstate Monica Feb 15 '15 at 14:14
  • If you only have 2 files, and not planning to add more, you can use a script: `g++ file1.cpp file2.cpp` (assuming your compiler is `g++`) – anatolyg Feb 15 '15 at 14:18
  • @CATspellsDOG Pro tip: `make` provides you with implicit rules to compile `.c` to `.o`. – πάντα ῥεῖ Feb 15 '15 at 14:25
  • @anatolyg how would I add a `.h` file? – CATspellsDOG Feb 15 '15 at 14:25
  • @CATspellsDOG You don't need to "compile" h-files. Maybe this was the bit that made it all so difficult for you. You only need to compile the `cpp` files. Header files are included in the `makefile` just for `make` to figure out when to recompile the source files. – anatolyg Feb 15 '15 at 14:28
  • BTW example seems to be taken from [here](http://stackoverflow.com/a/1484873/509868) – anatolyg Feb 15 '15 at 14:29
  • @anatolyg nope. I watched a tutorial on it and that was the code. – CATspellsDOG Feb 15 '15 at 14:30
  • Possible duplicate of [How do I make a simple makefile? GCC Unix](https://stackoverflow.com/questions/1484817/how-do-i-make-a-simple-makefile-gcc-unix/1484873) –  Feb 15 '15 at 14:31
  • Fascinating that your makefile is *exactly* the one from the question @remyabel posted - **including that it compiles C files, not CPP files**. – danielschemmel Feb 15 '15 at 16:13

1 Answers1

2

Thanks for anatolyg for explaining:

You don't need to "compile" h-files Maybe this was the bit that made it all so difficult for you. You only need to compile the cpp files. Header files are included in the makefile just for make to figure out when to recompile the source files.

Thanks to that, I figured out I just need to make a makefile to compile 2 .cpp files and it handles the rest.

Greatly Appreciated anatolyg. Thank you :)

CATspellsDOG
  • 109
  • 1
  • 2
  • 10