Jump to content



Make and Makefiles

  • You cannot reply to this topic
12 replies to this topic

#1 vleugel   Members   -  Reputation: 112

Like
0Likes
Like

Posted 06 February 2012 - 02:53 PM

Hello,

I am confused about make and makefiles. I learned programming with visual studio, which uses convenient project files. VS lists all source files and when you press 'Build solution' it will be compiled and build and everything goes smoothly.

But sometimes I want to use code written by others like SDK's. Every now and than I come across some code that does not offer binaries, you have to build it yourself. They come with makefiles and as soon as I see the file "Makefile" I delete everything and search for an alternative. But today I want to find out how it works and understand why so many people use this horrible build system :)
So my knowledge so far:

Makefiles contain a set of instructions to build a particular program. Make is the program that reads the makefiles and builds the program based on the source files indicated by the makefile.


Questions i have:
- Make itself is not a compiler is it not? So what compiler is being used? How can you configure it?
- Lets say I download some source code that needs to be build first, it contains a Makefile. How do I proceed in Windows?
What programs do I need to download in order to build this code on Windows? I already downloaded GNUWin32, added the make to my system path but when I open cmd.exe and run the make command in the right directory I get an error (process_begin: CreateProcess(NULL, echo <etc etc> Please wait..., ...) failed. make (e=2): The system cannot find the file specified.).


Googleing doesn't really help me, neither did a search on gamedev. I heard something about cygwin, downloaded it but than I have to run some kind of Linux command screen which I don't understand too well. I also downloaded the gcc compiler, copied the files to the home directory and ran make inside this console, but than I got some other error again and I don't realy feel like using all this stuff, do people realy think this mess is any better than the goodworking convenient project files from visual studio?

I just can seem to figure out why people want to go through all this trouble to use makefiles instead of project files?
Are there any easy ways to build Makefile-projects in Windows? I hope anyone can push my in the right direction, all tutorials I can find on make and makefiles assume you use linux I guess.


Greetings,
Rob

Ad:

#2 SiCrane   Moderators   -  Reputation: 2378

Like
0Likes
Like

Posted 06 February 2012 - 03:30 PM

At the most basic Makefiles consist of two kinds of elements: definitions and rules. A definition assigns a string to a variable. Ex:
CC = gcc
This assings the string "gcc" to the variable CC. Next there are rules. Rules consist of a target and dependencies followed by a list of commands. A target is separated from its dependencies by a colon. When a target is processed, if the target is missing or any of the dependencies are newer than the target, the list of commands are executed. For example:
a.obj: a.cpp a.h
     $(CC) a.cpp
This specifies that a.obj depends on a.cpp and a.h. If either a.obj is missing or a.cpp or a.h are newer than a.obj, then $(CC) a.cpp is run. Here $(CC) is a variable reference to a previous definition. If CC is set like it was in the first example, this would expand to "gcc a.cpp".

Again, this is the most basic of explanations, but it should help you understand the more in-depth explanations elsewhere.

#3 SimonForsman   Members   -  Reputation: 1199

Like
0Likes
Like

Posted 06 February 2012 - 04:34 PM

View Postvleugel, on 06 February 2012 - 02:53 PM, said:

Hello, I am confused about make and makefiles. I learned programming with visual studio, which uses convenient project files. VS lists all source files and when you press 'Build solution' it will be compiled and build and everything goes smoothly. But sometimes I want to use code written by others like SDK's. Every now and than I come across some code that does not offer binaries, you have to build it yourself. They come with makefiles and as soon as I see the file "Makefile" I delete everything and search for an alternative. But today I want to find out how it works and understand why so many people use this horrible build system :) So my knowledge so far: Makefiles contain a set of instructions to build a particular program. Make is the program that reads the makefiles and builds the program based on the source files indicated by the makefile. Questions i have: - Make itself is not a compiler is it not? So what compiler is being used? How can you configure it? - Lets say I download some source code that needs to be build first, it contains a Makefile. How do I proceed in Windows? What programs do I need to download in order to build this code on Windows? I already downloaded GNUWin32, added the make to my system path but when I open cmd.exe and run the make command in the right directory I get an error (process_begin: CreateProcess(NULL, echo Please wait..., ...) failed. make (e=2): The system cannot find the file specified.). Googleing doesn't really help me, neither did a search on gamedev. I heard something about cygwin, downloaded it but than I have to run some kind of Linux command screen which I don't understand too well. I also downloaded the gcc compiler, copied the files to the home directory and ran make inside this console, but than I got some other error again and I don't realy feel like using all this stuff, do people realy think this mess is any better than the goodworking convenient project files from visual studio? I just can seem to figure out why people want to go through all this trouble to use makefiles instead of project files? Are there any easy ways to build Makefile-projects in Windows? I hope anyone can push my in the right direction, all tutorials I can find on make and makefiles assume you use linux I guess. Greetings, Rob

