Malaken
Malaken

Reputation: 103

beginner GNU Makefile Error

This is my first, attempt at a, Makefile after necessity from a previous post.

Anyway here's the code

SortLab.exe : SelectionSort.o Main.o  
        g++ -o $@ $^  
SelectionSort.o : SelectionSort.cpp SelectionSort.h  
Main.o : Main.cpp  

#-------------------------------------------------------------

run: SortLab.exe  
        ./SortLab.exe  

clean:  
    rm -f *.o  
    rm -f *.exe  

build: clean SortLab.exe  

%.o: %.cpp  
    g++ -c $<  

I intend to have SelectionSort.cpp & SelectionSort.h form an object file, and Main.cpp to form its own object file. Then finally create an executable. Main.cpp depends on SelectionSort.cpp, where do I go wrong?

Also where can I find what the different GNU commands mean, -o -c and such

Upvotes: 0

Views: 381

Answers (1)

Fred Foo
Fred Foo

Reputation: 363467

  1. You shouldn't need to define the %.o: %.cpp rule yourself, Make knows how to compile C++.
  2. Indent with tabs, not spaces; Make is sensitive to the difference.
  3. Every object file should depend on the headers included in the source files it depends on. You probably need Main.o : Main.cpp SelectionSort.h.
  4. build shouldn't depend on clean, it defeats one of Make's main features (selectively recompilation when files have changed).
  5. If you make build the first target, you can run Make without a target to get a full compile. It's customary to call the main target all.

Upvotes: 2

Related Questions