Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Makefiles?
Index -> Programming, C++ -> C++ Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
ssy




PostPosted: Sun Jun 27, 2010 3:11 pm   Post subject: Makefiles?

I am supposed to create a makefile for a simple C++ program (something as simple as Hello World) that allows the user to type make for a production build of the program, and make debug for a build of the program that includes debugging information.

How am I supposed to do that? I thought makefiles are used for large scale projects that consist of a lot of different source files, how does it work if there is only one source file that can be compiled directly?
Sponsor
Sponsor
Sponsor
sponsor
DtY




PostPosted: Sun Jun 27, 2010 3:25 pm   Post subject: RE:Makefiles?

Only one source file?

Let's pretend the program is called HelloWorld, and is made up of only the file HelloWorld.cpp, of course, replace the file names with their proper names

code:
HelloWorld: HelloWorld.o
[tab]g++ -o HelloWorld HelloWorld.o

HelloWorld.o: HelloWorld.cpp
[tab]g++ -c HelloWorld.cpp


NB. You MUST use a tab, if you use spaces, it will not work, and you will get a meaningless error.

When you run make, it will start with the first target (you could also give a target on the command line), and then:

- Will check if HelloWorld is up to date; it's up to date if it is at least as new as all it's dependencies, in this case, it doesn't exist, so it's out of date. Then checks if all the dependencies are up to date
- Checks HelloWorld.o, it sees that this target relies on HelloWorld.cpp, and doesn't exist, so it's out of date.
- Checks the dependencies of HelloWorld.cpp, it has none, so it is considered always up to date (which makes sense, since it's entered by a human)
- Runs the program under HelloWorld.o to make HelloWorld.o up to date (g++ -c...)
- HelloWorld.o is now up to date, so make is ready to build HelloWorld by executing what is under it's program (g++ -o...).
- Target is up to date, make is finished.
Display posts from previous:   
   Index -> Programming, C++ -> C++ Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 2 Posts ]
Jump to:   


Style:  
Search: