| 1 | <chapter> |
| 2 | <title>Using libgutenprint</title> |
| 3 | <para> |
| 4 | This chapter describes how to write programs that use |
| 5 | libgutenprint. |
| 6 | </para> |
| 7 | <sect1> |
| 8 | <title>Code prerequisites</title> |
| 9 | <para> |
| 10 | To use libgutenprint with a program, several steps must be taken: |
| 11 | </para> |
| 12 | <itemizedlist> |
| 13 | <listitem> |
| 14 | <para> |
| 15 | Include the master libgutenprint header: |
| 16 | <informalexample> |
| 17 | <programlisting><gimp-print/gimp-print.h></programlisting> |
| 18 | </informalexample> |
| 19 | </para> |
| 20 | </listitem> |
| 21 | <listitem> |
| 22 | <para> |
| 23 | Call <function>stp_init</function>. |
| 24 | </para> |
| 25 | </listitem> |
| 26 | <listitem> |
| 27 | <para> |
| 28 | Link with the libgutenprint library. |
| 29 | </para> |
| 30 | </listitem> |
| 31 | </itemizedlist> |
| 32 | <para> |
| 33 | The following is a short example program. It does not do |
| 34 | anything useful, but it does everything required to link with |
| 35 | libgutenprint and call other functions from libgutenprint. |
| 36 | </para> |
| 37 | <informalexample> |
| 38 | <programlisting>#include <gimp-print/gimp-print.h> |
| 39 | int |
| 40 | main (int argc, char *argv[]) |
| 41 | { |
| 42 | stp_init(); |
| 43 | return 0; |
| 44 | } |
| 45 | </programlisting> |
| 46 | </informalexample> |
| 47 | </sect1> |
| 48 | <sect1> |
| 49 | <title>Linking with libgutenprint</title> |
| 50 | <para> |
| 51 | To link a program with libgutenprint, the option |
| 52 | <option>-lgutenprint</option> needs to be passed to the compiler |
| 53 | when linking. For example, to compile and link |
| 54 | <filename>stpimage.c</filename> the following commands would be |
| 55 | used: |
| 56 | </para> |
| 57 | <screen><prompt>$</prompt> <userinput>gcc -c stpimage.c</userinput> |
| 58 | <prompt>$</prompt> <userinput>gcc -o stpimage -lgutenprint stpimage.o</userinput></screen> |
| 59 | <para> |
| 60 | The compiler and linker flags needed may vary depending on the |
| 61 | options Gutenprint was configured with when it was built. The |
| 62 | <command>pkg-config</command> script will give the correct |
| 63 | parameters for the local installation. |
| 64 | </para> |
| 65 | </sect1> |
| 66 | <sect1> |
| 67 | <title>Integrating libgutenprint</title> |
| 68 | <para> |
| 69 | This section describes how to integrate the compiling and |
| 70 | linking of programs using libgutenprint with build |
| 71 | scripts. Commonly used systems include <command>make</command>, |
| 72 | but often <filename>Makefile</filename> files are generated by |
| 73 | using tools such as <command>autoconf</command> and |
| 74 | <command>automake</command>. |
| 75 | </para> |
| 76 | <sect2> |
| 77 | <title><command>pkg-config</command></title> |
| 78 | <para> |
| 79 | Depending on the nature of the computer system Gutenprint was |
| 80 | installed on, as well as the options passed to |
| 81 | <command>configure</command> when configuring the package when |
| 82 | it was built, the <varname>CFLAGS</varname> and |
| 83 | <varname>LIBS</varname> parameters needed to compile and link |
| 84 | programs with libgutenprint may vary. To make it simple to |
| 85 | determine what these are on any given system, a |
| 86 | <command>pkg-config</command> datafile was created when |
| 87 | Gutenprint was built. <command>pkg-config</command> will |
| 88 | output the correct parameters for the setup on your system. |
| 89 | See the |
| 90 | <citerefentry><refentrytitle>pkg-config(1)</refentrytitle> |
| 91 | </citerefentry> manual page for a compete synopsis. |
| 92 | </para> |
| 93 | <para> |
| 94 | The correct <varname>CFLAGS</varname> to use can be obtained |
| 95 | with the <option>--cflags</option> option: |
| 96 | </para> |
| 97 | <screen><prompt>$</prompt> <userinput>pkg-config --cflags gutenprint</userinput> |
| 98 | -I/usr/local/include</screen> |
| 99 | <para> |
| 100 | The correct <varname>LIBS</varname> to use can the obtained |
| 101 | with the <option>--libs</option> option: |
| 102 | </para> |
| 103 | <screen><prompt>$</prompt> <userinput>pkg-config --libs gutenprint</userinput> |
| 104 | -L/usr/local/lib -lgutenprint -lm -ldl</screen> |
| 105 | <para> |
| 106 | Lastly, the installed version of Gutenprint can be obtained with the |
| 107 | <varname>--version</varname> option: |
| 108 | </para> |
| 109 | <screen><prompt>$</prompt> <userinput>pkg-config --modversion gutenprint</userinput> |
| 110 | 4.3.23</screen> |
| 111 | <para> |
| 112 | The command can be used from the shell by enclosing it in |
| 113 | backquotes ‘`’: |
| 114 | </para> |
| 115 | <screen><prompt>$</prompt> <userinput>gcc `pkg-config --cflags gutenprint` -c stpimage.c</userinput> |
| 116 | <prompt>$</prompt> <userinput>gcc `pkg-config --libs gutenprint` -o |
| 117 | stpimage stpimage.o</userinput></screen> |
| 118 | <para> |
| 119 | However, this is not the way it it typically used. Normally |
| 120 | it is used in a <filename>Makefile</filename> or by an m4 |
| 121 | macro in a <command>configure</command> script. |
| 122 | </para> |
| 123 | </sect2> |
| 124 | <sect2> |
| 125 | <title><command>make</command></title> |
| 126 | <para> |
| 127 | If you use <command>make</command> with your own |
| 128 | <filename>Makefile</filename> files, then you are on your |
| 129 | own. This manual offers no assistance with doing this. Only |
| 130 | the following suggestion is offered, for use with GNU |
| 131 | <command>make</command>: |
| 132 | </para> |
| 133 | <programlisting>GUTENPRINT_VERSION = $(shell pkg-config --version gutenprint) |
| 134 | GUTENPRINT_CFLAGS = $(shell pkg-config --cflags gutenprint) |
| 135 | GUTENPRINT_LIBS = $(shell pkg-config --libs gutenprint)</programlisting> |
| 136 | <para> |
| 137 | How you choose to use these variables is entirely up to |
| 138 | you. See the GNU <command>make</command> manual for more |
| 139 | information. |
| 140 | </para> |
| 141 | </sect2> |
| 142 | <sect2> |
| 143 | <title><command>autoconf</command></title> |
| 144 | <para> |
| 145 | The <command>autoconf</command> program produces a Bourne |
| 146 | shell script called <filename>configure</filename> from a |
| 147 | template file called <filename>configure.ac</filename>. |
| 148 | <filename>configure.ac</filename> contains both Bourne shell |
| 149 | script, and m4 macros. <command>autoconf</command> expands |
| 150 | the m4 macros into ‘real’ shell script. The |
| 151 | resulting <command>configure</command> script performs various |
| 152 | checks for installed programs, compiler characteristics and |
| 153 | other system information such as available headers and |
| 154 | libraries. See the GNU <command>autoconf</command> manual for |
| 155 | more information. |
| 156 | </para> |
| 157 | <para> |
| 158 | <command>pkg-config</command> provides an m4 macro, |
| 159 | <function>PKG_CHECK_MODULES</function>, suitable for use in a |
| 160 | <filename>configure.ac</filename> script. It defines the |
| 161 | environment variables required for building libgutenprint-based |
| 162 | programs. For example, to set GUTENPRINT_CFLAGS and |
| 163 | GUTENPRINT_LIBS: |
| 164 | </para> |
| 165 | <informalexample> |
| 166 | <programlisting>PKG_CHECK_MODULES(GUTENPRINT, gutenprint)</programlisting> |
| 167 | </informalexample> |
| 168 | </sect2> |
| 169 | <sect2> |
| 170 | <title><command>automake</command></title> |
| 171 | <para> |
| 172 | The <command>automake</command> program can be used to |
| 173 | generate <filename>Makefile.in</filename> files suitable for |
| 174 | use with a <filename>configure</filename> script generated by |
| 175 | <command>autoconf</command>. As <command>automake</command> |
| 176 | <emphasis>requires</emphasis> <command>autoconf</command>, |
| 177 | this section will assume the use of a |
| 178 | <filename>configure.ac</filename> script which uses the |
| 179 | <function>PKG_CHECK_MODULES</function> macro described above |
| 180 | (there is little point in <emphasis>not</emphasis> using it!). |
| 181 | </para> |
| 182 | <para> |
| 183 | It is highly recommended that you use GNU |
| 184 | <command>autoconf</command> and |
| 185 | <command>automake</command>. They will allow you to make your |
| 186 | software build on most platforms with most compilers. |
| 187 | <command>automake</command> makes writing complex |
| 188 | <filename>Makefile</filename>'s very easy, by expressing how |
| 189 | to build your packages in terms of what files are required to |
| 190 | build a project and the installation locations of the files. |
| 191 | It imposes a few limitations over using plain |
| 192 | <filename>Makefile</filename>'s, such as in the use of |
| 193 | conditionals, but these problems are vastly outweighed by the |
| 194 | benefits it brings. It also creates many extra targets in the |
| 195 | generated <filename>Makefile.in</filename> files such as |
| 196 | <option>dist</option>, <option>distcheck</option>, |
| 197 | <option>clean</option>, <option>distclean</option>, |
| 198 | <option>maintainer-clean</option> and <option>tags</option>, |
| 199 | and there are many more more available. See the GNU |
| 200 | <command>automake</command> manual for more information. |
| 201 | </para> |
| 202 | <para> |
| 203 | Because <function>PKG_CHECK_MODULES</function> calls |
| 204 | <function>AC_SUBST</function> to substitute |
| 205 | <varname>GUTENPRINT_CFLAGS</varname> and |
| 206 | <varname>GUTENPRINT_LIBS</varname>, <command>automake</command> |
| 207 | will automatically set these variables in the |
| 208 | <filename>Makefile.in</filename> files it generates, requiring |
| 209 | no additional effort on your part! |
| 210 | </para> |
| 211 | <para> |
| 212 | As in previous examples, we will make a program |
| 213 | <command>stpimage</command> from |
| 214 | <filename>stpimage.c</filename>. This is how one might build |
| 215 | write a <filename>Makefile.am</filename> to do this: |
| 216 | </para> |
| 217 | <informalexample> |
| 218 | <programlisting>@SET_MAKE@ |
| 219 | |
| 220 | AM_CFLAGS = $(GUTENPRINT_CFLAGS) |
| 221 | |
| 222 | bin_PROGRAMS = stpimage |
| 223 | stpimage_SOURCES = stpimage.c |
| 224 | stpimage_LDADD = $(GUTENPRINT_LIBS) |
| 225 | |
| 226 | MAINTAINERCLEANFILES = Makefile.in</programlisting> |
| 227 | </informalexample> |
| 228 | <para> |
| 229 | That's all there is to it! Please note that this example also |
| 230 | requires the macro <function>AC_PROG_MAKE_SET</function> to be |
| 231 | used in <filename>configure.ac</filename>. |
| 232 | </para> |
| 233 | </sect2> |
| 234 | </sect1> |
| 235 | </chapter> |