1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML
><HEAD
><TITLE
>Compiling a KDE Program</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.54"/><LINK
REL="HOME"
TITLE="KDE 2.0 Development"
HREF="index.html"/><LINK
REL="UP"
TITLE="A Simple KDE Application"
HREF="ch02.html"/><LINK
REL="PREVIOUS"
TITLE="A Simple KDE Application"
HREF="ch02.html"/><LINK
REL="NEXT"
TITLE="KDE Application Structure"
HREF="ch02lev1sec3.html"/><META
HTTP-EQUIV="Content-Style-Type"
CONTENT="text/css"/><LINK
REL="stylesheet"
HREF="kde-common.css"
TYPE="text/css"/><META
HTTP-EQUIV="Content-Type"
CONTENT="text/html; charset=iso-8859-1"/><META
HTTP-EQUIV="Content-Language"
CONTENT="en"/><LINK
REL="stylesheet"
HREF="kde-localised.css"
TYPE="text/css"
TITLE="KDE-English"/><LINK
REL="stylesheet"
HREF="kde-default.css"
TYPE="text/css"
TITLE="KDE-Default"/></HEAD
><BODY
CLASS="section"
LINK="#336699"
VLINK="#336699"
ALINK="#336699"
BGCOLOR="#FFFFFF"
><DIV
ALIGN="RIGHT"
CLASS="NAVBAR"
><P
><A
HREF="ch02.html"
>Prev</A
> <A
HREF="ch02lev1sec3.html"
>Next</A
> <A
HREF="index.html"
>Table of Contents</A
></P
></DIV
><DIV
CLASS="section"
><TABLE
WIDTH="100%"
CELLPADDING="0"
CELLSPACING="0"
BORDER="0"
ALIGN="CENTER"
><TR
><TD
WIDTH="90%"
><H1
CLASS="section"
><A
NAME="ch02lev1sec2"
>2.2. Compiling a KDE Program</A
></H1
><P
>I am discussing compiling early on so that you may begin programming immediately. I hope that you will key in the source code that is presented, compile it, and play with it as you read. (You can also download the source code from the web site, but typing it in yourself will help familiarize you with class names and conventions that you may miss even in a careful reading.) The concepts that are presented will be clearer to you if you are programming them while you read.</P
><P
>The source code presented in <A
HREF="ch02lev1sec2.html#ch02list01"
>Listing 2.1</A
> is an example of a C++ program that depends on the KDE and Qt libraries. <TT
CLASS="literal"
>khello</TT
> is a simple application that says <SPAN
CLASS="QUOTE"
>"Hello!"</SPAN
></P
><DIV
CLASS="example"
><HR/><A
NAME="ch02list01"
></A
><P
><B
>Example 2.1. <TT
CLASS="literal"
>khello</TT
> is a Simple KDE Application that Says <SPAN
CLASS="QUOTE"
>"Hello!"</SPAN
></B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="programlisting"
> 1
2 1: #include <qstring.h>
3 2: #include <kapp.h>
4 3: #include <klined.h>
5 4:
6 5: int main (int argc, char *argv[])
7 6: {
8 7: KApplication khello (argc, argv, "khello");
9 8: KLineEdit *helloeditor = new KLineEdit (0);
10 9: QString hellostring ("Hello!");
11 10: helloeditor->setText (hellostring);
12 11: helloeditor->show();
13 12:
14 13: khello.setMainWidget (helloeditor);
15 14: return khello.exec();</PRE
></TD
></TR
></TABLE
><HR/></DIV
><P
>To compile this code, you first need to set the environment variables <TT
CLASS="literal"
>KDEDIR</TT
> and <TT
CLASS="literal"
>QTDIR</TT
>. <TT
CLASS="literal"
>KDEDIR</TT
> should be set to the path where KDE was installed. This is usually /opt/kde, but it is /usr on a Red Hat 6.x system. <TT
CLASS="literal"
>QTDIR</TT
> may be /usr/local/lib, /usr/lib, or some other directory. The command to set environment variables differs from shell to shell. If you are using <TT
CLASS="literal"
>bash</TT
>, for example, type</P
><DIV
CLASS="informalexample"
><HR/><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="programlisting"
> 1
2 <I
CLASS="emphasis"
>QTDIR=/usr/local/lib</I
>
3 <I
CLASS="emphasis"
>KDEDIR=/opt/kde</I
></PRE
></TD
></TR
></TABLE
><HR/></DIV
><P
>If you are using <TT
CLASS="literal"
>tcsh</TT
>, type</P
><DIV
CLASS="informalexample"
><HR/><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="programlisting"
> 1
2 <I
CLASS="emphasis"
>setenv QTDIR /usr/local/lib</I
>
3 <I
CLASS="emphasis"
>setenv KDEDIR /opt/kde</I
>
4 </PRE
></TD
></TR
></TABLE
><HR/></DIV
><P
>On typical systems, the command to compile the source code given in <A
HREF="ch02lev1sec2.html#ch02list01"
>Listing 2.1</A
> is</P
><DIV
CLASS="informalexample"
><HR/><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="programlisting"
> 1
2 <I
CLASS="emphasis"
>g++ khello.cpp -I$KDEDIR/include -I$QTDIR/include</I
>
3 <I
CLASS="emphasis"
>L$KDEDIR/lib -L$QTDIR/lib -lkdeui -lkdecore -ldl -lqt</I
>
4 </PRE
></TD
></TR
></TABLE
><HR/></DIV
><P
>On a Red Hat 6.x system, the Qt header files are in /usr/include/qt, and the libraries are in /usr/lib, which is checked by g++ by default. You should compile the program using the following command on a Red Hat 6.x system:</P
><DIV
CLASS="informalexample"
><HR/><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="programlisting"
> 1
2 <I
CLASS="emphasis"
>g++ khello.cpp -I$KDEDIR/include</I
>
3 <I
CLASS="emphasis"
>I/usr/include/qt -L$KDEDIR/lib -lkdeui -lkdecore -ldl -lqt</I
>
4 </PRE
></TD
></TR
></TABLE
><HR/></DIV
><P
>The compiler is the GNU C++ compiler, g++. khello.cpp is the name of the source-code file. <A
HREF="ch02lev1sec2.html#ch02list01"
>Listing 2.1</A
> shows that khello.cpp includes header files from the Qt (qlabel.h) and KDE (kapp.h and klined.h) library distributions.</P
><P
>The <TT
CLASS="literal"
>-I</TT
> option tells g++ in what directory to look for header files. The <TT
CLASS="literal"
>-L</TT
> option tells where, in addition to standard directories, to look for libraries.</P
><DIV
CLASS="informalexample"
><HR/><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="programlisting"
> 1
2 <I
CLASS="emphasis"
>g++ khello.cpp -I$KDEDIR/include -I$QTDIR/include</I
>
3 <I
CLASS="emphasis"
>L$KDEDIR/lib -L$QTDIR/lib -lkdecore –lkdeui</I
>
4 </PRE
></TD
></TR
></TABLE
><HR/></DIV
><P
>khello uses classes from the Qt library and from the KDE libraries libkdecore and libkdeui. The <TT
CLASS="literal"
>-l</TT
> option specifies which libraries (in addition to default libraries, such as libc) to link to khello.</P
><P
>khello is shown in <A
HREF="ch02lev1sec2.html#ch02fig02"
>Figure 2.2</A
>. Its window contains only a line editor with the text <SPAN
CLASS="QUOTE"
>"Hello!"</SPAN
> in it. You can edit the text and click the close button (the X in the upper-right corner of the window) when you are through.</P
><DIV
CLASS="figure"
><HR/><A
NAME="ch02fig02"
></A
><P
><B
>Figure 2.2. khello is a simple KDE application that says <SPAN
CLASS="QUOTE"
>"Hello!"</SPAN
></B
></P
><DIV
CLASS="mediaobject"
><P
><IMG
SRC="graphics/02fig02.gif"
></IMG
></P
></DIV
><HR/></DIV
><DIV
CLASS="section"
><TABLE
WIDTH="100%"
CELLPADDING="0"
CELLSPACING="0"
BORDER="0"
ALIGN="CENTER"
><TR
><TD
WIDTH="90%"
><H2
CLASS="section"
><A
NAME="ch02lev2sec3"
>2.2.1. Using <TT
CLASS="literal"
>make</TT
></A
></H2
><P
>The development process usually consists of the following steps:</P
><OL
COMPACT="COMPACT"
TYPE="1"
><LI
><P
>Edit the source code.</P
></LI
><LI
><P
>Compile it.</P
></LI
><LI
><P
>Test the program.</P
></LI
></OL
><P
>To minimize the overhead involved in compilation—especially on large projects—you can use the <TT
CLASS="literal"
>make</TT
> utility. <TT
CLASS="literal"
>make</TT
> will</P
><UL
COMPACT="COMPACT"
><LI
STYLE="list-style-type: disc"
><P
>Execute compilation commands, which can often be much longer than the one presented previously.</P
></LI
><LI
STYLE="list-style-type: disc"
><P
>Compile only the files that need recompiling. This can save a lot of time when a project consists of more than one file.</P
></LI
></UL
><P
>By examining source files, <TT
CLASS="literal"
>make</TT
> determines which files need recompiling; it checks whether the source files are newer than the object files that they get compiled to and whether the file depends on other files (such as header files) that have been updated. The drawback to using <TT
CLASS="literal"
>make</TT
> is that it takes time to describe how the files in your project depend on each other. To see why this is necessary, consider a project that has a source file called mysource.cpp, which <TT
CLASS="literal"
>#includes</TT
> a header called myheader.h. If you change mysource.cpp, you want it to be recompiled the next time you execute <TT
CLASS="literal"
>make</TT
>. You also want it recompiled if you make any changes to myheader.h, because ultimately, myheader.h becomes part of mysource.cpp. You will see in <A
HREF="ch16.html"
>Chapter 16, <SPAN
CLASS="QUOTE"
>"Packaging and Distributing Code,"</SPAN
></A
> how the standard KDE packaging automates the process of creating a <TT
CLASS="literal"
>Makefile</TT
>.</P
><P
>Listing 2.2 gives the <TT
CLASS="literal"
>Makefile</TT
> used for compiling Listings 2.3–2.5. This listings are the source code KSimpleApp, a simple KDE application. You should place all four of these files in the same directory and type <I
CLASS="emphasis"
><TT
CLASS="literal"
>make</TT
></I
> to compile the code and create an executable named <TT
CLASS="literal"
>ksimpleapp</TT
>. When keying in the <TT
CLASS="literal"
>Makefile</TT
>, you need to set the variables <TT
CLASS="literal"
>QTINC</TT
>, <TT
CLASS="literal"
>KDEINC</TT
>, <TT
CLASS="literal"
>QTLIB</TT
>, and <TT
CLASS="literal"
>KDELIB</TT
> to their correct values. Sample assignments of these variables are given in <A
HREF="ch02lev1sec2.html#ch02list02"
>Listing 2.2</A
>, lines 1–4.Variable assignment in makefiles works the same as you might have guessed from looking at <A
HREF="ch02lev1sec2.html#ch02list02"
>Listing 2.2</A
>:</P
><DIV
CLASS="informalexample"
><HR/><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="programlisting"
> 1
2 VARIABLE = <I
CLASS="emphasis"
>value</I
>
3 </PRE
></TD
></TR
></TABLE
><HR/></DIV
><P
>For more information about <TT
CLASS="literal"
>make</TT
>, see the man page (i.e., type <I
CLASS="emphasis"
><TT
CLASS="literal"
>man make</TT
></I
>).</P
><P
>From this point in the book, I won't present any more <TT
CLASS="literal"
>Makefiles</TT
>. You can adapt this <TT
CLASS="literal"
>Makefile</TT
> to compile later source code, or find prepared <TT
CLASS="literal"
>Makefiles</TT
> with the source code on the World Wide Web support site.</P
><DIV
CLASS="example"
><HR/><A
NAME="ch02list02"
></A
><P
><B
>Example 2.2. The <TT
CLASS="literal"
>Makefile</TT
> Used as Input to the <TT
CLASS="literal"
>make</TT
> Utility to Compile Listings 2.3–2.5</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="programlisting"
> 1
2 1: QTINC = -I$(QTDIR)/include
3 2: KDEINC = -I$(KDEDIR)/include
4 3: QTLIB = -L$(QTDIR)/lib
5 4: KDELIB = -L$(KDEDIR)/lib
6 5: QTBIN = $(QTDIR)/bin
7 6:
8 7: ksimpleapp : ksimpleapp.o main.o
9 8: g++ $(QTLIB) $(KDELIB) -lkdeui -lkdecore -lqt -ldl \
10 main.o ksimpleapp.o -o ksimpleapp
11 9:
12 10: main.o : main.cpp
13 11: g++ -c $(QTINC) $(KDEINC) main.cpp
14 12:
15 13: ksimpleapp.moc : ksimpleapp.h
16 14: $(QTBIN)/moc ksimpleapp.h > ksimpleapp.moc
17 15:
18 16: ksimpleapp.o : ksimpleapp.cpp ksimpleapp.moc
19 17: g++ -c $(QTINC) $(KDEINC) ksimpleapp.cpp</PRE
></TD
></TR
></TABLE
><HR/></DIV
></TD
><TD
WIDTH="10%"
VALIGN="BOTTOM"
ALIGN="CENTER"
><ANNMARK
NAME="ch02lev2sec3"/></TD
></TR
><ANNOTATION
NAME="ch02lev2sec3"
TITLE="Using make"/></TABLE
></DIV
></TD
><TD
WIDTH="10%"
VALIGN="BOTTOM"
ALIGN="CENTER"
><ANNMARK
NAME="ch02lev1sec2"/></TD
></TR
><ANNOTATION
NAME="ch02lev1sec2"
TITLE="Compiling a KDE Program"/></TABLE
></DIV
><DIV
ALIGN="RIGHT"
CLASS="NAVBAR"
><P
><A
HREF="ch02.html"
>Prev</A
> <A
HREF="ch02lev1sec3.html"
>Next</A
> <A
HREF="index.html"
>Table of Contents</A
></P
></DIV
><HR
WIDTH="100%"
SIZE="2"
ALIGN="CENTER"
NOSHADE="NOSHADE"/></BODY
></HTML
>
|