The advantage of makefiles is that they are extremely lightweight and portable which is a good thing when you are distributing sourcecode that is intended to run on a wide variety of platforms, pretty much all *nix systems (not just Linux) have a compiler and make installed and a enviroment variable named CC that specifies which compiler the system should use by default, quite many IDEs can create makefiles for you automatically (so there is little need to write them by hand, unless you want some custom build steps).

In your case the problem has to do with the makefile trying to launch an application that it cannot find, posting the actual makefile and exact error message(s) would be quite helpful.
I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!

#4 Washu   Senior Moderators   -  Reputation: 2448

Like
0Likes
Like

Posted 06 February 2012 - 05:06 PM

As sicrane already mentioned a makefile is basically a collection of definitions and rules. Each rule has a set of dependencies, and commands to execute. The execution commands, rules names, and even the dependencies can all use the definitions. Really, a makefile just takes care of a lot of the work you would have to manually do to build a program from the command line.

If you're working in a linux-like environment and want to get familiar with the makefile syntax then really here is where you should start. But be warned: while a simple makefile can be easy to understand, even a slightly more useful makefile can be very difficult to understand. Attached you can find another fairly simple makefile that uses some more advanced concepts to avoid having to manually dictate the incoming source files incoming.

# external dependencies
# These can be set by specifying them on the command line
# i.e. make OBJDIR=build
# A list of paths to search for header files in
INCLUDES   = /Users/Shared/Projects/boost_1_48_0 . ./Shared	
# The output folder for intermediate build files (object files)
OBJDIR     = build

# Compiler directives and flags
# Ensure our include paths are all prefixed with -I
CC_INCLUDES= $(addprefix -I,$(INCLUDES))
# specify our desired compiler
CC         = clang++
# specify our compiler flags, including all of the include folders
CFLAGS     = -Wall -Werror -std=c++0x -c $(CC_INCLUDES)
# specify our linker
LD         = clang++
# specify our linker flags
LDFLAGS    =

