Jump to content
  • Advertisement
Sign in to follow this  
Toadhead

Make and Makefiles

This topic is 2498 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

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.

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 wink.png

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.microsoft.com/Forums/en/vsx/thread/f7cc8fdb-8e73-44e4-b78d-2bf8ae5aa83f

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!