1 Replies - 95 Views - Last Post: 07 February 2012 - 05:34 AM Rate Topic: -----

Topic Sponsor:

#1 kevinV  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 05-February 12

how do I create a makefile

Posted 07 February 2012 - 12:41 AM

I have 4 files: definitions.h devices.h devices.cpp and main.cpp
can anyone help me write a makefile for these files? thanks
Is This A Good Question/Topic? 0
  • +

Replies To: how do I create a makefile

#2 Karel-Lodewijk  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 438
  • View blog
  • Posts: 849
  • Joined: 17-March 11

Re: how do I create a makefile

Posted 07 February 2012 - 05:34 AM

A quick manual makefile to compile this looks something like this:

program: main.o devices.o
	g++ -oprogram main.o devices.o
main.o: main.cpp definitions.h
	g++ -c -omain.o main.cpp
devices.o: devices.cpp devices.h definitions.h
	g++ -c -odevices.o devices.cpp
clean:
	rm -rf program main.o devices.o



The basic principle is, you declare for every rule what it depends on. This can either be a file or another rule in the makefile to create what it depends on. Now whenever a file changes everything that depends on that file directly or indirectly will be recompiled but nothing more. i.e.: When devices.cpp changes it will be recompiled, but main.cpp won't be.

It does get hard to track these changes manually, so people use a lot of tricks/macros to make this easier. When that became hard to track, people started using scripts to write it for them, these are today further developed and known as autotools. When people realized that these scripts were becoming a mess and they could do better, they started writing alternatives like cmake/qmake/scons/... . I don't think any of those has much improved things, just laying down your options if you ever need something for a bigger project.

This post has been edited by Karel-Lodewijk: 07 February 2012 - 05:35 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1