|
Lines 118-130
Link Here
|
| 118 |
|
118 |
|
| 119 |
<para>To get one of these packages, all you need to do is to click on |
119 |
<para>To get one of these packages, all you need to do is to click on |
| 120 |
the hotlink for the package, then run |
120 |
the hotlink for the package, then run |
| 121 |
<screen>$ <userinput>pkg_add <replaceable>package name</></userinput></screen> |
|
|
| 122 |
</para> |
121 |
</para> |
|
|
122 |
<screen>&prompt.root; <userinput>pkg_add <replaceable>package name</></userinput></screen> |
| 123 |
|
123 |
|
| 124 |
<para>as root. Obviously, you will need to have a fully functional FreeBSD |
124 |
<para>as root. Obviously, you will need to have a fully functional FreeBSD |
| 125 |
2.1.0 or later system for the package to work!</para> |
125 |
2.1.0 or later system for the package to work!</para> |
| 126 |
|
126 |
|
| 127 |
<para> |
|
|
| 128 |
<variablelist> |
127 |
<variablelist> |
| 129 |
<varlistentry><term><acronym>BASIC</></term> |
128 |
<varlistentry><term><acronym>BASIC</></term> |
| 130 |
|
129 |
|
|
Lines 206-212
Link Here
|
| 206 |
</varlistentry> |
205 |
</varlistentry> |
| 207 |
|
206 |
|
| 208 |
</variablelist> |
207 |
</variablelist> |
| 209 |
</para> |
|
|
| 210 |
|
208 |
|
| 211 |
</sect2> |
209 |
</sect2> |
| 212 |
|
210 |
|
|
Lines 309-321
Link Here
|
| 309 |
<para>Fortunately, almost all this detail is hidden from you, as |
307 |
<para>Fortunately, almost all this detail is hidden from you, as |
| 310 |
<command>cc</> is a front end that manages calling all these programs |
308 |
<command>cc</> is a front end that manages calling all these programs |
| 311 |
with the right arguments for you; simply typing |
309 |
with the right arguments for you; simply typing |
| 312 |
<screen>$ <userinput>cc foobar.c</></screen></para> |
310 |
</para> |
|
|
311 |
<screen>&prompt.user; <userinput>cc foobar.c</></screen> |
| 313 |
|
312 |
|
| 314 |
<para>will cause <filename>foobar.c</> to be compiled by all the |
313 |
<para>will cause <filename>foobar.c</> to be compiled by all the |
| 315 |
steps above. If you have more than one file to compile, just do |
314 |
steps above. If you have more than one file to compile, just do |
| 316 |
something like |
315 |
something like |
| 317 |
<screen>$ <userinput>cc foo.c bar.c</></screen> |
|
|
| 318 |
</para> |
316 |
</para> |
|
|
317 |
<screen>&prompt.user; <userinput>cc foo.c bar.c</></screen> |
| 319 |
|
318 |
|
| 320 |
<para>Note that the syntax checking is just that—checking the |
319 |
<para>Note that the syntax checking is just that—checking the |
| 321 |
syntax. It will not check for any logical mistakes you may have made, |
320 |
syntax. It will not check for any logical mistakes you may have made, |
|
Lines 337-344
Link Here
|
| 337 |
the mists of history.</para></footnote></para> |
336 |
the mists of history.</para></footnote></para> |
| 338 |
|
337 |
|
| 339 |
<informalexample> |
338 |
<informalexample> |
| 340 |
<screen>$ <userinput>cc foobar.c</> <lineannotation>executable is <filename>a.out</></> |
339 |
<screen>&prompt.user; <userinput>cc foobar.c</> <lineannotation>executable is <filename>a.out</></> |
| 341 |
$ <userinput>cc -o foobar foobar.c</> <lineannotation>executable is <filename>foobar</></></screen> |
340 |
&prompt.user; <userinput>cc -o foobar foobar.c</> <lineannotation>executable is <filename>foobar</></></screen> |
| 342 |
</informalexample> |
341 |
</informalexample> |
| 343 |
</listitem> |
342 |
</listitem> |
| 344 |
</varlistentry> |
343 |
</varlistentry> |
|
Lines 349-355
Link Here
|
| 349 |
a <filename>Makefile</filename>.</para> |
348 |
a <filename>Makefile</filename>.</para> |
| 350 |
|
349 |
|
| 351 |
<informalexample> |
350 |
<informalexample> |
| 352 |
<screen>$ <userinput>cc -c foobar.c</userinput></screen> |
351 |
<screen>&prompt.user; <userinput>cc -c foobar.c</userinput></screen> |
| 353 |
</informalexample> |
352 |
</informalexample> |
| 354 |
|
353 |
|
| 355 |
<para>This will produce an <firstterm>object file</> (not an |
354 |
<para>This will produce an <firstterm>object file</> (not an |
|
Lines 373-379
Link Here
|
| 373 |
works properly.</para> |
372 |
works properly.</para> |
| 374 |
|
373 |
|
| 375 |
<informalexample> |
374 |
<informalexample> |
| 376 |
<screen>$ <userinput>cc -g foobar.c</userinput></screen> |
375 |
<screen>&prompt.user; <userinput>cc -g foobar.c</userinput></screen> |
| 377 |
</informalexample> |
376 |
</informalexample> |
| 378 |
|
377 |
|
| 379 |
<para>This will produce a debug version of the |
378 |
<para>This will produce a debug version of the |
|
Lines 401-407
Link Here
|
| 401 |
version.</para> |
400 |
version.</para> |
| 402 |
|
401 |
|
| 403 |
<informalexample> |
402 |
<informalexample> |
| 404 |
<screen>$ <userinput>cc -O -o foobar foobar.c</userinput></screen> |
403 |
<screen>&prompt.user; <userinput>cc -O -o foobar foobar.c</userinput></screen> |
| 405 |
</informalexample> |
404 |
</informalexample> |
| 406 |
|
405 |
|
| 407 |
<para>This will produce an optimised version of |
406 |
<para>This will produce an optimised version of |
|
Lines 461-467
Link Here
|
| 461 |
what you may be using in a few years time?</para> |
460 |
what you may be using in a few years time?</para> |
| 462 |
|
461 |
|
| 463 |
<informalexample> |
462 |
<informalexample> |
| 464 |
<screen>$ <userinput>cc -Wall -ansi -pedantic -o foobar foobar.c</userinput></screen> |
463 |
<screen>&prompt.user; <userinput>cc -Wall -ansi -pedantic -o foobar foobar.c</userinput></screen> |
| 465 |
</informalexample> |
464 |
</informalexample> |
| 466 |
|
465 |
|
| 467 |
<para>This will produce an executable <filename>foobar</filename> |
466 |
<para>This will produce an executable <filename>foobar</filename> |
|
Lines 490-496
Link Here
|
| 490 |
last library on the command line.</para> |
489 |
last library on the command line.</para> |
| 491 |
|
490 |
|
| 492 |
<informalexample> |
491 |
<informalexample> |
| 493 |
<screen>$ <userinput>cc -o foobar foobar.c -lm</userinput></screen> |
492 |
<screen>&prompt.user; <userinput>cc -o foobar foobar.c -lm</userinput></screen> |
| 494 |
</informalexample> |
493 |
</informalexample> |
| 495 |
|
494 |
|
| 496 |
<para>This will link the math library functions into |
495 |
<para>This will link the math library functions into |
|
Lines 505-513
Link Here
|
| 505 |
on FreeBSD.</para> |
504 |
on FreeBSD.</para> |
| 506 |
|
505 |
|
| 507 |
<informalexample> |
506 |
<informalexample> |
| 508 |
<screen>$ <userinput>cc -o foobar foobar.cc -lg++</userinput> <lineannotation>For FreeBSD 2.1.6 and earlier</> |
507 |
<screen>&prompt.user; <userinput>cc -o foobar foobar.cc -lg++</userinput> <lineannotation>For FreeBSD 2.1.6 and earlier</> |
| 509 |
$ <userinput>cc -o foobar foobar.cc -lstdc++</userinput> <lineannotation>For FreeBSD 2.2 and later</> |
508 |
&prompt.user; <userinput>cc -o foobar foobar.cc -lstdc++</userinput> <lineannotation>For FreeBSD 2.2 and later</> |
| 510 |
$ <userinput>c++ -o foobar foobar.cc</userinput></screen> |
509 |
&prompt.user; <userinput>c++ -o foobar foobar.cc</userinput></screen> |
| 511 |
</informalexample> |
510 |
</informalexample> |
| 512 |
|
511 |
|
| 513 |
<para>Each of these will both produce an executable |
512 |
<para>Each of these will both produce an executable |
|
Lines 529-551
Link Here
|
| 529 |
<sect2> |
528 |
<sect2> |
| 530 |
<title>Common <command>cc</command> Queries and Problems</title> |
529 |
<title>Common <command>cc</command> Queries and Problems</title> |
| 531 |
|
530 |
|
| 532 |
<para>Q. I am trying to write a program which uses the |
531 |
<qandaset> |
|
|
532 |
<qandaentry> |
| 533 |
<question> |
| 534 |
<para>I am trying to write a program which uses the |
| 533 |
<function>sin()</function> function and I get an error like this. |
535 |
<function>sin()</function> function and I get an error like this. |
| 534 |
What does it mean? |
536 |
What does it mean? |
|
|
537 |
</para> |
| 535 |
<informalexample> |
538 |
<informalexample> |
| 536 |
<screen>/var/tmp/cc0143941.o: Undefined symbol `_sin' referenced from text segment</screen> |
539 |
<screen>/var/tmp/cc0143941.o: Undefined symbol `_sin' referenced from text segment</screen> |
| 537 |
</informalexample> |
540 |
</informalexample> |
| 538 |
</para> |
541 |
</question> |
| 539 |
|
542 |
<answer> |
| 540 |
<para>A. When using mathematical functions like |
543 |
<para>When using mathematical functions like |
| 541 |
<function>sin()</function>, you have to tell <command>cc</command> to |
544 |
<function>sin()</function>, you have to tell <command>cc</command> to |
| 542 |
link in the math library, like so: |
545 |
link in the math library, like so:</para> |
| 543 |
<informalexample> |
546 |
<informalexample> |
| 544 |
<screen>$ <userinput>cc -o foobar foobar.c -lm</userinput></screen> |
547 |
<screen>&prompt.user; <userinput>cc -o foobar foobar.c -lm</userinput></screen> |
| 545 |
</informalexample></para> |
548 |
</informalexample> |
| 546 |
|
549 |
</answer> |
| 547 |
<para>Q. All right, I wrote this simple program to practice using |
550 |
</qandaentry> |
|
|
551 |
<qandaentry> |
| 552 |
<question> |
| 553 |
<para>All right, I wrote this simple program to practice using |
| 548 |
<option>-lm</option>. All it does is raise 2.1 to the power of 6. |
554 |
<option>-lm</option>. All it does is raise 2.1 to the power of 6. |
|
|
555 |
</para> |
| 549 |
<informalexample> |
556 |
<informalexample> |
| 550 |
<programlisting>#include <stdio.h> |
557 |
<programlisting>#include <stdio.h> |
| 551 |
|
558 |
|
|
Lines 557-587
Link Here
|
| 557 |
return 0; |
564 |
return 0; |
| 558 |
}</programlisting> |
565 |
}</programlisting> |
| 559 |
</informalexample> |
566 |
</informalexample> |
|
|
567 |
<para> |
| 560 |
and I compiled it as: |
568 |
and I compiled it as: |
|
|
569 |
</para> |
| 561 |
<informalexample> |
570 |
<informalexample> |
| 562 |
<screen>$ <userinput>cc temp.c -lm</userinput></screen> |
571 |
<screen>&prompt.user; <userinput>cc temp.c -lm</userinput></screen> |
| 563 |
</informalexample> |
572 |
</informalexample> |
|
|
573 |
<para> |
| 564 |
like you said I should, but I get this when I run it: |
574 |
like you said I should, but I get this when I run it: |
|
|
575 |
</para> |
| 565 |
<informalexample> |
576 |
<informalexample> |
| 566 |
<screen>$ <userinput>./a.out</userinput> |
577 |
<screen>&prompt.user; <userinput>./a.out</userinput> |
| 567 |
2.1 ^ 6 = 1023.000000</screen> |
578 |
2.1 ^ 6 = 1023.000000</screen> |
| 568 |
</informalexample> |
579 |
</informalexample> |
| 569 |
</para> |
|
|
| 570 |
|
580 |
|
| 571 |
<para>This is <emphasis>not</emphasis> the right answer! What is |
581 |
<para>This is <emphasis>not</emphasis> the right answer! What is |
| 572 |
going on?</para> |
582 |
going on?</para> |
| 573 |
|
583 |
</question> |
| 574 |
<para>A. When the compiler sees you call a function, it checks if it |
584 |
<answer> |
|
|
585 |
<para>When the compiler sees you call a function, it checks if it |
| 575 |
has already seen a prototype for it. If it has not, it assumes the |
586 |
has already seen a prototype for it. If it has not, it assumes the |
| 576 |
function returns an <type>int</type>, which is |
587 |
function returns an <type>int</type>, which is |
| 577 |
definitely not what you want here.</para> |
588 |
definitely not what you want here.</para> |
| 578 |
|
589 |
</answer> |
| 579 |
<para>Q. So how do I fix this?</para> |
590 |
</qandaentry> |
| 580 |
|
591 |
<qandaentry> |
| 581 |
<para>A. The prototypes for the mathematical functions are in |
592 |
<question> |
|
|
593 |
<para>So how do I fix this?</para> |
| 594 |
</question> |
| 595 |
<answer> |
| 596 |
<para>The prototypes for the mathematical functions are in |
| 582 |
<filename>math.h</filename>. If you include this file, the compiler |
597 |
<filename>math.h</filename>. If you include this file, the compiler |
| 583 |
will be able to find the prototype and it will stop doing strange |
598 |
will be able to find the prototype and it will stop doing strange |
| 584 |
things to your calculation! |
599 |
things to your calculation! |
|
|
600 |
</para> |
| 585 |
<informalexample> |
601 |
<informalexample> |
| 586 |
<programlisting>#include <math.h> |
602 |
<programlisting>#include <math.h> |
| 587 |
#include <stdio.h> |
603 |
#include <stdio.h> |
|
Lines 589-682
Link Here
|
| 589 |
int main() { |
605 |
int main() { |
| 590 |
...</programlisting> |
606 |
...</programlisting> |
| 591 |
</informalexample> |
607 |
</informalexample> |
| 592 |
</para> |
|
|
| 593 |
|
608 |
|
| 594 |
<para>After recompiling it as you did before, run it: |
609 |
<para>After recompiling it as you did before, run it: |
|
|
610 |
</para> |
| 595 |
<informalexample> |
611 |
<informalexample> |
| 596 |
<screen>$ <userinput>./a.out</userinput> |
612 |
<screen>&prompt.user; <userinput>./a.out</userinput> |
| 597 |
2.1 ^ 6 = 85.766121</screen> |
613 |
2.1 ^ 6 = 85.766121</screen> |
| 598 |
</informalexample> |
614 |
</informalexample> |
| 599 |
</para> |
|
|
| 600 |
|
615 |
|
| 601 |
<para>If you are using any of the mathematical functions, |
616 |
<para>If you are using any of the mathematical functions, |
| 602 |
<emphasis>always</emphasis> include <filename>math.h</filename> and |
617 |
<emphasis>always</emphasis> include <filename>math.h</filename> and |
| 603 |
remember to link in the math library.</para> |
618 |
remember to link in the math library.</para> |
| 604 |
|
619 |
</answer> |
| 605 |
<para>Q. I compiled a file called <filename>foobar.c</filename> and I |
620 |
</qandaentry> |
|
|
621 |
<qandaentry> |
| 622 |
<question> |
| 623 |
<para>I compiled a file called <filename>foobar.c</filename> and I |
| 606 |
cannot find an executable called <filename>foobar</filename>. Where's |
624 |
cannot find an executable called <filename>foobar</filename>. Where's |
| 607 |
it gone?</para> |
625 |
it gone?</para> |
| 608 |
|
626 |
</question> |
| 609 |
<para>A. Remember, <command>cc</command> will call the executable |
627 |
<answer> |
|
|
628 |
<para>Remember, <command>cc</command> will call the executable |
| 610 |
<filename>a.out</filename> unless you tell it differently. Use the |
629 |
<filename>a.out</filename> unless you tell it differently. Use the |
| 611 |
<option>-o <replaceable>filename</replaceable></option> option: |
630 |
<option>-o <replaceable>filename</replaceable></option> option: |
|
|
631 |
</para> |
| 612 |
<informalexample> |
632 |
<informalexample> |
| 613 |
<screen>$ <userinput>cc -o foobar foobar.c</userinput></screen> |
633 |
<screen>&prompt.user; <userinput>cc -o foobar foobar.c</userinput></screen> |
| 614 |
</informalexample> |
634 |
</informalexample> |
| 615 |
</para> |
635 |
</answer> |
| 616 |
|
636 |
</qandaentry> |
| 617 |
<para>Q. OK, I have an executable called <filename>foobar</filename>, |
637 |
<qandaentry> |
|
|
638 |
<question> |
| 639 |
<para>OK, I have an executable called <filename>foobar</filename>, |
| 618 |
I can see it when I run <command>ls</command>, but when I type in |
640 |
I can see it when I run <command>ls</command>, but when I type in |
| 619 |
<command>foobar</command> at the command prompt it tells me there is |
641 |
<command>foobar</command> at the command prompt it tells me there is |
| 620 |
no such file. Why can it not find it?</para> |
642 |
no such file. Why can it not find it?</para> |
| 621 |
|
643 |
</question> |
| 622 |
<para>A. Unlike <trademark>MS-DOS</trademark>, Unix does not look in the |
644 |
<answer> |
|
|
645 |
<para>Unlike <trademark>MS-DOS</trademark>, Unix does not look in the |
| 623 |
current directory when it is trying to find out which executable you |
646 |
current directory when it is trying to find out which executable you |
| 624 |
want it to run, unless you tell it to. Either type |
647 |
want it to run, unless you tell it to. Either type |
| 625 |
<command>./foobar</command>, which means <quote>run the file called |
648 |
<command>./foobar</command>, which means <quote>run the file called |
| 626 |
<filename>foobar</filename> in the current directory</quote>, or |
649 |
<filename>foobar</filename> in the current directory</quote>, or |
| 627 |
change your <systemitem class=environvar>PATH</systemitem> |
650 |
change your <systemitem class=environvar>PATH</systemitem> |
| 628 |
environment variable so that it looks something like |
651 |
environment variable so that it looks something like |
|
|
652 |
</para> |
| 629 |
<informalexample> |
653 |
<informalexample> |
| 630 |
<screen>bin:/usr/bin:/usr/local/bin:.</screen> |
654 |
<screen>bin:/usr/bin:/usr/local/bin:.</screen> |
| 631 |
</informalexample> |
655 |
</informalexample> |
|
|
656 |
<para> |
| 632 |
The dot at the end means <quote>look in the current directory if it is not in |
657 |
The dot at the end means <quote>look in the current directory if it is not in |
| 633 |
any of the others</quote>.</para> |
658 |
any of the others</quote>.</para> |
| 634 |
|
659 |
</answer> |
| 635 |
<para>Q. I called my executable <filename>test</filename>, but |
660 |
</qandaentry> |
|
|
661 |
<qandaentry> |
| 662 |
<question> |
| 663 |
<para>I called my executable <filename>test</filename>, but |
| 636 |
nothing happens when I run it. What is going on?</para> |
664 |
nothing happens when I run it. What is going on?</para> |
| 637 |
|
665 |
</question> |
| 638 |
<para>A. Most Unix systems have a program called |
666 |
<answer> |
|
|
667 |
<para>Most Unix systems have a program called |
| 639 |
<command>test</command> in <filename>/usr/bin</filename> and the |
668 |
<command>test</command> in <filename>/usr/bin</filename> and the |
| 640 |
shell is picking that one up before it gets to checking the current |
669 |
shell is picking that one up before it gets to checking the current |
| 641 |
directory. Either type: |
670 |
directory. Either type: |
|
|
671 |
</para> |
| 642 |
<informalexample> |
672 |
<informalexample> |
| 643 |
<screen>$ <userinput>./test</userinput></screen> |
673 |
<screen>&prompt.user; <userinput>./test</userinput></screen> |
| 644 |
</informalexample> |
674 |
</informalexample> |
|
|
675 |
<para> |
| 645 |
or choose a better name for your program!</para> |
676 |
or choose a better name for your program!</para> |
| 646 |
|
677 |
</answer> |
| 647 |
<para>Q. I compiled my program and it seemed to run all right at |
678 |
</qandaentry> |
|
|
679 |
<qandaentry> |
| 680 |
<question> |
| 681 |
<para>I compiled my program and it seemed to run all right at |
| 648 |
first, then there was an error and it said something about <errorname>core |
682 |
first, then there was an error and it said something about <errorname>core |
| 649 |
dumped</errorname>. What does that mean?</para> |
683 |
dumped</errorname>. What does that mean?</para> |
| 650 |
|
684 |
</question> |
| 651 |
<para>A. The name <firstterm>core dump</firstterm> dates back to the |
685 |
<answer> |
|
|
686 |
<para>The name <firstterm>core dump</firstterm> dates back to the |
| 652 |
very early days of Unix, when the machines used core memory for |
687 |
very early days of Unix, when the machines used core memory for |
| 653 |
storing data. Basically, if the program failed under certain |
688 |
storing data. Basically, if the program failed under certain |
| 654 |
conditions, the system would write the contents of core memory to |
689 |
conditions, the system would write the contents of core memory to |
| 655 |
disk in a file called <filename>core</filename>, which the programmer |
690 |
disk in a file called <filename>core</filename>, which the programmer |
| 656 |
could then pore over to find out what went wrong.</para> |
691 |
could then pore over to find out what went wrong.</para> |
| 657 |
|
692 |
</answer> |
| 658 |
<para>Q. Fascinating stuff, but what I am supposed to do now?</para> |
693 |
</qandaentry> |
| 659 |
|
694 |
<qandaentry> |
| 660 |
<para>A. Use <command>gdb</command> to analyse the core (see <xref |
695 |
<question> |
|
|
696 |
<para>Fascinating stuff, but what I am supposed to do now?</para> |
| 697 |
</question> |
| 698 |
<answer> |
| 699 |
<para>Use <command>gdb</command> to analyse the core (see <xref |
| 661 |
linkend="debugging">).</para> |
700 |
linkend="debugging">).</para> |
| 662 |
|
701 |
</answer> |
| 663 |
<para>Q. When my program dumped core, it said something about a |
702 |
</qandaentry> |
|
|
703 |
<qandaentry> |
| 704 |
<question> |
| 705 |
<para>When my program dumped core, it said something about a |
| 664 |
<errorname>segmentation fault</errorname>. What's that?</para> |
706 |
<errorname>segmentation fault</errorname>. What's that?</para> |
| 665 |
|
707 |
</question> |
| 666 |
<para>A. This basically means that your program tried to perform some sort |
708 |
<answer> |
|
|
709 |
<para>This basically means that your program tried to perform some sort |
| 667 |
of illegal operation on memory; Unix is designed to protect the |
710 |
of illegal operation on memory; Unix is designed to protect the |
| 668 |
operating system and other programs from rogue programs.</para> |
711 |
operating system and other programs from rogue programs.</para> |
| 669 |
|
712 |
|
| 670 |
<para>Common causes for this are: |
713 |
<para>Common causes for this are: |
|
|
714 |
</para> |
| 671 |
<itemizedlist> |
715 |
<itemizedlist> |
| 672 |
<listitem><para>Trying to write to a <symbol>NULL</symbol> pointer, eg |
716 |
<listitem><para>Trying to write to a <symbol>NULL</symbol> pointer, eg |
|
|
717 |
</para> |
| 673 |
<programlisting>char *foo = NULL; |
718 |
<programlisting>char *foo = NULL; |
| 674 |
strcpy(foo, "bang!");</programlisting> |
719 |
strcpy(foo, "bang!");</programlisting> |
| 675 |
</para></listitem> |
720 |
</listitem> |
| 676 |
|
721 |
|
| 677 |
<listitem><para>Using a pointer that hasn't been initialised, eg |
722 |
<listitem><para>Using a pointer that hasn't been initialised, eg |
|
|
723 |
</para> |
| 678 |
<programlisting>char *foo; |
724 |
<programlisting>char *foo; |
| 679 |
strcpy(foo, "bang!");</programlisting> |
725 |
strcpy(foo, "bang!");</programlisting> |
|
|
726 |
<para> |
| 680 |
The pointer will have some random value that, with luck, |
727 |
The pointer will have some random value that, with luck, |
| 681 |
will point into an area of memory that isn't available to |
728 |
will point into an area of memory that isn't available to |
| 682 |
your program and the kernel will kill your program before |
729 |
your program and the kernel will kill your program before |
|
Lines 686-736
Link Here
|
| 686 |
mysteriously.</para></listitem> |
733 |
mysteriously.</para></listitem> |
| 687 |
|
734 |
|
| 688 |
<listitem><para>Trying to access past the end of an array, eg |
735 |
<listitem><para>Trying to access past the end of an array, eg |
|
|
736 |
</para> |
| 689 |
<programlisting>int bar[20]; |
737 |
<programlisting>int bar[20]; |
| 690 |
bar[27] = 6;</programlisting></para></listitem> |
738 |
bar[27] = 6;</programlisting></listitem> |
| 691 |
|
739 |
|
| 692 |
<listitem><para> Trying to store something in read-only memory, eg |
740 |
<listitem><para> Trying to store something in read-only memory, eg |
|
|
741 |
</para> |
| 693 |
<programlisting>char *foo = "My string"; |
742 |
<programlisting>char *foo = "My string"; |
| 694 |
strcpy(foo, "bang!");</programlisting> |
743 |
strcpy(foo, "bang!");</programlisting> |
|
|
744 |
<para> |
| 695 |
Unix compilers often put string literals like |
745 |
Unix compilers often put string literals like |
| 696 |
<literal>"My string"</literal> into |
746 |
<literal>"My string"</literal> into |
| 697 |
read-only areas of memory.</para></listitem> |
747 |
read-only areas of memory.</para></listitem> |
| 698 |
|
748 |
|
| 699 |
<listitem><para>Doing naughty things with |
749 |
<listitem><para>Doing naughty things with |
| 700 |
<function>malloc()</function> and <function>free()</function>, eg |
750 |
<function>malloc()</function> and <function>free()</function>, eg |
|
|
751 |
</para> |
| 701 |
<programlisting>char bar[80]; |
752 |
<programlisting>char bar[80]; |
| 702 |
free(bar);</programlisting> |
753 |
free(bar);</programlisting> |
|
|
754 |
<para> |
| 703 |
or |
755 |
or |
|
|
756 |
</para> |
| 704 |
<programlisting>char *foo = malloc(27); |
757 |
<programlisting>char *foo = malloc(27); |
| 705 |
free(foo); |
758 |
free(foo); |
| 706 |
free(foo);</programlisting> |
759 |
free(foo);</programlisting> |
| 707 |
</para></listitem> |
760 |
</listitem> |
| 708 |
|
761 |
|
| 709 |
</itemizedlist></para> |
762 |
</itemizedlist> |
| 710 |
|
763 |
|
| 711 |
<para>Making one of these mistakes will not always lead to an |
764 |
<para>Making one of these mistakes will not always lead to an |
| 712 |
error, but they are always bad practice. Some systems and |
765 |
error, but they are always bad practice. Some systems and |
| 713 |
compilers are more tolerant than others, which is why programs |
766 |
compilers are more tolerant than others, which is why programs |
| 714 |
that ran well on one system can crash when you try them on an |
767 |
that ran well on one system can crash when you try them on an |
| 715 |
another.</para> |
768 |
another.</para> |
| 716 |
|
769 |
</answer> |
| 717 |
<para>Q. Sometimes when I get a core dump it says <errorname>bus |
770 |
</qandaentry> |
|
|
771 |
<qandaentry> |
| 772 |
<question> |
| 773 |
<para>Sometimes when I get a core dump it says <errorname>bus |
| 718 |
error</errorname>. It says in my Unix book that this means a hardware |
774 |
error</errorname>. It says in my Unix book that this means a hardware |
| 719 |
problem, but the computer still seems to be working. Is this |
775 |
problem, but the computer still seems to be working. Is this |
| 720 |
true?</para> |
776 |
true?</para> |
| 721 |
|
777 |
</question> |
| 722 |
<para>A. No, fortunately not (unless of course you really do have a hardware |
778 |
<answer> |
|
|
779 |
<para>No, fortunately not (unless of course you really do have a hardware |
| 723 |
problem…). This is usually another way of saying that you |
780 |
problem…). This is usually another way of saying that you |
| 724 |
accessed memory in a way you shouldn't have.</para> |
781 |
accessed memory in a way you shouldn't have.</para> |
| 725 |
|
782 |
</answer> |
| 726 |
<para>Q. This dumping core business sounds as though it could be quite |
783 |
</qandaentry> |
|
|
784 |
<qandaentry> |
| 785 |
<question> |
| 786 |
<para>This dumping core business sounds as though it could be quite |
| 727 |
useful, if I can make it happen when I want to. Can I do this, or |
787 |
useful, if I can make it happen when I want to. Can I do this, or |
| 728 |
do I have to wait until there's an error?</para> |
788 |
do I have to wait until there's an error?</para> |
| 729 |
|
789 |
</question> |
| 730 |
<para>A. Yes, just go to another console or xterm, do |
790 |
<answer> |
| 731 |
<screen>$ <userinput>ps</userinput></screen> |
791 |
<para>Yes, just go to another console or xterm, do</para> |
|
|
792 |
<screen>&prompt.user; <userinput>ps</userinput></screen> |
| 793 |
<para> |
| 732 |
to find out the process ID of your program, and do |
794 |
to find out the process ID of your program, and do |
| 733 |
<screen>$ <userinput>kill -ABRT <replaceable>pid</replaceable></userinput></screen> |
795 |
</para> |
|
|
796 |
<screen>&prompt.user; <userinput>kill -ABRT <replaceable>pid</replaceable></userinput></screen> |
| 797 |
<para> |
| 734 |
where <parameter><replaceable>pid</replaceable></parameter> is the |
798 |
where <parameter><replaceable>pid</replaceable></parameter> is the |
| 735 |
process ID you looked up.</para> |
799 |
process ID you looked up.</para> |
| 736 |
|
800 |
|
|
Lines 738-743
Link Here
|
| 738 |
loop, for instance. If your program happens to trap |
802 |
loop, for instance. If your program happens to trap |
| 739 |
<symbol>SIGABRT</symbol>, there are several other signals which have |
803 |
<symbol>SIGABRT</symbol>, there are several other signals which have |
| 740 |
a similar effect.</para> |
804 |
a similar effect.</para> |
|
|
805 |
</answer> |
| 806 |
</qandaentry> |
| 807 |
</qandaset> |
| 741 |
|
808 |
|
| 742 |
</sect2> |
809 |
</sect2> |
| 743 |
</sect1> |
810 |
</sect1> |
|
Lines 751-764
Link Here
|
| 751 |
|
818 |
|
| 752 |
<para>When you're working on a simple program with only one or two source |
819 |
<para>When you're working on a simple program with only one or two source |
| 753 |
files, typing in |
820 |
files, typing in |
| 754 |
<screen>$ <userinput>cc file1.c file2.c</userinput></screen> |
821 |
</para> |
|
|
822 |
<screen>&prompt.user; <userinput>cc file1.c file2.c</userinput></screen> |
| 823 |
<para> |
| 755 |
is not too bad, but it quickly becomes very tedious when there are |
824 |
is not too bad, but it quickly becomes very tedious when there are |
| 756 |
several files—and it can take a while to compile, too.</para> |
825 |
several files—and it can take a while to compile, too.</para> |
| 757 |
|
826 |
|
| 758 |
<para>One way to get around this is to use object files and only recompile |
827 |
<para>One way to get around this is to use object files and only recompile |
| 759 |
the source file if the source code has changed. So we could have |
828 |
the source file if the source code has changed. So we could have |
| 760 |
something like: |
829 |
something like: |
| 761 |
<screen>$ <userinput>cc file1.o file2.o</userinput> … <userinput>file37.c</userinput> &hellip</screen> |
830 |
</para> |
|
|
831 |
<screen>&prompt.user; <userinput>cc file1.o file2.o</userinput> … <userinput>file37.c</userinput> &hellip</screen> |
| 832 |
<para> |
| 762 |
if we'd changed <filename>file37.c</filename>, but not any of the |
833 |
if we'd changed <filename>file37.c</filename>, but not any of the |
| 763 |
others, since the last time we compiled. This may speed up the |
834 |
others, since the last time we compiled. This may speed up the |
| 764 |
compilation quite a bit, but doesn't solve the typing |
835 |
compilation quite a bit, but doesn't solve the typing |
|
Lines 805-812
Link Here
|
| 805 |
<title>Example of using <command>make</command></title> |
876 |
<title>Example of using <command>make</command></title> |
| 806 |
|
877 |
|
| 807 |
<para>Here's a very simple make file: |
878 |
<para>Here's a very simple make file: |
|
|
879 |
</para> |
| 808 |
<programlisting>foo: foo.c |
880 |
<programlisting>foo: foo.c |
| 809 |
cc -o foo foo.c</programlisting> |
881 |
cc -o foo foo.c</programlisting> |
|
|
882 |
<para> |
| 810 |
It consists of two lines, a dependency line and a creation line.</para> |
883 |
It consists of two lines, a dependency line and a creation line.</para> |
| 811 |
|
884 |
|
| 812 |
<para>The dependency line here consists of the name of the program |
885 |
<para>The dependency line here consists of the name of the program |
|
Lines 840-853
Link Here
|
| 840 |
<para>Another useful property of makefiles is that the targets don't have |
913 |
<para>Another useful property of makefiles is that the targets don't have |
| 841 |
to be programs. For instance, we could have a make file that looks |
914 |
to be programs. For instance, we could have a make file that looks |
| 842 |
like this: |
915 |
like this: |
|
|
916 |
</para> |
| 843 |
<programlisting>foo: foo.c |
917 |
<programlisting>foo: foo.c |
| 844 |
cc -o foo foo.c |
918 |
cc -o foo foo.c |
| 845 |
|
919 |
|
| 846 |
install: |
920 |
install: |
| 847 |
cp foo /home/me</programlisting></para> |
921 |
cp foo /home/me</programlisting> |
| 848 |
|
922 |
|
| 849 |
<para>We can tell make which target we want to make by typing: |
923 |
<para>We can tell make which target we want to make by typing: |
| 850 |
<screen>$ <userinput>make <replaceable>target</replaceable></userinput></screen> |
924 |
</para> |
|
|
925 |
<screen>&prompt.user; <userinput>make <replaceable>target</replaceable></userinput></screen> |
| 926 |
<para> |
| 851 |
<command>make</command> will then only look at that target and ignore any |
927 |
<command>make</command> will then only look at that target and ignore any |
| 852 |
others. For example, if we type <userinput>make foo</userinput> with the |
928 |
others. For example, if we type <userinput>make foo</userinput> with the |
| 853 |
makefile above, make will ignore the <action>install</action> target.</para> |
929 |
makefile above, make will ignore the <action>install</action> target.</para> |
|
Lines 886-896
Link Here
|
| 886 |
BSD-based systems like FreeBSD come with some very powerful ones as |
962 |
BSD-based systems like FreeBSD come with some very powerful ones as |
| 887 |
part of the system. One very good example of this is the FreeBSD |
963 |
part of the system. One very good example of this is the FreeBSD |
| 888 |
ports system. Here's the essential part of a typical ports |
964 |
ports system. Here's the essential part of a typical ports |
| 889 |
<filename>Makefile</filename>: |
965 |
<filename>Makefile</filename>:</para> |
| 890 |
<programlisting>MASTER_SITES= ftp://freefall.cdrom.com/pub/FreeBSD/LOCAL_PORTS/ |
966 |
<programlisting>MASTER_SITES= ftp://freefall.cdrom.com/pub/FreeBSD/LOCAL_PORTS/ |
| 891 |
DISTFILES= scheme-microcode+dist-7.3-freebsd.tgz |
967 |
DISTFILES= scheme-microcode+dist-7.3-freebsd.tgz |
| 892 |
|
968 |
|
| 893 |
.include <bsd.port.mk></programlisting></para> |
969 |
.include <bsd.port.mk></programlisting> |
| 894 |
|
970 |
|
| 895 |
<para>Now, if we go to the directory for this port and type |
971 |
<para>Now, if we go to the directory for this port and type |
| 896 |
<userinput>make</userinput>, the following happens:</para> |
972 |
<userinput>make</userinput>, the following happens:</para> |
|
Lines 965-971
Link Here
|
| 965 |
<para>The version of make that comes with FreeBSD is the <application>Berkeley |
1041 |
<para>The version of make that comes with FreeBSD is the <application>Berkeley |
| 966 |
make</application>; there is a tutorial for it in |
1042 |
make</application>; there is a tutorial for it in |
| 967 |
<filename>/usr/share/doc/psd/12.make</filename>. To view it, do |
1043 |
<filename>/usr/share/doc/psd/12.make</filename>. To view it, do |
| 968 |
<screen>$ <userinput>zmore paper.ascii.gz</userinput></screen> |
1044 |
</para> |
|
|
1045 |
<screen>&prompt.user; <userinput>zmore paper.ascii.gz</userinput></screen> |
| 1046 |
<para> |
| 969 |
in that directory.</para> |
1047 |
in that directory.</para> |
| 970 |
|
1048 |
|
| 971 |
<para>Many applications in the ports use <application>GNU |
1049 |
<para>Many applications in the ports use <application>GNU |
|
Lines 979-985
Link Here
|
| 979 |
you will have to edit the <filename>dir</filename> file in the |
1057 |
you will have to edit the <filename>dir</filename> file in the |
| 980 |
<filename>/usr/local/info</filename> directory to add an entry for |
1058 |
<filename>/usr/local/info</filename> directory to add an entry for |
| 981 |
it. This involves adding a line like |
1059 |
it. This involves adding a line like |
|
|
1060 |
</para> |
| 982 |
<programlisting> * Make: (make). The GNU Make utility.</programlisting> |
1061 |
<programlisting> * Make: (make). The GNU Make utility.</programlisting> |
|
|
1062 |
<para> |
| 983 |
to the file. Once you have done this, you can type |
1063 |
to the file. Once you have done this, you can type |
| 984 |
<userinput>info</userinput> and then select |
1064 |
<userinput>info</userinput> and then select |
| 985 |
<guimenuitem>make</guimenuitem> from the menu (or in |
1065 |
<guimenuitem>make</guimenuitem> from the menu (or in |
|
Lines 998-1007
Link Here
|
| 998 |
<para>The debugger that comes with FreeBSD is called |
1078 |
<para>The debugger that comes with FreeBSD is called |
| 999 |
<command>gdb</command> (<application>GNU |
1079 |
<command>gdb</command> (<application>GNU |
| 1000 |
debugger</application>). You start it up by typing |
1080 |
debugger</application>). You start it up by typing |
| 1001 |
<screen>$ <userinput>gdb <replaceable>progname</replaceable></userinput></screen> |
1081 |
</para> |
|
|
1082 |
<screen>&prompt.user; <userinput>gdb <replaceable>progname</replaceable></userinput></screen> |
| 1083 |
<para> |
| 1002 |
although most people prefer to run it inside |
1084 |
although most people prefer to run it inside |
| 1003 |
<application>Emacs</application>. You can do this by: |
1085 |
<application>Emacs</application>. You can do this by: |
| 1004 |
<screen><userinput>M-x gdb RET <replaceable>progname</replaceable> RET</userinput></screen></para> |
1086 |
</para> |
|
|
1087 |
<screen><userinput>M-x gdb RET <replaceable>progname</replaceable> RET</userinput></screen> |
| 1005 |
|
1088 |
|
| 1006 |
<para>Using a debugger allows you to run the program under more |
1089 |
<para>Using a debugger allows you to run the program under more |
| 1007 |
controlled circumstances. Typically, you can step through the program |
1090 |
controlled circumstances. Typically, you can step through the program |
|
Lines 1035-1041
Link Here
|
| 1035 |
<command>gdb</command>. It will work without, but you'll only see the |
1118 |
<command>gdb</command>. It will work without, but you'll only see the |
| 1036 |
name of the function you're in, instead of the source code. If you |
1119 |
name of the function you're in, instead of the source code. If you |
| 1037 |
see a line like: |
1120 |
see a line like: |
| 1038 |
<screen>… (no debugging symbols found) …</screen>when |
1121 |
</para> |
|
|
1122 |
<screen>… (no debugging symbols found) …</screen><para>when |
| 1039 |
<command>gdb</command> starts up, you'll know that the program wasn't |
1123 |
<command>gdb</command> starts up, you'll know that the program wasn't |
| 1040 |
compiled with the <option>-g</option> option.</para> |
1124 |
compiled with the <option>-g</option> option.</para> |
| 1041 |
|
1125 |
|
|
Lines 1058-1063
Link Here
|
| 1058 |
<para>Here's a simple example of how to spot a mistake in a program |
1142 |
<para>Here's a simple example of how to spot a mistake in a program |
| 1059 |
with <command>gdb</command>. This is our program (with a deliberate |
1143 |
with <command>gdb</command>. This is our program (with a deliberate |
| 1060 |
mistake): |
1144 |
mistake): |
|
|
1145 |
</para> |
| 1061 |
<programlisting>#include <stdio.h> |
1146 |
<programlisting>#include <stdio.h> |
| 1062 |
|
1147 |
|
| 1063 |
int bazz(int anint); |
1148 |
int bazz(int anint); |
|
Lines 1074-1093
Link Here
|
| 1074 |
printf("You gave me %d\n", anint); |
1159 |
printf("You gave me %d\n", anint); |
| 1075 |
return anint; |
1160 |
return anint; |
| 1076 |
}</programlisting> |
1161 |
}</programlisting> |
| 1077 |
</para> |
|
|
| 1078 |
|
1162 |
|
| 1079 |
<para>This program sets <symbol>i</symbol> to be <literal>5</literal> |
1163 |
<para>This program sets <symbol>i</symbol> to be <literal>5</literal> |
| 1080 |
and passes it to a function <function>bazz()</function> which prints |
1164 |
and passes it to a function <function>bazz()</function> which prints |
| 1081 |
out the number we gave it.</para> |
1165 |
out the number we gave it.</para> |
| 1082 |
|
1166 |
|
| 1083 |
<para>When we compile and run the program we get |
1167 |
<para>When we compile and run the program we get |
| 1084 |
<screen>$ <userinput>cc -g -o temp temp.c</userinput> |
1168 |
</para> |
| 1085 |
$ <userinput>./temp</userinput> |
1169 |
<screen>&prompt.user; <userinput>cc -g -o temp temp.c</userinput> |
|
|
1170 |
&prompt.user; <userinput>./temp</userinput> |
| 1086 |
This is my program |
1171 |
This is my program |
| 1087 |
anint = 4231</screen></para> |
1172 |
anint = 4231</screen> |
| 1088 |
|
1173 |
|
| 1089 |
<para>That wasn't what we expected! Time to see what's going |
1174 |
<para>That wasn't what we expected! Time to see what's going |
| 1090 |
on!<screen>$ <userinput>gdb temp</userinput> |
1175 |
on!</para><screen>&prompt.user; <userinput>gdb temp</userinput> |
| 1091 |
GDB is free software and you are welcome to distribute copies of it |
1176 |
GDB is free software and you are welcome to distribute copies of it |
| 1092 |
under certain conditions; type "show copying" to see the conditions. |
1177 |
under certain conditions; type "show copying" to see the conditions. |
| 1093 |
There is absolutely no warranty for GDB; type "show warranty" for details. |
1178 |
There is absolutely no warranty for GDB; type "show warranty" for details. |
|
Lines 1102-1108
Link Here
|
| 1102 |
This is my program <lineannotation>Program prints out</> |
1187 |
This is my program <lineannotation>Program prints out</> |
| 1103 |
(gdb) <userinput>s</> <lineannotation>step into <function>bazz()</></> |
1188 |
(gdb) <userinput>s</> <lineannotation>step into <function>bazz()</></> |
| 1104 |
bazz (anint=4231) at temp.c:17 <lineannotation><command>gdb</command> displays stack frame</> |
1189 |
bazz (anint=4231) at temp.c:17 <lineannotation><command>gdb</command> displays stack frame</> |
| 1105 |
(gdb)</screen></para> |
1190 |
(gdb)</screen> |
| 1106 |
|
1191 |
|
| 1107 |
|
1192 |
|
| 1108 |
<para>Hang on a minute! How did <symbol>anint</symbol> get to be |
1193 |
<para>Hang on a minute! How did <symbol>anint</symbol> get to be |
|
Lines 1110-1121
Link Here
|
| 1110 |
in <function>main()</function>? Let's move up to |
1195 |
in <function>main()</function>? Let's move up to |
| 1111 |
<function>main()</function> and have a look.</para> |
1196 |
<function>main()</function> and have a look.</para> |
| 1112 |
|
1197 |
|
| 1113 |
<para><screen>(gdb) <userinput>up</> <lineannotation>Move up call stack</> |
1198 |
<screen>(gdb) <userinput>up</> <lineannotation>Move up call stack</> |
| 1114 |
#1 0x1625 in main () at temp.c:11 <lineannotation><command>gdb</command> displays stack frame</> |
1199 |
#1 0x1625 in main () at temp.c:11 <lineannotation><command>gdb</command> displays stack frame</> |
| 1115 |
(gdb) <userinput>p i</> <lineannotation>Show us the value of <symbol>i</></> |
1200 |
(gdb) <userinput>p i</> <lineannotation>Show us the value of <symbol>i</></> |
| 1116 |
$1 = 4231 <lineannotation><command>gdb</command> displays <literal>4231</></></screen> |
1201 |
$1 = 4231 <lineannotation><command>gdb</command> displays <literal>4231</></> |
|
|
1202 |
</screen> |
| 1203 |
<para> |
| 1117 |
Oh dear! Looking at the code, we forgot to initialise |
1204 |
Oh dear! Looking at the code, we forgot to initialise |
| 1118 |
<symbol>i</symbol>. We meant to put |
1205 |
<symbol>i</symbol>. We meant to put |
|
|
1206 |
</para> |
| 1119 |
<programlisting><lineannotation>…</> |
1207 |
<programlisting><lineannotation>…</> |
| 1120 |
main() { |
1208 |
main() { |
| 1121 |
int i; |
1209 |
int i; |
|
Lines 1123-1128
Link Here
|
| 1123 |
i = 5; |
1211 |
i = 5; |
| 1124 |
printf("This is my program\n"); |
1212 |
printf("This is my program\n"); |
| 1125 |
<lineannotation>&hellip</></programlisting> |
1213 |
<lineannotation>&hellip</></programlisting> |
|
|
1214 |
<para> |
| 1126 |
but we left the <literal>i=5;</literal> line out. As we didn't |
1215 |
but we left the <literal>i=5;</literal> line out. As we didn't |
| 1127 |
initialise <symbol>i</symbol>, it had whatever number happened to be |
1216 |
initialise <symbol>i</symbol>, it had whatever number happened to be |
| 1128 |
in that area of memory when the program ran, which in this case |
1217 |
in that area of memory when the program ran, which in this case |
|
Lines 1154-1165
Link Here
|
| 1154 |
<para>To examine a core file, start up <command>gdb</command> in the |
1243 |
<para>To examine a core file, start up <command>gdb</command> in the |
| 1155 |
usual way. Instead of typing <command>break</command> or |
1244 |
usual way. Instead of typing <command>break</command> or |
| 1156 |
<command>run</command>, type |
1245 |
<command>run</command>, type |
|
|
1246 |
</para> |
| 1157 |
<screen>(gdb) <userinput>core <replaceable>progname</replaceable>.core</userinput></screen> |
1247 |
<screen>(gdb) <userinput>core <replaceable>progname</replaceable>.core</userinput></screen> |
|
|
1248 |
<para> |
| 1158 |
If you're not in the same directory as the core file, you'll have to |
1249 |
If you're not in the same directory as the core file, you'll have to |
| 1159 |
do <userinput>dir /path/to/core/file</userinput> first.</para> |
1250 |
do <userinput>dir /path/to/core/file</userinput> first.</para> |
| 1160 |
|
1251 |
|
| 1161 |
<para>You should see something like this: |
1252 |
<para>You should see something like this: |
| 1162 |
<screen>$ <userinput>gdb a.out</userinput> |
1253 |
</para> |
|
|
1254 |
<screen>&prompt.user; <userinput>gdb a.out</userinput> |
| 1163 |
GDB is free software and you are welcome to distribute copies of it |
1255 |
GDB is free software and you are welcome to distribute copies of it |
| 1164 |
under certain conditions; type "show copying" to see the conditions. |
1256 |
under certain conditions; type "show copying" to see the conditions. |
| 1165 |
There is absolutely no warranty for GDB; type "show warranty" for details. |
1257 |
There is absolutely no warranty for GDB; type "show warranty" for details. |
|
Lines 1169-1175
Link Here
|
| 1169 |
Program terminated with signal 11, Segmentation fault. |
1261 |
Program terminated with signal 11, Segmentation fault. |
| 1170 |
Cannot access memory at address 0x7020796d. |
1262 |
Cannot access memory at address 0x7020796d. |
| 1171 |
#0 0x164a in bazz (anint=0x5) at temp.c:17 |
1263 |
#0 0x164a in bazz (anint=0x5) at temp.c:17 |
| 1172 |
(gdb)</screen></para> |
1264 |
(gdb)</screen> |
| 1173 |
|
1265 |
|
| 1174 |
<para>In this case, the program was called |
1266 |
<para>In this case, the program was called |
| 1175 |
<filename>a.out</filename>, so the core file is called |
1267 |
<filename>a.out</filename>, so the core file is called |
|
Lines 1182-1192
Link Here
|
| 1182 |
stack in a complex program. The <command>bt</command> command causes |
1274 |
stack in a complex program. The <command>bt</command> command causes |
| 1183 |
<command>gdb</command> to print out a back-trace of the call |
1275 |
<command>gdb</command> to print out a back-trace of the call |
| 1184 |
stack: |
1276 |
stack: |
|
|
1277 |
</para> |
| 1185 |
<screen>(gdb) <userinput>bt</userinput> |
1278 |
<screen>(gdb) <userinput>bt</userinput> |
| 1186 |
#0 0x164a in bazz (anint=0x5) at temp.c:17 |
1279 |
#0 0x164a in bazz (anint=0x5) at temp.c:17 |
| 1187 |
#1 0xefbfd888 in end () |
1280 |
#1 0xefbfd888 in end () |
| 1188 |
#2 0x162c in main () at temp.c:11 |
1281 |
#2 0x162c in main () at temp.c:11 |
| 1189 |
(gdb)</screen>The <function>end()</function> function is called when |
1282 |
(gdb)</screen><para>The <function>end()</function> function is called when |
| 1190 |
a program crashes; in this case, the <function>bazz()</function> |
1283 |
a program crashes; in this case, the <function>bazz()</function> |
| 1191 |
function was called from <function>main()</function>.</para> |
1284 |
function was called from <function>main()</function>.</para> |
| 1192 |
|
1285 |
|
|
Lines 1204-1216
Link Here
|
| 1204 |
|
1297 |
|
| 1205 |
<para>What you do is start up another <command>gdb</command>, use |
1298 |
<para>What you do is start up another <command>gdb</command>, use |
| 1206 |
<command>ps</command> to find the process ID for the child, and |
1299 |
<command>ps</command> to find the process ID for the child, and |
| 1207 |
do<screen>(gdb) <userinput>attach <replaceable>pid</replaceable></userinput></screen> |
1300 |
do</para><screen>(gdb) <userinput>attach <replaceable>pid</replaceable></userinput></screen> |
|
|
1301 |
<para> |
| 1208 |
in <command>gdb</command>, and then debug as usual.</para> |
1302 |
in <command>gdb</command>, and then debug as usual.</para> |
| 1209 |
|
1303 |
|
| 1210 |
<para><quote>That's all very well,</quote> you're probably thinking, |
1304 |
<para><quote>That's all very well,</quote> you're probably thinking, |
| 1211 |
<quote>but by the time I've done that, the child process will be over |
1305 |
<quote>but by the time I've done that, the child process will be over |
| 1212 |
the hill and far away</quote>. Fear not, gentle reader, here's how to |
1306 |
the hill and far away</quote>. Fear not, gentle reader, here's how to |
| 1213 |
do it (courtesy of the <command>gdb</command> info pages): |
1307 |
do it (courtesy of the <command>gdb</command> info pages): |
|
|
1308 |
</para> |
| 1214 |
<screen><lineannotation>&hellip</lineannotation> |
1309 |
<screen><lineannotation>&hellip</lineannotation> |
| 1215 |
if ((pid = fork()) < 0) /* _Always_ check this */ |
1310 |
if ((pid = fork()) < 0) /* _Always_ check this */ |
| 1216 |
error(); |
1311 |
error(); |
|
Lines 1222-1227
Link Here
|
| 1222 |
<lineannotation>&hellip</lineannotation> |
1317 |
<lineannotation>&hellip</lineannotation> |
| 1223 |
} else { /* parent */ |
1318 |
} else { /* parent */ |
| 1224 |
<lineannotation>&hellip</lineannotation></screen> |
1319 |
<lineannotation>&hellip</lineannotation></screen> |
|
|
1320 |
<para> |
| 1225 |
Now all you have to do is attach to the child, set |
1321 |
Now all you have to do is attach to the child, set |
| 1226 |
<symbol>PauseMode</symbol> to <literal>0</literal>, and |
1322 |
<symbol>PauseMode</symbol> to <literal>0</literal>, and |
| 1227 |
wait for the <function>sleep()</function> call to return!</para> |
1323 |
wait for the <function>sleep()</function> call to return!</para> |
|
Lines 1259-1264
Link Here
|
| 1259 |
|
1355 |
|
| 1260 |
<para>It's impossible even to summarise everything Emacs can do here, but |
1356 |
<para>It's impossible even to summarise everything Emacs can do here, but |
| 1261 |
here are some of the features of interest to developers: |
1357 |
here are some of the features of interest to developers: |
|
|
1358 |
</para> |
| 1262 |
<itemizedlist> |
1359 |
<itemizedlist> |
| 1263 |
|
1360 |
|
| 1264 |
<listitem><para>Very powerful editor, allowing search-and-replace on |
1361 |
<listitem><para>Very powerful editor, allowing search-and-replace on |
|
Lines 1289-1295
Link Here
|
| 1289 |
<listitem><para>You can read Usenet news and mail while your program |
1386 |
<listitem><para>You can read Usenet news and mail while your program |
| 1290 |
is compiling.</para></listitem> |
1387 |
is compiling.</para></listitem> |
| 1291 |
|
1388 |
|
| 1292 |
</itemizedlist>And doubtless many more that I've overlooked.</para> |
1389 |
</itemizedlist><para>And doubtless many more that I've overlooked.</para> |
| 1293 |
|
1390 |
|
| 1294 |
<para>Emacs can be installed on FreeBSD using <ulink |
1391 |
<para>Emacs can be installed on FreeBSD using <ulink |
| 1295 |
URL="../../ports/editors.html">the Emacs |
1392 |
URL="../../ports/editors.html">the Emacs |
|
Lines 1386-1392
Link Here
|
| 1386 |
<para>Unfortunately, there's far too much here to explain it in detail; |
1483 |
<para>Unfortunately, there's far too much here to explain it in detail; |
| 1387 |
however there are one or two points worth mentioning.</para> |
1484 |
however there are one or two points worth mentioning.</para> |
| 1388 |
|
1485 |
|
| 1389 |
<para> |
|
|
| 1390 |
<itemizedlist> |
1486 |
<itemizedlist> |
| 1391 |
|
1487 |
|
| 1392 |
<listitem><para>Everything beginning with a <literal>;</> is a |
1488 |
<listitem><para>Everything beginning with a <literal>;</> is a |
|
Lines 1423-1429
Link Here
|
| 1423 |
<listitem><para> We enable Emacs's ability to act as a server, so |
1519 |
<listitem><para> We enable Emacs's ability to act as a server, so |
| 1424 |
that if you're doing something outside Emacs and you want to edit a |
1520 |
that if you're doing something outside Emacs and you want to edit a |
| 1425 |
file, you can just type in |
1521 |
file, you can just type in |
| 1426 |
<screen>$ <userinput>emacsclient <replaceable>filename</replaceable></userinput></screen> |
1522 |
</para> |
|
|
1523 |
<screen>&prompt.user; <userinput>emacsclient <replaceable>filename</replaceable></userinput></screen> |
| 1524 |
<para> |
| 1427 |
and then you can edit the file in your Emacs!<footnote><para>Many |
1525 |
and then you can edit the file in your Emacs!<footnote><para>Many |
| 1428 |
Emacs users set their <systemitem |
1526 |
Emacs users set their <systemitem |
| 1429 |
class=environvar>EDITOR</systemitem> environment to |
1527 |
class=environvar>EDITOR</systemitem> environment to |
|
Lines 1431-1441
Link Here
|
| 1431 |
to edit a file.</para></footnote></para></listitem> |
1529 |
to edit a file.</para></footnote></para></listitem> |
| 1432 |
|
1530 |
|
| 1433 |
</itemizedlist> |
1531 |
</itemizedlist> |
| 1434 |
</para> |
|
|
| 1435 |
|
1532 |
|
| 1436 |
<example> |
1533 |
<example> |
| 1437 |
<title>A sample <filename>.emacs</filename> file</title> |
1534 |
<title>A sample <filename>.emacs</filename> file</title> |
| 1438 |
<screen>;; -*-Emacs-Lisp-*- |
1535 |
<programlisting>;; -*-Emacs-Lisp-*- |
| 1439 |
|
1536 |
|
| 1440 |
;; This file is designed to be re-evaled; use the variable first-time |
1537 |
;; This file is designed to be re-evaled; use the variable first-time |
| 1441 |
;; to avoid any problems with this. |
1538 |
;; to avoid any problems with this. |
|
Lines 1509-1517
Link Here
|
| 1509 |
(autoload 'ediff-buffers "ediff" "Intelligent Emacs interface to diff" t) |
1606 |
(autoload 'ediff-buffers "ediff" "Intelligent Emacs interface to diff" t) |
| 1510 |
(autoload 'ediff-files "ediff" "Intelligent Emacs interface to diff" t) |
1607 |
(autoload 'ediff-files "ediff" "Intelligent Emacs interface to diff" t) |
| 1511 |
(autoload 'ediff-files-remote "ediff" |
1608 |
(autoload 'ediff-files-remote "ediff" |
| 1512 |
"Intelligent Emacs interface to diff") </screen> |
1609 |
"Intelligent Emacs interface to diff") |
| 1513 |
|
1610 |
|
| 1514 |
<screen>(if first-time |
1611 |
(if first-time |
| 1515 |
(setq auto-mode-alist |
1612 |
(setq auto-mode-alist |
| 1516 |
(append '(("\\.cpp$" . c++-mode) |
1613 |
(append '(("\\.cpp$" . c++-mode) |
| 1517 |
("\\.hpp$" . c++-mode) |
1614 |
("\\.hpp$" . c++-mode) |
|
Lines 1613-1621
Link Here
|
| 1613 |
(defun previous-error (n) |
1710 |
(defun previous-error (n) |
| 1614 |
"Visit previous compilation error message and corresponding source code." |
1711 |
"Visit previous compilation error message and corresponding source code." |
| 1615 |
(interactive "p") |
1712 |
(interactive "p") |
| 1616 |
(next-error (- n)))</screen> |
1713 |
(next-error (- n))) |
| 1617 |
|
1714 |
|
| 1618 |
<screen>;; Misc... |
1715 |
;; Misc... |
| 1619 |
(transient-mark-mode 1) |
1716 |
(transient-mark-mode 1) |
| 1620 |
(setq mark-even-if-inactive t) |
1717 |
(setq mark-even-if-inactive t) |
| 1621 |
(setq visible-bell nil) |
1718 |
(setq visible-bell nil) |
|
Lines 1716-1722
Link Here
|
| 1716 |
|
1813 |
|
| 1717 |
;; All done |
1814 |
;; All done |
| 1718 |
(message "All done, %s%s" (user-login-name) ".") |
1815 |
(message "All done, %s%s" (user-login-name) ".") |
| 1719 |
</screen> |
1816 |
</programlisting> |
| 1720 |
</example> |
1817 |
</example> |
| 1721 |
|
1818 |
|
| 1722 |
</sect2> |
1819 |
</sect2> |
|
Lines 1735-1750
Link Here
|
| 1735 |
usually end in <filename>.el</filename>, short for <quote>Emacs |
1832 |
usually end in <filename>.el</filename>, short for <quote>Emacs |
| 1736 |
Lisp</quote>. For example, if whizbang is a FreeBSD |
1833 |
Lisp</quote>. For example, if whizbang is a FreeBSD |
| 1737 |
port, we can locate these files by doing |
1834 |
port, we can locate these files by doing |
| 1738 |
<screen>$ <userinput>find /usr/ports/lang/whizbang -name "*.el" -print</userinput></screen> |
1835 |
</para> |
|
|
1836 |
<screen>&prompt.user; <userinput>find /usr/ports/lang/whizbang -name "*.el" -print</userinput></screen> |
| 1837 |
<para> |
| 1739 |
and install them by copying them into the Emacs site Lisp directory. On |
1838 |
and install them by copying them into the Emacs site Lisp directory. On |
| 1740 |
FreeBSD 2.1.0-RELEASE, this is |
1839 |
FreeBSD 2.1.0-RELEASE, this is |
| 1741 |
<filename>/usr/local/share/emacs/site-lisp</filename>.</para> |
1840 |
<filename>/usr/local/share/emacs/site-lisp</filename>.</para> |
| 1742 |
|
1841 |
|
| 1743 |
<para>So for example, if the output from the find command was |
1842 |
<para>So for example, if the output from the find command was |
|
|
1843 |
</para> |
| 1744 |
<screen>/usr/ports/lang/whizbang/work/misc/whizbang.el</screen> |
1844 |
<screen>/usr/ports/lang/whizbang/work/misc/whizbang.el</screen> |
|
|
1845 |
<para> |
| 1745 |
we would do |
1846 |
we would do |
| 1746 |
<screen>$ <userinput>cp /usr/ports/lang/whizbang/work/misc/whizbang.el /usr/local/share/emacs/site-lisp</userinput></screen> |
|
|
| 1747 |
</para> |
1847 |
</para> |
|
|
1848 |
<screen>&prompt.root; <userinput>cp /usr/ports/lang/whizbang/work/misc/whizbang.el /usr/local/share/emacs/site-lisp</userinput></screen> |
| 1748 |
|
1849 |
|
| 1749 |
<para>Next, we need to decide what extension whizbang source files |
1850 |
<para>Next, we need to decide what extension whizbang source files |
| 1750 |
have. Let's say for the sake of argument that they all end in |
1851 |
have. Let's say for the sake of argument that they all end in |
|
Lines 1755-1765
Link Here
|
| 1755 |
<para>Find the <symbol>auto-mode-alist entry</symbol> in |
1856 |
<para>Find the <symbol>auto-mode-alist entry</symbol> in |
| 1756 |
<filename>.emacs</filename> and add a line for whizbang, such |
1857 |
<filename>.emacs</filename> and add a line for whizbang, such |
| 1757 |
as: |
1858 |
as: |
|
|
1859 |
</para> |
| 1758 |
<programlisting><lineannotation>…</> |
1860 |
<programlisting><lineannotation>…</> |
| 1759 |
("\\.lsp$" . lisp-mode) |
1861 |
("\\.lsp$" . lisp-mode) |
| 1760 |
("\\.wiz$" . whizbang-mode) |
1862 |
("\\.wiz$" . whizbang-mode) |
| 1761 |
("\\.scm$" . scheme-mode) |
1863 |
("\\.scm$" . scheme-mode) |
| 1762 |
<lineannotation>…</></programlisting> |
1864 |
<lineannotation>…</></programlisting> |
|
|
1865 |
<para> |
| 1763 |
This means that Emacs will automatically go into |
1866 |
This means that Emacs will automatically go into |
| 1764 |
<function>whizbang-mode</function> when you edit a file ending in |
1867 |
<function>whizbang-mode</function> when you edit a file ending in |
| 1765 |
<filename>.wiz</filename>.</para> |
1868 |
<filename>.wiz</filename>.</para> |
|
Lines 1767-1776
Link Here
|
| 1767 |
<para>Just below this, you'll find the |
1870 |
<para>Just below this, you'll find the |
| 1768 |
<symbol>font-lock-auto-mode-list</symbol> entry. Add |
1871 |
<symbol>font-lock-auto-mode-list</symbol> entry. Add |
| 1769 |
<function>whizbang-mode</function> to it like so: |
1872 |
<function>whizbang-mode</function> to it like so: |
|
|
1873 |
</para> |
| 1770 |
<programlisting>;; Auto font lock mode |
1874 |
<programlisting>;; Auto font lock mode |
| 1771 |
(defvar font-lock-auto-mode-list |
1875 |
(defvar font-lock-auto-mode-list |
| 1772 |
(list 'c-mode 'c++-mode 'c++-c-mode 'emacs-lisp-mode 'whizbang-mode 'lisp-mode 'perl-mode 'scheme-mode) |
1876 |
(list 'c-mode 'c++-mode 'c++-c-mode 'emacs-lisp-mode 'whizbang-mode 'lisp-mode 'perl-mode 'scheme-mode) |
| 1773 |
"List of modes to always start in font-lock-mode")</programlisting> |
1877 |
"List of modes to always start in font-lock-mode")</programlisting> |
|
|
1878 |
<para> |
| 1774 |
This means that Emacs will always enable |
1879 |
This means that Emacs will always enable |
| 1775 |
<function>font-lock-mode</function> (ie syntax highlighting) when |
1880 |
<function>font-lock-mode</function> (ie syntax highlighting) when |
| 1776 |
editing a <filename>.wiz</filename> file.</para> |
1881 |
editing a <filename>.wiz</filename> file.</para> |