0

I have following makefile:

CC = g++
CFLAGS=-c -Wall


all: myProgram

myProgram: obj/main.o obj/Class1.o obj/Class2.o 
    $(CC) -o bin/myProgram obj/main.o obj/Class1.o obj/Class2.o 

obj/main.o: main.cpp  
    $(CC) $(CFLAGS) main.cpp -o obj/main.o

obj/Class1.o: src/Class1.cpp
    $(CC) $(CFLAGS) src/Class1.cpp -o obj/Class1.o

obj/Class2.o: src/Class2.cpp
    $(CC) $(CFLAGS) src/Class2.cpp -o obj/Class2.o

The idea is I have such structure of files/directories:

/project
    Makefile
    main.cpp
    /src
        Class1.cpp
        Class2.cpp
    /obj
        Class1.obj
        Class2.obj
    /bin
        myProgram
    /inc
        Class1.h
        Class2.h

The makefiles takes sources and object files in these directories and outputs them in /bin as myProgram.

I know my makefile might not look very ideal but anyway. I want to learn how to create makefiles, so what do you recommend I add to my makefile, or learn additionally about makefiles as a next step to improve my knowledge? (I might be working on a project where I will need to know makefiles, so I am trying to learn them)

pseudonym_127
  • 357
  • 7
  • 16
  • Your question is likely to get closed, as it just causes debate. Scaling a Makefile to a complex project in multiple directories is not the easiest thing in the world, and I have seen many ways in which people do it. If you can, take your team and your self to SCons. If you need to stick to them, check gmake extensions and figure out ways to accomplish what you want more easily. – dsign Jun 26 '13 at 06:40
  • Also, your Makefile doesn't take into account dependencies to header files. Makefiles don't do that automatically, but there is some "simple" compiler-dependent way of doing it... – dsign Jun 26 '13 at 06:44
  • 2
    `CC = g++` - **Nope.** `CC = gcc`. If you want C++, that's `CXX`. –  Jun 26 '13 at 06:46
  • http://stackoverflow.com/questions/2908057/makefiles-compile-all-cpp-files-in-src-to-os-in-obj-then-link-to-binary/2908351 – bobah Jun 26 '13 at 07:01
  • `CXX` and `CXXFLAGS` for C++. – juanchopanza Jun 26 '13 at 07:03
  • @H2CO3: I got errors when I used gcc instead of g++ – pseudonym_127 Jun 26 '13 at 07:17
  • @pseudonym_127 I didn't tell you to use `gcc`. Use `g++`, but call it `CXX` in the Makefile. Consistency powwa! –  Jun 26 '13 at 07:18
  • I am not sure I get what you mean – pseudonym_127 Jun 26 '13 at 07:22
  • @pseudonym_127 `CXX = g++` –  Jun 26 '13 at 07:27
  • @pseudonym_127 Did you find any of the answers helpful? – Robert S. Barnes Jul 01 '13 at 18:12
  • @Robert S. Barnes: hm.. yes, yours actually which contained link to Michael Safyan's makefile. But now everyone id discouraging me to use makefile -- and I'm a bit "sad" since I had already invested some time learning it ... – pseudonym_127 Jul 02 '13 at 06:20
  • @pseudonym_127 Personally, I think it's a worthwhile skill to have. It gives you a certain freedom and flexibility that may not always be available with boxed solutions. Also, you'll notice that allot of large, important open source project use makefiles, such as the Linux kernel. – Robert S. Barnes Jul 02 '13 at 09:42

2 Answers2

1

Here is a generic Makefile I created for C, it can be easily adapted to C++:

Generic Makefile for Linux

and here is a great tutorial on how to write a makefile.

Robert S. Barnes
  • 39,711
  • 30
  • 131
  • 179
  • Robert if I try to learn more or less well that tutorial by Michael Safyan, will it allow me to say I can create makefiles for projects of say 'medium' size? – pseudonym_127 Jul 08 '13 at 09:34
  • Depends on what you consider medium size. The Linux Kernel uses hand written makefiles. – Robert S. Barnes Jul 08 '13 at 13:18
  • Robert, I am curious what I can say I know about makefiles after I have read/understood this tutorial? like what are the learning outcomes of this tutorial? thanks. – pseudonym_127 Jul 08 '13 at 14:08
  • If you read the tutorial, and the document "Recursive Make Considered Harmful" which is linked from within the tutorial you will have a strong understanding of the *principles* of how makefiles work, and an ability to write useful makefiles by hand. – Robert S. Barnes Jul 09 '13 at 06:39
  • @pseudonym_127 You know that if it was helpful you can mark up and answer and accept it. :-) – Robert S. Barnes Jul 20 '13 at 19:48
0

This is quite a generic question.. but let me see if I can answer it in a small way

  • target called clean - necessary
  • recursive makefiles might be useful to learn
  • you should use variables that contain list of your cpp files and obj files, and try to avoid typing them out.

Try out genmake.pl, it gives a decent default structure.

Karthik T
  • 31,456
  • 5
  • 68
  • 87
  • thanks for feedback yes I was interested in that kind of stuff and also maybe what to learn in future to improve my knowledge in makefiles as I might need to use them (maybe not for very small projects) – pseudonym_127 Jun 26 '13 at 06:45