# recursively find all C++ files from the Makefile directory
SOURCES    = $(wildcard *.cpp) $(wildcard */*.cpp)
# for each C++ file found change cpp to o and prefix the OBJDIR
OBJECTS   := $(addprefix $(OBJDIR)/,$(patsubst %.cpp,%.o,$(SOURCES)))
# the final binary output. can be overriden at the command line
BIN        = test

all: $(OBJECTS)
	$(LD) $(LDFLAGS) $(OBJECTS) -o $(BIN)

$(OBJDIR)/%.o : %.cpp | $(OBJDIR)
	@mkdir -p $(dir $@)
	$(CC) $(CFLAGS) -c $< -o $@

$(OBJDIR):
	@mkdir -p $(OBJDIR)

clean:
	@rm -rf build

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.
ScapeCode - Blog | SlimDX

#5 daviangel   Members   -  Reputation: 590

Like
0Likes
Like

Posted 06 February 2012 - 06:41 PM

View Postvleugel, on 06 February 2012 - 02:53 PM, said:

Questions i have:
- Make itself is not a compiler is it not? So what compiler is being used? How can you configure it?
- Lets say I download some source code that needs to be build first, it contains a Makefile. How do I proceed in Windows?
What you want to google is msbuild vs nmake.
MSBuild is the Microsoft replacement tool for nmake which it used for makefiles.
The problem with Microsoft's version of make i.e. nmake is that it was never as powerful as the Unix version.
And that leads to why make is so hard to use is that it's so general.
You can pretty much run any available unix command inside your makefile and do all kinds of crazy stuff.
Also, that's probably why you are getting that error above since the makefile you found is probably trying to run some unix program which of course it's not going to find on your Windows system!
Unless you want to spend a ton of time learning about makefiles your best bet is to find a Windows specific version of what you are looking for.
For example, here is a makefile for the code for Stroustrup: "Programming: Principles and Practice using C++" and it's Mac specific at that.
The only way half of that stuff will even make any sense is if you have some unix shell programming experience.

View Postvleugel, on 06 February 2012 - 02:53 PM, said:

I just can seem to figure out why people want to go through all this trouble to use makefiles instead of project files?
Because it's the unix way Posted Image
Don't talk about writing games, don't write design docs, don't spend your time on web boards. Sit in your house write 20 games when you complete them you will either want to do it the rest of your life or not * Andre Lamothe

#6 enunes   Members   -  Reputation: 104

Like
0Likes
Like

Posted 06 February 2012 - 08:16 PM

Also, consider Makefiles are generic in sense they aren't limited to calling your compiler.
You can do anything you want in them, as long as you have some, uh, "command line" skills.
Consider an environment where everytime you want to "build", you not only want to compile your code into an executable, but also to run stuff like: run scripts that auto generate documentation, run programs that check your code, generate some of the source code automatically prior to compiling, and after all, create a final product (think of creating a compacted file) containing arbitrary data (such as models and textures as this is gamedev) plus your freshly compiled executables.
With a properly written Makefile you can usually do that by just running "make".
(I don't have any Windows development experience so I don't really have a clue on how you would do that without make or some case specific and probably clumbered tool).

#7 cheawick   Members   -  Reputation: 122

Like
0Likes
Like

Posted 07 February 2012 - 12:22 AM

I've run into a makefile situation not too long ago and did not figure out how to resolve it either. I'd like to ask what exactly is a makefile? Is it partial source code, full source code, or even source code?

I haven't touched command line *nix since Ubuntu (Fiesta I think) came out and was more heavily dependent on Win OS, though I've moved onto Mac now but still a total newb there. =+P

#8 mhagain   Members   -  Reputation: 698

Like
0Likes
Like

Posted 07 February 2012 - 04:55 AM

I don't think you can call a makefile "portable" if it won't work right away on all target platforms (maybe when a lot of people say "portable" they really mean "Unix", but oh well)...

You can import a makefile into Visual Studio and it will create a solution/project for you. See: http://social.msdn.m...8d-2bf8ae5aa83f

#9 vleugel   Members   -  Reputation: 112

Like
0Likes
Like

Posted 07 February 2012 - 11:09 AM

Thanks everyone. I guess the problem I had with makefiles is that I was under the false impression that makefiles were portable.
It just turns out makefile projects are a pain in the ass unless you use *nix too.
I still dont get it why this is, it shouldnt be so hard to come up with a system that is platform indepentant is it?
For now Ill tro to learn how to read and convert them. Thanks for the help everyone!

#10 swiftcoder   Senior Moderators   -  Reputation: 1617

Like
1Likes
Like

Posted 07 February 2012 - 12:44 PM

View Postvleugel, on 07 February 2012 - 11:09 AM, said:

Thanks everyone. I guess the problem I had with makefiles is that I was under the false impression that makefiles were portable.
Portable makefiles are portable. Just as C++ is portable, but most C++ programs are not actually portable.

I routinely write makefiles that work with Mingw on Windows, and standard gcc on Linux/Mac. Although a real portable build system like CMake is much to be preferred where available.
Tristam MacDonald - swiftcoding [new blog post: bidding a freelance contract]

#11 AllEightUp   Members   -  Reputation: 170

Like
1Likes
Like

Posted 07 February 2012 - 06:31 PM

View Postswiftcoder, on 07 February 2012 - 12:44 PM, said:

View Postvleugel, on 07 February 2012 - 11:09 AM, said:

Thanks everyone. I guess the problem I had with makefiles is that I was under the false impression that makefiles were portable.
Portable makefiles are portable. Just as C++ is portable, but most C++ programs are not actually portable.

I routinely write makefiles that work with Mingw on Windows, and standard gcc on Linux/Mac. Although a real portable build system like CMake is much to be preferred where available.
I highly recommend this solution as an alternative though CMake is pretty arcane magic syntactically sometimes. (I swear, sometimes makefiles *would* be easier. :)) Until you need the more advanced features another alternative is Premake4 which does most of the same things but the syntax is much preferable due to being lua based and very straight forward. Unfortunately Premake lacks a number of CMake's nice features, which are completely inaccessible till you get past the horrible syntax/behavior learning curve anyway, so until you need those things premake may do everything you want. And the conversion process is not horrible in general. (Been there, done that, got the T-Shirt.)

#12 SiCrane   Moderators   -  Reputation: 2378

Like
0Likes
Like

Posted 07 February 2012 - 06:40 PM

There's also SCons, which is another more or less cross platform build solution that doesn't make me want to kill people.

#13 AllEightUp   Members   -  Reputation: 170

Like
0Likes
Like

Posted 07 February 2012 - 09:44 PM

View PostSiCrane, on 07 February 2012 - 06:40 PM, said:

There's also SCons, which is another more or less cross platform build solution that doesn't make me want to kill people.

I like Scons but I really prefer working on the per platform dev environments. I worked with it for a long time but eventually the lack of all the nice tools turned me against it. I.e. no visual assist on MS, no effective "project" source for SlickEdit on Linux/Mac etc. The project generators have grown up considerably and having all those nice per platform items is hard to ignore when they speed up coding time and debugging (mostly debugging) considerably. (I just can't live without SlickEdit and it's integration of GDB on Linux/OsX/etc platforms, I'm too lazy to learn alternatives as I'm so embedded in VC's debugger behavior anymore which SE emulates.)






We are working on generating results for this topic
PARTNERS