Editing Documentation/Maemo 5 Developer Guide/GNU Build System

Warning: You are not logged in. Your IP address will be recorded in this page's edit history.

Warning: This page is 77 kilobytes long; some browsers may have problems editing pages approaching or longer than 32kb. Please consider breaking the page into smaller sections.

The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then save the changes below to finish undoing the edit.
Latest revision Your text
Line 16: Line 16:
Make can be used for other purposes as well. A target can easily be created for installing the built software on a destination computer, a target for generating documentation by using some automatic documentation generation tool, and so on. Some people use make to keep multiple Linux systems up to date with the newest software and various system configuration changes. In short, make is flexible enough to be generally useful.
Make can be used for other purposes as well. A target can easily be created for installing the built software on a destination computer, a target for generating documentation by using some automatic documentation generation tool, and so on. Some people use make to keep multiple Linux systems up to date with the newest software and various system configuration changes. In short, make is flexible enough to be generally useful.
-
[[Image:make.png|center|frame|alt=Diagram of project dependencies|The dependencies between different files making up a software project]]
+
[[Image:make.png|center|frame|The dependencies between different files making up a software project]]
The aim of make is to satisfy the target. Each target has its own dependencies. A user generally selects a target for make to satisfy by supplying the target name on the command line. Make starts by checking whether all of the dependencies exist and have an older timestamp than the target. If so, make  does nothing, because nothing has changed. However, because a header file (that an application is not dependent on directly) can change, make propagates the changes to the 'root' of the target as shown in the picture above.
The aim of make is to satisfy the target. Each target has its own dependencies. A user generally selects a target for make to satisfy by supplying the target name on the command line. Make starts by checking whether all of the dependencies exist and have an older timestamp than the target. If so, make  does nothing, because nothing has changed. However, because a header file (that an application is not dependent on directly) can change, make propagates the changes to the 'root' of the target as shown in the picture above.
Line 24: Line 24:
To illustrate this, suppose that '''file3.c''' in the above picture is modified. After that, make is run, and it will automatically rebuild the necessary targets ('''file3.o''', '''libutil.a''' and '''app'''):
To illustrate this, suppose that '''file3.c''' in the above picture is modified. After that, make is run, and it will automatically rebuild the necessary targets ('''file3.o''', '''libutil.a''' and '''app'''):
-
[[Image:make-mod1.png|center|frame|alt=Diagram of project files that need to be rebuilt|Project files that need to be rebuilt after file3.c is modified]]
+
[[Image:make-mod1.png|center|frame|Project files that need to be rebuilt after file3.c is modified]]
Now suppose that another function is added to '''file1.c'''. Then also '''util.h''' needs to be modified accordingly. The picture shows that quite a few objects and targets depend on this header file, so a sizable number of objects need to be rebuilt (but not all):
Now suppose that another function is added to '''file1.c'''. Then also '''util.h''' needs to be modified accordingly. The picture shows that quite a few objects and targets depend on this header file, so a sizable number of objects need to be rebuilt (but not all):
-
[[Image:make-mod2.png|center|frame|alt=Diagram of project files that need to be rebuilt|Project files that need to be rebuilt after file1.c and util.h are modified]]
+
[[Image:make-mod2.png|center|frame|Project files that need to be rebuilt after file1.c and util.h are modified]]
N.B. In the example pictures, there is a project with a custom static library, which is linked against the test application.
N.B. In the example pictures, there is a project with a custom static library, which is linked against the test application.
Line 45: Line 45:
* What are the commands to regenerate the resulting files when the need arises?
* What are the commands to regenerate the resulting files when the need arises?
-
This example deals with that as simply as possible. There is a project that consists of two source files and one header file. The contents of these files are listed below: [https://vcs.maemo.org/svn/maemoexamples/tags/maemo_5.0/simple-make-files/hello.c simple-make-files/hello.c]
+
This example deals with that as simply as possible. There is a project that consists of two source files and one header file. The contents of these files are listed below: simple-make-files/hello.c
<source lang="c">
<source lang="c">
Line 64: Line 64:
</source>
</source>
-
[https://vcs.maemo.org/svn/maemoexamples/tags/maemo_5.0/simple-make-files/hello_func.c simple-make-files/hello_func.c]
+
simple-make-files/hello_func.c
<source lang="c">
<source lang="c">
Line 84: Line 84:
</source>
</source>
-
[https://vcs.maemo.org/svn/maemoexamples/tags/maemo_5.0/simple-make-files/hello_api.h simple-make-files/hello_api.h]
+
simple-make-files/hello_api.h
<source lang="c">
<source lang="c">
Line 123: Line 123:
In the second case, gcc is instructed to create a binary object file for each of the source files. After that, gcc is instructed to link these output files ('''hello.o''' and '''hello_func.o''') together, and store the linked code into '''hello'''.
In the second case, gcc is instructed to create a binary object file for each of the source files. After that, gcc is instructed to link these output files ('''hello.o''' and '''hello_func.o''') together, and store the linked code into '''hello'''.
-
{{ambox|text=When gcc reads through the C source files, it also reads in the header files, because the C code uses the <code>#include</code> preprocessor directive. This is because gcc internally runs all files ending with .c through cpp (the preprocessor) first.}}
+
N.B. When gcc reads through the C source files, it also reads in the header files, because the C code uses the #include -preprocessor directive. This is because gcc internally runs all files ending with .c through cpp (the preprocessor) first.
-
The file describing this situation to make is as follows: [https://vcs.maemo.org/svn/maemoexamples/tags/maemo_5.0/simple-make-files/Makefile simple-make-files/Makefile]
+
The file describing this situation to make is as follows: simple-make-files/Makefile
<source lang="make">
<source lang="make">
Line 239: Line 239:
The next step is to create a copy of the original makefile, and add a goal that removes the binary object files and the application.  
The next step is to create a copy of the original makefile, and add a goal that removes the binary object files and the application.  
-
N.B. Other parts of the makefile do not change. [https://vcs.maemo.org/svn/maemoexamples/tags/maemo_5.0/simple-make-files/Makefile.2 simple-make-files/Makefile.2]
+
N.B. Other parts of the makefile do not change. simple-make-files/Makefile.2
<source lang="make">
<source lang="make">
Line 316: Line 316:
Suppose that for some reason a file called '''clean''' appears in the directory in which make is run with clean as the target. In this case, make probably decides that because clean already exists, there is no need to run the commands leading to the target, and in this case, make does not run the rm command at all. Clearly this is something that needs to be avoided.
Suppose that for some reason a file called '''clean''' appears in the directory in which make is run with clean as the target. In this case, make probably decides that because clean already exists, there is no need to run the commands leading to the target, and in this case, make does not run the rm command at all. Clearly this is something that needs to be avoided.
-
For these cases, GNU make provides a special target called <code>.PHONY</code> (note the leading dot). The real phony targets (clean) will be listed as a dependency to <code>.PHONY</code>. This signals to make that it should never consider a file called clean to be the result of the phony target.
+
For these cases, GNU make provides a special target called '''.PHONY''' (note the leading dot). The real phony targets (clean) will be listed as a dependency to .PHONY. This signals to make that it should never consider a file called clean to be the result of the phony target.
In fact, this is what most open source projects that use make do.
In fact, this is what most open source projects that use make do.
-
This leads in the following makefile (comments omitted for brevity): [https://vcs.maemo.org/svn/maemoexamples/tags/maemo_5.0/simple-make-files/Makefile.3 simple-make-files/Makefile.3]
+
This leads in the following makefile (comments omitted for brevity): simple-make-files/Makefile.3
-
<source lang="make">
+
<tt>hello<span><font color="#990000"><nowiki>:</nowiki></font></span> hello<span><font color="#990000">.</font></span>o hello_func<span><font color="#990000">.</font></span>o
-
hello: hello.o hello_func.o
+
        gcc -Wall hello<span><font color="#990000">.</font></span>o hello_func<span><font color="#990000">.</font></span>o -o hello
-
        gcc -Wall hello.o hello_func.o -o hello
+
hello<span><font color="#990000">.</font></span>o<span><font color="#990000"><nowiki>:</nowiki></font></span> hello<span><font color="#990000">.</font></span>c hello_api<span><font color="#990000">.</font></span>h
-
hello.o: hello.c hello_api.h
+
        gcc -Wall -c hello<span><font color="#990000">.</font></span>c -o hello<span><font color="#990000">.</font></span>o
-
        gcc -Wall -c hello.c -o hello.o
+
hello_func<span><font color="#990000">.</font></span>o<span><font color="#990000"><nowiki>:</nowiki></font></span> hello_func<span><font color="#990000">.</font></span>c hello_api<span><font color="#990000">.</font></span>h
-
hello_func.o: hello_func.c hello_api.h
+
        gcc -Wall -c hello_func<span><font color="#990000">.</font></span>c -o hello_func<span><font color="#990000">.</font></span>o
-
        gcc -Wall -c hello_func.c -o hello_func.o
+
<span><font color="#990000">.</font></span>PHONY<span><font color="#990000"><nowiki>:</nowiki></font></span> clean
-
.PHONY: clean
+
clean<span><font color="#990000"><nowiki>:</nowiki></font></span>
-
clean:
+
        rm -f hello hello<span><font color="#990000">.</font></span>o hello_func<span><font color="#990000">.</font></span>o
-
        rm -f hello hello.o hello_func.o
+
</tt>
-
</source>
+
=== Specifying Default Goal ===
=== Specifying Default Goal ===
Line 338: Line 337:
Make uses the first target in a makefile as its default goal. What if there is a need to explicitly set the default goal instead? Because it is not possible to actually change the "first target is the default goal" setting in make, this needs to be taken into account. So, a new target has to be added, and made sure that it will be processed as the first target in a makefile.
Make uses the first target in a makefile as its default goal. What if there is a need to explicitly set the default goal instead? Because it is not possible to actually change the "first target is the default goal" setting in make, this needs to be taken into account. So, a new target has to be added, and made sure that it will be processed as the first target in a makefile.
-
To achieve this, a new phony target must be created, and the wanted targets should be listed as the phony target's prerequisites. Any name can be used for the target, but all is a very common name for this use. The only thing that needs to be rememberd is that this target needs to be the first one in the makefile. [https://vcs.maemo.org/svn/maemoexamples/tags/maemo_5.0/simple-make-files/Makefile.4 simple-make-files/Makefile.4]
+
To achieve this, a new phony target must be created, and the wanted targets should be listed as the phony target's prerequisites. Any name can be used for the target, but all is a very common name for this use. The only thing that needs to be rememberd is that this target needs to be the first one in the makefile. simple-make-files/Makefile.4
-
<source lang="make">
+
<tt><span><font color="#990000">.</font></span>PHONY<span><font color="#990000"><nowiki>:</nowiki></font></span> all
-
.PHONY: all
+
all<span><font color="#990000"><nowiki>:</nowiki></font></span> hello
-
all: hello
+
hello<span><font color="#990000"><nowiki>:</nowiki></font></span> hello<span><font color="#990000">.</font></span>o hello_func<span><font color="#990000">.</font></span>o
-
hello: hello.o hello_func.o
+
        gcc -Wall hello<span><font color="#990000">.</font></span>o hello_func<span><font color="#990000">.</font></span>o -o hello
-
        gcc -Wall hello.o hello_func.o -o hello
+
hello<span><font color="#990000">.</font></span>o<span><font color="#990000"><nowiki>:</nowiki></font></span> hello<span><font color="#990000">.</font></span>c hello_api<span><font color="#990000">.</font></span>h
-
hello.o: hello.c hello_api.h
+
        gcc -Wall -c hello<span><font color="#990000">.</font></span>c -o hello<span><font color="#990000">.</font></span>o
-
        gcc -Wall -c hello.c -o hello.o
+
hello_func<span><font color="#990000">.</font></span>o<span><font color="#990000"><nowiki>:</nowiki></font></span> hello_func<span><font color="#990000">.</font></span>c hello_api<span><font color="#990000">.</font></span>h
-
hello_func.o: hello_func.c hello_api.h
+
        gcc -Wall -c hello_func<span><font color="#990000">.</font></span>c -o hello_func<span><font color="#990000">.</font></span>o
-
        gcc -Wall -c hello_func.c -o hello_func.o
+
<span><font color="#990000">.</font></span>PHONY<span><font color="#990000"><nowiki>:</nowiki></font></span> clean
-
.PHONY: clean
+
clean<span><font color="#990000"><nowiki>:</nowiki></font></span>
-
clean:
+
        rm -f hello hello<span><font color="#990000">.</font></span>o hello_func<span><font color="#990000">.</font></span>o
-
        rm -f hello hello.o hello_func.o
+
</tt>
-
</source>
+
-
Something peculiar can be seen in the listing above. Because the first target is the default goal for make, would that not make <code>.PHONY</code> now to be the default target? Because <code>.PHONY</code> is a special target in GNU make, this is safe. Also, because of compatibility reasons, GNU make ignores any targets that start with a leading dot (.) when looking for the default goal.
+
Something peculiar can be seen in the listing above. Because the first target is the default goal for make, would that not make .PHONY now to be the default target? Because .PHONY is a special target in GNU make, this is safe. Also, because of compatibility reasons, GNU make ignores any targets that start with a leading dot (.) when looking for the default goal.
=== Other Common Phony Goals ===
=== Other Common Phony Goals ===
Line 362: Line 360:
Some of the more common ones include:
Some of the more common ones include:
-
* install: Prerequisites for install is the software application binary (or binaries). The commands (normally ''install'' is used on Linux) will specify which binary file to place under which directory on the system (<code>/usr/bin</code>, <code>/usr/local/bin</code>, etc).
+
* install: Prerequisites for install is the software application binary (or binaries). The commands (normally ''install'' is used on Linux) will specify which binary file to place under which directory on the system (/usr/bin, /usr/local/bin, etc).
* distclean: Similar to clean; removes object files and such, but removes other files as well (sounds scary). Normally this is used to implement the removal of Makefile in the case that it was created by some automatic tool (''configure'' for example).
* distclean: Similar to clean; removes object files and such, but removes other files as well (sounds scary). Normally this is used to implement the removal of Makefile in the case that it was created by some automatic tool (''configure'' for example).
* package: Prerequisites are as in install, but instead of installing, creates an archive file (''tar'') with the files in question. This archive can then be used to distribute the software.
* package: Prerequisites are as in install, but instead of installing, creates an archive file (''tar'') with the files in question. This archive can then be used to distribute the software.
Line 396: Line 394:
* Do not reference a variable that is being defined in its text portion. This results in an endless loop whenever this variable is used. If this happens GNU make stops to an error.
* Do not reference a variable that is being defined in its text portion. This results in an endless loop whenever this variable is used. If this happens GNU make stops to an error.
-
Now the previously used makefile is reused, but some variables are introduced. This also shows the syntax of defining the variables, and how to use them (reference them): [https://vcs.maemo.org/svn/maemoexamples/tags/maemo_5.0/simple-make-files/Makefile.5 simple-make-files/Makefile.5]
+
Now the previously used makefile is reused, but some variables are introduced. This also shows the syntax of defining the variables, and how to use them (reference them): simple-make-files/Makefile.5
<source lang="make">
<source lang="make">
Line 427: Line 425:
===== Simple Variables =====
===== Simple Variables =====
-
Suppose you have a makefile like this: [https://vcs.maemo.org/svn/maemoexamples/tags/maemo_5.0/simple-make-files/Makefile.6 simple-make-files/Makefile.6]
+
Suppose you have a makefile like this: simple-make-files/Makefile.6
-
<source lang="make">
+
<tt>CC <span><font color="#990000"><nowiki>=</nowiki></font></span> gcc -Wall
-
CC = gcc -Wall
+
<span>''<span><font color="#9A1900"><nowiki># we want to add something to the end of the variable</nowiki></font></span>''</span>
-
# we want to add something to the end of the variable
+
CC <span><font color="#990000"><nowiki>=</nowiki></font></span> <span><font color="#009900">$(CC)</font></span> -g
-
CC = $(CC) -g
+
hello<span><font color="#990000">.</font></span>o<span><font color="#990000"><nowiki>:</nowiki></font></span> hello<span><font color="#990000">.</font></span>c hello_api<span><font color="#990000">.</font></span>h
-
hello.o: hello.c hello_api.h
+
        <span><font color="#009900">$(CC)</font></span> -c hello<span><font color="#990000">.</font></span>c -o hello<span><font color="#990000">.</font></span>o
-
        $(CC) -c hello.c -o hello.o
+
</tt>
-
</source>
+
What might seem quite logical to a human reader, does not seem very logical to make.
What might seem quite logical to a human reader, does not seem very logical to make.
Line 441: Line 438:
Because the contents of recursive variables are evaluated when they are referenced, it can be seen that the above fragment will lead to an infinite loop.
Because the contents of recursive variables are evaluated when they are referenced, it can be seen that the above fragment will lead to an infinite loop.
-
How can this be corrected? Make actually provides two mechanisms for this. This is the solution with simple variables: [https://vcs.maemo.org/svn/maemoexamples/tags/maemo_5.0/simple-make-files/Makefile.7 simple-make-files/Makefile.7]
+
How can this be corrected? Make actually provides two mechanisms for this. This is the solution with simple variables: simple-make-files/Makefile.7
-
<source lang="make">
+
<tt>CC <span><font color="#990000"><nowiki>:=</nowiki></font></span> gcc -Wall
-
CC := gcc -Wall
+
<span>''<span><font color="#9A1900"><nowiki># we want to add something to the end of the variable</nowiki></font></span>''</span>
-
# we want to add something to the end of the variable
+
CC <span><font color="#990000"><nowiki>:=</nowiki></font></span> <span><font color="#009900">$(CC)</font></span> -g
-
CC := $(CC) -g
+
hello<span><font color="#990000">.</font></span>o<span><font color="#990000"><nowiki>:</nowiki></font></span> hello<span><font color="#990000">.</font></span>c hello_api<span><font color="#990000">.</font></span>h
-
hello.o: hello.c hello_api.h
+
        <span><font color="#009900">$(CC)</font></span> -c hello<span><font color="#990000">.</font></span>c -o hello<span><font color="#990000">.</font></span>o
-
        $(CC) -c hello.c -o hello.o
+
</tt>
-
</source>
+
Notice that the equals character (=) has been changed into := .
Notice that the equals character (=) has been changed into := .
-
This is how simple variables are created. Whenever a simple variable is referenced, make just replaces the text that the variable contains without evaluating it. It does the evaluation only when the variable is defined. In the above example, <code>CC</code> is created with the content of <code>gcc -Wall</code> (which is evaluated, but is just plain text), and when the <code>CC</code> variable is defined next time, make evaluates <code>$(CC) -g</code> which is replaced with <code>gcc -Wall -g</code>.
+
This is how simple variables are created. Whenever a simple variable is referenced, make just replaces the text that the variable contains without evaluating it. It does the evaluation only when the variable is defined. In the above example, CC is created with the content of gcc -Wall (which is evaluated, but is just plain text), and when the CC variable is defined next time, make evaluates $(CC) -g which is replaced with gcc -Wall -g.
So, the only two differences between the variable flavors are:
So, the only two differences between the variable flavors are:
Line 462: Line 458:
Most of the time it is advisable to use the recursive variable flavor, because it does what is wanted.
Most of the time it is advisable to use the recursive variable flavor, because it does what is wanted.
-
Two ways of appending text to an existing variable were mentioned. The other mechanism is the += operation as follows: [https://vcs.maemo.org/svn/maemoexamples/tags/maemo_5.0/simple-make-files/Makefile.8 simple-make-files/Makefile.8]
+
Two ways of appending text to an existing variable were mentioned. The other mechanism is the += operation as follows: simple-make-files/Makefile.8
-
<source lang="make">
+
<tt>CC <span><font color="#990000"><nowiki>=</nowiki></font></span> gcc -Wall
-
CC = gcc -Wall
+
<span>''<span><font color="#9A1900"><nowiki># we want to add something to the end of the variable</nowiki></font></span>''</span>
-
# we want to add something to the end of the variable
+
CC <span><font color="#990000">+=</font></span> -g
-
CC += -g
+
hello<span><font color="#990000">.</font></span>o<span><font color="#990000"><nowiki>:</nowiki></font></span> hello<span><font color="#990000">.</font></span>c hello_api<span><font color="#990000">.</font></span>h
-
hello.o: hello.c hello_api.h
+
        <span><font color="#009900">$(CC)</font></span> -c hello<span><font color="#990000">.</font></span>c -o hello<span><font color="#990000">.</font></span>o
-
        $(CC) -c hello.c -o hello.o
+
</tt>
-
</source>
+
The prepending operation can also be used with simple variables. Make will not change the type of variable on the left side of the += operator.
The prepending operation can also be used with simple variables. Make will not change the type of variable on the left side of the += operator.
Line 485: Line 480:
* <code>$?</code> : list of prerequisites that are newer than the target is (if target does not exist, they are all considered newer).
* <code>$?</code> : list of prerequisites that are newer than the target is (if target does not exist, they are all considered newer).
-
By rewriting the makefile using automatic variables, the result is: [https://vcs.maemo.org/svn/maemoexamples/tags/maemo_5.0/simple-make-files/Makefile.9 simple-make-files/Makefile.9]
+
By rewriting the makefile using automatic variables, the result is: simple-make-files/Makefile.9
-
<source lang="make">
+
<tt><span>''<span><font color="#9A1900"><nowiki># add the warning flag to CFLAGS-variable</nowiki></font></span>''</span>
-
# add the warning flag to CFLAGS-variable
+
CFLAGS <span><font color="#990000">+=</font></span> -Wall
-
CFLAGS += -Wall
+
targets <span><font color="#990000"><nowiki>=</nowiki></font></span> hello
-
targets = hello
+
<span><font color="#990000">.</font></span>PHONY<span><font color="#990000"><nowiki>:</nowiki></font></span> all
-
.PHONY: all
+
all<span><font color="#990000"><nowiki>:</nowiki></font></span> <span><font color="#009900">$(targets)</font></span>
-
all: $(targets)
+
hello<span><font color="#990000"><nowiki>:</nowiki></font></span> hello<span><font color="#990000">.</font></span>o hello_func<span><font color="#990000">.</font></span>o
-
hello: hello.o hello_func.o
+
        <span><font color="#009900">$(CC)</font></span> <span><font color="#009900">$^</font></span> -o <span><font color="#009900">$@</font></span>
-
        $(CC) $^ -o $@
+
hello<span><font color="#990000">.</font></span>o<span><font color="#990000"><nowiki>:</nowiki></font></span> hello<span><font color="#990000">.</font></span>c hello_api<span><font color="#990000">.</font></span>h
-
hello.o: hello.c hello_api.h
+
        <span><font color="#009900">$(CC)</font></span> <span><font color="#009900">$(CFLAGS)</font></span> -c <span><font color="#009900">$&lt;</font></span> -o <span><font color="#009900">$@</font></span>
-
        $(CC) $(CFLAGS) -c $< -o $@
+
hello_func<span><font color="#990000">.</font></span>o<span><font color="#990000"><nowiki>:</nowiki></font></span> hello_func<span><font color="#990000">.</font></span>c hello_api<span><font color="#990000">.</font></span>h
-
hello_func.o: hello_func.c hello_api.h
+
        <span><font color="#009900">$(CC)</font></span> <span><font color="#009900">$(CFLAGS)</font></span> -c <span><font color="#009900">$&lt;</font></span> -o <span><font color="#009900">$@</font></span>
-
        $(CC) $(CFLAGS) -c $< -o $@
+
<span><font color="#990000">.</font></span>PHONY<span><font color="#990000"><nowiki>:</nowiki></font></span> clean
-
.PHONY: clean
+
clean<span><font color="#990000"><nowiki>:</nowiki></font></span>
-
clean:
+
        <span><font color="#009900">$(RM)</font></span> <span><font color="#009900">$(targets)</font></span> <span><font color="#990000"><nowiki>*.</nowiki></font></span>o
-
        $(RM) $(targets) *.o
+
</tt>
-
</source>
+
Notice the cases when <code>$^</code> is used instead of <code>$&lt;</code> in the above snippet. It is not desired to pass the header files for compilation to the compiler, because the source file already includes the header files. For this reason, <code>$&lt;</code> is used. On the other hand, when linking programs and there are multiple files to put into the executable, <code>$^</code> would normally be used.
Notice the cases when <code>$^</code> is used instead of <code>$&lt;</code> in the above snippet. It is not desired to pass the header files for compilation to the compiler, because the source file already includes the header files. For this reason, <code>$&lt;</code> is used. On the other hand, when linking programs and there are multiple files to put into the executable, <code>$^</code> would normally be used.
Line 508: Line 502:
This relies on a couple of things:
This relies on a couple of things:
-
* <code>$(RM)</code> and <code>$(CC)</code> will be replaced with paths to the system compiler and removal commands.
+
* $(RM) and $(CC) will be replaced with paths to the system compiler and removal commands.
-
* <code>$(CFLAGS)</code> is a variable that contains a list of options to pass whenever make invokes the C compiler.
+
* $(CFLAGS) is a variable that contains a list of options to pass whenever make invokes the C compiler.
-
It can also be noticed that all these variable names are in uppercase. This signifies that they have been communicated from the system environment to make. In fact, if creating an environment variable called <code>CFLAGS</code>, make will create it internally for any makefile that it will process. This is the mechanism to communicate variables into makefiles from outside.
+
It can also be noticed that all these variable names are in uppercase. This signifies that they have been communicated from the system environment to make. In fact, if creating an environment variable called CFLAGS, make will create it internally for any makefile that it will process. This is the mechanism to communicate variables into makefiles from outside.
Writing variables in uppercase is a convention signaling external variables, or environmental variables, so this is the reason why lowercase should be used in own private variables within a Makefile.
Writing variables in uppercase is a convention signaling external variables, or environmental variables, so this is the reason why lowercase should be used in own private variables within a Makefile.
Line 517: Line 511:
=== Integrating with Pkg-Config ===
=== Integrating with Pkg-Config ===
-
The above has provided the knowledge to write a simple Makefile for the Hello World example. To get the the result of [http://pkg-config.freedesktop.org/wiki/ pkg-config], the GNU make <code>$(shell command params)</code> function will be used here. Its function is similar to the backtick operation of the shell. [https://vcs.maemo.org/svn/maemoexamples/tags/maemo_5.0/simple-make-files/Makefile.10 simple-make-files/Makefile.10]
+
The above has provided the knowledge to write a simple Makefile for the Hello World example. To get the the result of [http://pkg-config.freedesktop.org/wiki/ pkg-config], the GNU make $(shell command params) function will be used here. Its function is similar to the backtick operation of the shell. simple-make-files/Makefile.10
-
<source lang="make">
+
<tt><span>''<span><font color="#9A1900"><nowiki># define a list of pkg-config packages we want to use</nowiki></font></span>''</span>
-
# define a list of pkg-config packages we want to use
+
pkg_packages <span><font color="#990000"><nowiki>:=</nowiki></font></span> gtk<span><font color="#990000">+</font></span>-<span><font color="#993399">2.0</font></span> hildon-<span><font color="#993399">1</font></span>
-
pkg_packages := gtk+-2.0 hildon-1
+
<span>''<span><font color="#9A1900"><nowiki># get the necessary flags for compiling</nowiki></font></span>''</span>
-
# get the necessary flags for compiling
+
PKG_CFLAGS <span><font color="#990000"><nowiki>:=</nowiki></font></span> <span><font color="#009900">$(</font></span>shell pkg-config --cflags <span><font color="#009900">$(pkg_packages))</font></span>
-
PKG_CFLAGS := $(shell pkg-config --cflags $(pkg_packages))
+
<span>''<span><font color="#9A1900"><nowiki># get the necessary flags for linking</nowiki></font></span>''</span>
-
# get the necessary flags for linking
+
PKG_LDFLAGS <span><font color="#990000"><nowiki>:=</nowiki></font></span> <span><font color="#009900">$(</font></span>shell pkg-config --libs <span><font color="#009900">$(pkg_packages))</font></span>
-
PKG_LDFLAGS := $(shell pkg-config --libs $(pkg_packages))
+
<span>''<span><font color="#9A1900"><nowiki># additional flags</nowiki></font></span>''</span>
-
# additional flags
+
<span>''<span><font color="#9A1900"><nowiki># -Wall: warnings</nowiki></font></span>''</span>
-
# -Wall: warnings
+
<span>''<span><font color="#9A1900"><nowiki># -g: debugging</nowiki></font></span>''</span>
-
# -g: debugging
+
ADD_CFLAGS <span><font color="#990000"><nowiki>:=</nowiki></font></span> -Wall -g
-
ADD_CFLAGS := -Wall -g
+
<span>''<span><font color="#9A1900"><nowiki># combine the flags (so that CFLAGS/LDFLAGS from the command line</nowiki></font></span>''</span>
-
# combine the flags (so that CFLAGS/LDFLAGS from the command line
+
<span>''<span><font color="#9A1900"><nowiki># still work).</nowiki></font></span>''</span>
-
# still work).
+
CFLAGS  <span><font color="#990000"><nowiki>:=</nowiki></font></span> <span><font color="#009900">$(PKG_CFLAGS)</font></span> <span><font color="#009900">$(ADD_CFLAGS)</font></span> <span><font color="#009900">$(CFLAGS)</font></span>
-
CFLAGS  := $(PKG_CFLAGS) $(ADD_CFLAGS) $(CFLAGS)
+
LDFLAGS <span><font color="#990000"><nowiki>:=</nowiki></font></span> <span><font color="#009900">$(PKG_LDFLAGS)</font></span> <span><font color="#009900">$(LDFLAGS)</font></span>
-
LDFLAGS := $(PKG_LDFLAGS) $(LDFLAGS)
+
targets <span><font color="#990000"><nowiki>=</nowiki></font></span> hildon_helloworld-<span><font color="#993399">1</font></span>
-
targets = hildon_helloworld-1
+
<span><font color="#990000">.</font></span>PHONY<span><font color="#990000"><nowiki>:</nowiki></font></span> all
-
.PHONY: all
+
all<span><font color="#990000"><nowiki>:</nowiki></font></span> <span><font color="#009900">$(targets)</font></span>
-
all: $(targets)
+
hildon_helloworld-<span><font color="#993399">1</font></span><span><font color="#990000"><nowiki>:</nowiki></font></span> hildon_helloworld-<span><font color="#993399">1</font></span><span><font color="#990000">.</font></span>o
-
hildon_helloworld-1: hildon_helloworld-1.o
+
        <span><font color="#009900">$(CC)</font></span> <span><font color="#009900">$^</font></span> -o <span><font color="#009900">$@</font></span> <span><font color="#009900">$(LDFLAGS)</font></span>
-
        $(CC) $^ -o $@ $(LDFLAGS)
+
hildon_helloworld-<span><font color="#993399">1</font></span><span><font color="#990000">.</font></span>o<span><font color="#990000"><nowiki>:</nowiki></font></span> hildon_helloworld-<span><font color="#993399">1</font></span><span><font color="#990000">.</font></span>c
-
hildon_helloworld-1.o: hildon_helloworld-1.c
+
        <span><font color="#009900">$(CC)</font></span> <span><font color="#009900">$(CFLAGS)</font></span> -c <span><font color="#009900">$&lt;</font></span> -o <span><font color="#009900">$@</font></span>
-
        $(CC) $(CFLAGS) -c $< -o $@
+
<span><font color="#990000">.</font></span>PHONY<span><font color="#990000"><nowiki>:</nowiki></font></span> clean
-
.PHONY: clean
+
clean<span><font color="#990000"><nowiki>:</nowiki></font></span>
-
clean:
+
        <span><font color="#009900">$(RM)</font></span> <span><font color="#009900">$(targets)</font></span> <span><font color="#990000"><nowiki>*.</nowiki></font></span>o
-
        $(RM) $(targets) *.o
+
</tt>
-
</source>
+
-
You might wonder, where the <code>CC</code> and <code>RM</code> variables come from. They certainly were not declared anywhere in the Makefile. GNU make comes with a list of pre-defined variables for many programs and their arguments. GNU make also contains a preset list of pattern rules (which will not be dealt here), but basically these are pre-defined rules that are used when (for example) one needs an .o file from an .c file. The rules that were manually specified above are actually the same rules that GNU make already contains, so the Makefile can be made even more compact by only specifying the dependencies between files.
+
You might wonder, where the CC and RM variables come from. They certainly were not declared anywhere in the Makefile. GNU make comes with a list of pre-defined variables for many programs and their arguments. GNU make also contains a preset list of pattern rules (which will not be dealt here), but basically these are pre-defined rules that are used when (for example) one needs an .o file from an .c file. The rules that were manually specified above are actually the same rules that GNU make already contains, so the Makefile can be made even more compact by only specifying the dependencies between files.
-
This leads to the following, slightly simpler version: [https://vcs.maemo.org/svn/maemoexamples/tags/maemo_5.0/simple-make-files/Makefile.11 simple-make-files/Makefile.11]
+
This leads to the following, slightly simpler version: simple-make-files/Makefile.11
-
<source lang="make">
+
<tt><span>''<span><font color="#9A1900"><nowiki># define a list of pkg-config packages we want to use</nowiki></font></span>''</span>
-
# define a list of pkg-config packages we want to use
+
pkg_packages <span><font color="#990000"><nowiki>:=</nowiki></font></span> gtk<span><font color="#990000">+</font></span>-<span><font color="#993399">2.0</font></span> hildon-<span><font color="#993399">1</font></span>
-
pkg_packages := gtk+-2.0 hildon-1
+
PKG_CFLAGS <span><font color="#990000"><nowiki>:=</nowiki></font></span> <span><font color="#009900">$(</font></span>shell pkg-config -cflags <span><font color="#009900">$(pkg_packages))</font></span>
-
PKG_CFLAGS := $(shell pkg-config -cflags $(pkg_packages))
+
PKG_LDFLAGS <span><font color="#990000"><nowiki>:=</nowiki></font></span> <span><font color="#009900">$(</font></span>shell pkg-config -libs <span><font color="#009900">$(pkg_packages))</font></span>
-
PKG_LDFLAGS := $(shell pkg-config -libs $(pkg_packages))
+
ADD_CFLAGS <span><font color="#990000"><nowiki>:=</nowiki></font></span> -Wall -g
-
ADD_CFLAGS := -Wall -g
+
<span>''<span><font color="#9A1900"><nowiki># combine the flags</nowiki></font></span>''</span>
-
# combine the flags
+
CFLAGS  <span><font color="#990000"><nowiki>:=</nowiki></font></span> <span><font color="#009900">$(PKG_CFLAGS)</font></span> <span><font color="#009900">$(ADD_CFLAGS)</font></span> <span><font color="#009900">$(CFLAGS)</font></span>
-
CFLAGS  := $(PKG_CFLAGS) $(ADD_CFLAGS) $(CFLAGS)
+
LDFLAGS <span><font color="#990000"><nowiki>:=</nowiki></font></span> <span><font color="#009900">$(PKG_LDFLAGS)</font></span> <span><font color="#009900">$(LDFLAGS)</font></span>
-
LDFLAGS := $(PKG_LDFLAGS) $(LDFLAGS)
+
targets <span><font color="#990000"><nowiki>=</nowiki></font></span> hildon_helloworld-<span><font color="#993399">1</font></span>
-
targets = hildon_helloworld-1
+
<span><font color="#990000">.</font></span>PHONY<span><font color="#990000"><nowiki>:</nowiki></font></span> all
-
.PHONY: all
+
all<span><font color="#990000"><nowiki>:</nowiki></font></span> <span><font color="#009900">$(targets)</font></span>
-
all: $(targets)
+
<span>''<span><font color="#9A1900"><nowiki># we can omit the rules and just specify the dependencies</nowiki></font></span>''</span>
-
# we can omit the rules and just specify the dependencies
+
<span>''<span><font color="#9A1900"><nowiki># for our simple project this doesn't seem like a big win</nowiki></font></span>''</span>
-
# for our simple project this doesn't seem like a big win
+
<span>''<span><font color="#9A1900"><nowiki># but for larger projects where you have multiple object</nowiki></font></span>''</span>
-
# but for larger projects where you have multiple object
+
<span>''<span><font color="#9A1900"><nowiki># files, this will save considerable time.</nowiki></font></span>''</span>
-
# files, this will save considerable time.
+
hildon_helloworld-<span><font color="#993399">1</font></span><span><font color="#990000"><nowiki>:</nowiki></font></span> hildon_helloworld-<span><font color="#993399">1</font></span><span><font color="#990000">.</font></span>o
-
hildon_helloworld-1: hildon_helloworld-1.o
+
hildon_helloworld-<span><font color="#993399">1</font></span><span><font color="#990000">.</font></span>o<span><font color="#990000"><nowiki>:</nowiki></font></span> hildon_helloworld-<span><font color="#993399">1</font></span><span><font color="#990000">.</font></span>c
-
hildon_helloworld-1.o: hildon_helloworld-1.c
+
<span><font color="#990000">.</font></span>PHONY<span><font color="#990000"><nowiki>:</nowiki></font></span> clean
-
.PHONY: clean
+
clean<span><font color="#990000"><nowiki>:</nowiki></font></span>
-
clean:
+
        <span><font color="#009900">$(RM)</font></span> <span><font color="#009900">$(targets)</font></span> <span><font color="#990000"><nowiki>*.</nowiki></font></span>o
-
        $(RM) $(targets) *.o
+
</tt>
-
</source>
+
== GNU Autotools ==
== GNU Autotools ==
Line 602: Line 594:
Autoconf is a tool that uses the GNU m4 macro preprocessor to process the configuration file and output a shell script based on the macros used in the file. Anything that m4 does not recognize as a macro is passed verbatim to the output script. This means that almost any wanted shell script fragments can be included into the <code>configure.ac</code> file (the modern name for the default configuration file for autoconf).
Autoconf is a tool that uses the GNU m4 macro preprocessor to process the configuration file and output a shell script based on the macros used in the file. Anything that m4 does not recognize as a macro is passed verbatim to the output script. This means that almost any wanted shell script fragments can be included into the <code>configure.ac</code> file (the modern name for the default configuration file for autoconf).
-
The first subject here is a simple example to see how the basic configuration file works. Then some limitations of m4 syntax are covered, and hints on how to avoid problems with the syntax are given. [https://vcs.maemo.org/svn/maemoexamples/tags/maemo_5.0/autoconf-automake/example1/configure.ac autoconf-automake/example1/configure.ac]
+
The first subject here is a simple example to see how the basic configuration file works. Then some limitations of m4 syntax are covered, and hints on how to avoid problems with the syntax are given. autoconf-automake/example1/configure.ac
<source lang="autoconf">
<source lang="autoconf">
Line 719: Line 711:
  </nowiki>
  </nowiki>
-
Autoconf does not output information about its progress. Normally, only errors are output on stdout. Instead, it creates a new script called <code>configure</code> in the same directory, and sets it as executable.
+
Autoconf does not output information about its progress. Normally, only errors are output on stdout. Instead, it creates a new script called '''configure''' in the same directory, and sets it as executable.
The configure script is the result of all of the macro processing. If taking a look at the generated file with an editor or by using less, it can be seen that it contains a lot of shell code.
The configure script is the result of all of the macro processing. If taking a look at the generated file with an editor or by using less, it can be seen that it contains a lot of shell code.
Line 772: Line 764:
Besides creating the configure script, autoconf can do other useful things as well. Some people say that autoconf is at least as powerful as emacs, if not more so. Unfortunately, with all this power comes also lot of complexity. Sometimes finding out why things do not quite work can be rather difficult.
Besides creating the configure script, autoconf can do other useful things as well. Some people say that autoconf is at least as powerful as emacs, if not more so. Unfortunately, with all this power comes also lot of complexity. Sometimes finding out why things do not quite work can be rather difficult.
-
It can be useful to use the variables inside text files that are not directly related to the <code>configure.ac</code>'''. These might be configuration files or files that are used in some part of the building process later on. For this, autoconf provides a mechanism called ''substitution''. A special macro reads in an external file, replaces all instances of variable names in it, and then stores the resulting file as a new file. The convention in naming the input files is to add a suffix .in to the names. The name of generated output file are the same but without the suffix.  
+
It can be useful to use the variables inside text files that are not directly related to the '''configure.ac'''. These might be configuration files or files that are used in some part of the building process later on. For this, autoconf provides a mechanism called ''substitution''. A special macro reads in an external file, replaces all instances of variable names in it, and then stores the resulting file as a new file. The convention in naming the input files is to add a suffix .in to the names. The name of generated output file are the same but without the suffix.  
Note that the substitution is done when the generated configure script is run, not when autoconf is run.
Note that the substitution is done when the generated configure script is run, not when autoconf is run.
Line 778: Line 770:
The generated configure script replaces all occurrences of the variable name surrounded with 'at' (@) characters with the variable value when it reads through each of the input files.
The generated configure script replaces all occurrences of the variable name surrounded with 'at' (@) characters with the variable value when it reads through each of the input files.
-
This is best illustrated with a small example. The input file contents are listed after the autoconf configuration file. In this example, the substitution is only made for one file, but it is also possible to process multiple files using the substitution mechanism. [https://vcs.maemo.org/svn/maemoexamples/tags/maemo_5.0/autoconf-automake/example2/configure.ac autoconf-automake/example2/configure.ac]
+
This is best illustrated with a small example. The input file contents are listed after the autoconf configuration file. In this example, the substitution is only made for one file, but it is also possible to process multiple files using the substitution mechanism. autoconf-automake/example2/configure.ac
<source lang="autoconf">
<source lang="autoconf">
Line 798: Line 790:
</source>
</source>
-
[https://vcs.maemo.org/svn/maemoexamples/tags/maemo_5.0/autoconf-automake/example2/test-output.txt.in autoconf-automake/example2/test-output.txt.in]
+
autoconf-automake/example2/test-output.txt.in
<pre>
<pre>
Line 881: Line 873:
In order for this to work, two things are necessary:
In order for this to work, two things are necessary:
-
* A configuration file for automake (conventionally <code>Makefile.am</code>) which contains the files to be generated using make
+
* A configuration file for automake (conventionally Makefile.am) which contains the files to be generated using make
-
* A configuration file for autoconf (conventionally <code>configure.ac</code>) which lists the tests which are required to ensure that the system has all the required dependencies
+
* A configuration file for autoconf (conventionally configure.ac) which lists the tests which are required to ensure that the system has all the required dependencies
-
Running automake will generate a file <code>Makefile.in</code> containing a number of standard targets for make, and running autoconf will generate a configure script which will test your system, and generate final Makefiles and other files based on the software it finds present on the system.
+
Running automake will generate a file Makefile.in containing a number of standard targets for make, and running autoconf will generate a configure script which will test your system, and generate final Makefiles and other files based on the software it finds present on the system.
-
The example starts with the autoconf configuration file: autoconf-[https://vcs.maemo.org/svn/maemoexamples/tags/maemo_5.0/autoconf-automake/example3/configure.ac automake/example3/configure.ac]
+
The example starts with the autoconf configuration file: autoconf-automake/example3/configure.ac
<source lang="autoconf">
<source lang="autoconf">
Line 905: Line 897:
</source>
</source>
-
Then the configuration file is presented for automake: [https://vcs.maemo.org/svn/maemoexamples/tags/maemo_5.0/autoconf-automake/example3/Makefile.am autoconf-automake/example3/Makefile.am]
+
Then the configuration file is presented for automake: autoconf-automake/example3/Makefile.am
<source lang="make">
<source lang="make">
Line 932: Line 924:
</source>
</source>
-
This material tries to introduce just enough of automake for it to be useful for small projects. Because of this, the detailed syntax of <code>Makefile.am</code> is not explained. Based on the above description, automake knows what kind of <code>Makefile.in</code> to create, and autoconf takes it over from there and fills in the missing pieces.
+
This material tries to introduce just enough of automake for it to be useful for small projects. Because of this, the detailed syntax of Makefile.am is not explained. Based on the above description, automake knows what kind of Makefile.in to create, and autoconf takes it over from there and fills in the missing pieces.
-
The source files for the project are as simple as possible, but notice the implementation of printHello. [https://vcs.maemo.org/svn/maemoexamples/tags/maemo_5.0/autoconf-automake/example3/helloapp.c autoconf-automake/example3/helloapp.c]
+
The source files for the project are as simple as possible, but notice the implementation of printHello. autoconf-automake/example3/helloapp.c
<source lang="c">
<source lang="c">
Line 951: Line 943:
</source>
</source>
-
[https://vcs.maemo.org/svn/maemoexamples/tags/maemo_5.0/autoconf-automake/example3/hello.h autoconf-automake/example3/hello.h]
+
autoconf-automake/example3/hello.h
<source lang="c">
<source lang="c">
Line 966: Line 958:
</source>
</source>
-
[https://vcs.maemo.org/svn/maemoexamples/tags/maemo_5.0/autoconf-automake/example3/hello.c autoconf-automake/example3/hello.c]
+
autoconf-automake/example3/hello.c
<source lang="c">
<source lang="c">
Line 986: Line 978:
</source>
</source>
-
The <code>PACKAGE</code> and <code>VERSION</code> defines are passed to the build process automatically.
+
The PACKAGE and VERSION defines is passed to the build process automatically.
  [sbox-FREMANTLE_X86: ~/example3] &gt; autoconf
  [sbox-FREMANTLE_X86: ~/example3] &gt; autoconf
Line 995: Line 987:
Autoconf is complaining about a macro, for which it cannot find a definition (actually m4 is the program that does the complaining). The problem is that by default, autoconf only knows about built-in macros. When using a macro for integration or a macro that comes with another package, autoconf needs to be told about it. Luckily, this process is quite painless.
Autoconf is complaining about a macro, for which it cannot find a definition (actually m4 is the program that does the complaining). The problem is that by default, autoconf only knows about built-in macros. When using a macro for integration or a macro that comes with another package, autoconf needs to be told about it. Luckily, this process is quite painless.
-
A local <code>aclocal.m4</code> file needs to be created into the same directory with the <code>configure.ac</code>. When starting, autoconf reads this file, and it is enough to put the necessary macros there.
+
A local '''aclocal.m4''' file needs to be created into the same directory with the '''configure.ac'''. When starting, autoconf reads this file, and it is enough to put the necessary macros there.
-
For this, a utility program called aclocal is used, which scans the configure.ac and copy the relevant macros into the local <code>aclocal.m4</code>. The directory for all the extension macros is usually <code>/usr/share/aclocal/</code>, which should be checked out at some point.
+
For this, a utility program called aclocal is used, which scans the configure.ac and copy the relevant macros into the local aclocal.m4. The directory for all the extension macros is usually '''/usr/share/aclocal/''', which should be checked out at some point.
Now it is time to run aclocal, and then try and run autoconf again.  
Now it is time to run aclocal, and then try and run autoconf again.  
Line 1,021: Line 1,013:
</pre>
</pre>
-
Listing the contents of the directory reveals that the <code>Makefile.in</code> is not among the contents. Automake needs to be run manually, so that the file is created. At the same time, the missing files need to be introduced to the directory (such as the install.sh that configure seems to complain about).
+
Listing the contents of the directory reveals that the '''Makefile.in''' is not among the contents. Automake needs to be run manually, so that the file is created. At the same time, the missing files need to be introduced to the directory (such as the install.sh that configure seems to complain about).
-
This is done by executing automake -ac, which creates the Makefile.in and also copy the missing files into their proper places. ''This step also copies a file called <code>COPYING</code> into the directory, which by default contains the GPL.'' So, if the software is going to be distributed under some other license, this is the correct moment to replace the license file with the appropriate one.
+
This is done by executing automake -ac, which creates the Makefile.in and also copy the missing files into their proper places. ''This step also copies a file called COPYING into the directory, which by default contains the GPL.'' So, if the software is going to be distributed under some other license, this is the correct moment to replace the license file with the appropriate one.
<pre>
<pre>
Line 1,052: Line 1,044:
</pre>
</pre>
-
Notice the second to last line of the output, which shows that configure just created a Makefile (based on the <code>Makefile.in</code> that ''automake'' created).
+
Notice the second to last line of the output, which shows that configure just created a Makefile (based on the Makefile.in that ''automake'' created).
-
Performing all these steps manually each time to make sure that all the generated files are really generated can be rather tedious. Most developers create a script called '''autogen.sh''', which implements the necessary bootstrap procedures for them. Below is a file that is suitable for this example. Real-life projects can have more steps because of localization and other requirements. [https://vcs.maemo.org/svn/maemoexamples/tags/maemo_5.0/autoconf-automake/example3/autogen.sh autoconf-automake/example3/autogen.sh]
+
Performing all these steps manually each time to make sure that all the generated files are really generated can be rather tedious. Most developers create a script called '''autogen.sh''', which implements the necessary bootstrap procedures for them. Below is a file that is suitable for this example. Real-life projects can have more steps because of localization and other requirements. autoconf-automake/example3/autogen.sh
<source lang="bash">
<source lang="bash">
Line 1,115: Line 1,107:
=== Checking for Distribution Sanity ===
=== Checking for Distribution Sanity ===
-
The generated Makefile contains various targets that can be used when creating distribution tarballs (tar files containing the source code and the necessary files to build the software). The most important of these is the dist-target, which by default creates a .tar.gz-file out of the source, including the configure script and other necessary files (which are specified in <code>Makefile.am</code>).
+
The generated Makefile contains various targets that can be used when creating distribution tarballs (tar files containing the source code and the necessary files to build the software). The most important of these is the dist-target, which by default creates a .tar.gz-file out of the source, including the configure script and other necessary files (which are specified in Makefile.am).
To test whether building the software from the distribution tarball is possible, execute the distcheck-target. It first creates a distribution tarball, then extracts it in a new subdirectory, runs configure there and tries and builds the software with the default make target. If it fails, the relevant error is given.
To test whether building the software from the distribution tarball is possible, execute the distcheck-target. It first creates a distribution tarball, then extracts it in a new subdirectory, runs configure there and tries and builds the software with the default make target. If it fails, the relevant error is given.
-
Making the distcheck target each time before making a dist target is recommended, so that you can be sure that the distribution tarball can be used outside the source tree. This step is especially critical later when [[packaging|making Debian packages]].
+
Making the distcheck target each time before making a dist target is recommended, so that you can be sure that the distribution tarball can be used outside the source tree. This step is especially critical later when making Debian packages.
=== Cleaning up ===
=== Cleaning up ===
-
For the sake of convenience, the example3 directory also includes a script called <code>antigen.sh</code>, which tries its best to get rid of all generated files (it is necessary to <code>autogen.sh</code> the project afterwards).
+
For the sake of convenience, the example3 directory also includes a script called antigen.sh, which tries its best to get rid of all generated files (it is necessary to autogen.sh the project afterwards).
Having a clean-up script is not very common in open source projects, but it is especially useful when starting autotools development because it allows testing the toolchain easily from scratch.
Having a clean-up script is not very common in open source projects, but it is especially useful when starting autotools development because it allows testing the toolchain easily from scratch.
-
The contents of antigen.sh that is suitable for simple projects: [https://vcs.maemo.org/svn/maemoexamples/tags/maemo_5.0/autoconf-automake/example3/antigen.sh autoconf-automake/example3/antigen.sh]
+
The contents of antigen.sh that is suitable for simple projects: autoconf-automake/example3/antigen.sh
<source lang="bash">
<source lang="bash">
Line 1,154: Line 1,146:
=== Integration with Pkg-Config ===
=== Integration with Pkg-Config ===
-
The last part that is covered for autoconf is how to integrate pkg-config support into the projects when using <code>configure.ac</code>.
+
The last part that is covered for autoconf is how to integrate pkg-config support into the projects when using configure.ac.
Pkg-config comes with a macro package ('''pkg.m4'''), which contains some useful macros for integration. The best documentation for these can be found in the pkg-config manual pages.
Pkg-config comes with a macro package ('''pkg.m4'''), which contains some useful macros for integration. The best documentation for these can be found in the pkg-config manual pages.
-
For the purpose of this example, only one macro is used, <code>PKG_CHECK_MODULES</code>, which is used as part of <code>configure.ac</code> for pkg-config integration
+
For the purpose of this example, only one macro is used, <code>PKG_CHECK_MODULES</code>, which is used as part of configure.ac for pkg-config integration
-
<source lang="text">
+
<tt><span>''<span><font color="#9A1900"><nowiki># Check whether the necessary pkg-config packages are present. The</nowiki></font></span>''</span>
-
# Check whether the necessary pkg-config packages are present. The
+
<span>''<span><font color="#9A1900"><nowiki># PKG_CHECK_MODULES macro is supplied by pkg-config</nowiki></font></span>''</span>
-
# PKG_CHECK_MODULES macro is supplied by pkg-config
+
<span>''<span><font color="#9A1900"><nowiki># (/usr/share/aclocal/).</nowiki></font></span>''</span>
-
# (/usr/share/aclocal/).
+
<span>''<span><font color="#9A1900"><nowiki>#</nowiki></font></span>''</span>
-
#
+
<span>''<span><font color="#9A1900"><nowiki># The first parameter is the variable name prefix that is</nowiki></font></span>''</span>
-
# The first parameter is the variable name prefix that is
+
<span>''<span><font color="#9A1900"><nowiki># used to create two variables: one to hold the CFLAGS required by</nowiki></font></span>''</span>
-
# used to create two variables: one to hold the CFLAGS required by
+
<span>''<span><font color="#9A1900"><nowiki># the packages, and one to hold the LDFLAGS (LIBS) required by the</nowiki></font></span>''</span>
-
# the packages, and one to hold the LDFLAGS (LIBS) required by the
+
<span>''<span><font color="#9A1900"><nowiki># packages. The variable name prefix (HHW) can be chosen freely.</nowiki></font></span>''</span>
-
# packages. The variable name prefix (HHW) can be chosen freely.
+
PKG_CHECK_MODULES<span><font color="#990000">(</font></span>HHW<span><font color="#990000">,</font></span> gtk<span><font color="#990000">+</font></span>-<span><font color="#993399">2.0</font></span> hildon-<span><font color="#993399">1</font></span> hildon-fm-<span><font color="#993399">2</font></span> gnome-vfs-<span><font color="#993399">2.0</font></span> <span><font color="#990000">\</font></span>
-
PKG_CHECK_MODULES(HHW, gtk+-2.0 hildon-1 hildon-fm-2 gnome-vfs-2.0 \
+
                        gconf-<span><font color="#993399">2.0</font></span> libosso<span><font color="#990000">)</font></span>
-
                      gconf-2.0 libosso)
+
<span>''<span><font color="#9A1900"><nowiki># At this point HHW_CFLAGS contains the necessary compiler flags</nowiki></font></span>''</span>
-
# At this point HHW_CFLAGS contains the necessary compiler flags
+
<span>''<span><font color="#9A1900"><nowiki># and HHW_LIBS contains the linker options necessary for all the</nowiki></font></span>''</span>
-
# and HHW_LIBS contains the linker options necessary for all the
+
<span>''<span><font color="#9A1900"><nowiki># packages listed above.</nowiki></font></span>''</span>
-
# packages listed above.
+
<span>''<span><font color="#9A1900"><nowiki>#</nowiki></font></span>''</span>
-
#
+
<span>''<span><font color="#9A1900"><nowiki># Add the pkg-config supplied values to the ones that are used by</nowiki></font></span>''</span>
-
# Add the pkg-config supplied values to the ones that are used by
+
<span>''<span><font color="#9A1900"><nowiki># Makefile or supplied by the user running ./configure.</nowiki></font></span>''</span>
-
# Makefile or supplied by the user running ./configure.
+
<span><font color="#009900">CFLAGS</font></span><span><font color="#990000"><nowiki>=</nowiki></font></span><span><font color="#FF0000">"$HHW_CFLAGS $CFLAGS"</font></span>
-
CFLAGS="$HHW_CFLAGS $CFLAGS"
+
<span><font color="#009900">LIBS</font></span><span><font color="#990000"><nowiki>=</nowiki></font></span><span><font color="#FF0000">"$HHW_LIBS $LIBS"</font></span></tt>
-
LIBS="$HHW_LIBS $LIBS"
+
-
</source>
+
The proper placing for this code is after all the <code>AC_PROG_*</code> checks and before the <code>AC_OUTPUT</code> macros (so that the build flags may affect the substituted files).
The proper placing for this code is after all the <code>AC_PROG_*</code> checks and before the <code>AC_OUTPUT</code> macros (so that the build flags may affect the substituted files).

Learn more about Contributing to the wiki.


Please note that all contributions to maemo.org wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see maemo.org wiki:Copyrights for details). Do not submit copyrighted work without permission!


Cancel | Editing help (opens in new window)

Templates used on this page: