Changeset View
Changeset View
Standalone View
Standalone View
documentation/content/en/books/developers-handbook/tools/_index.adoc
--- | --- | ||||
title: Chapter 2. Programming Tools | title: Chapter 2. Programming Tools | ||||
authors: | authors: | ||||
- author: James Raynard | - author: James Raynard | ||||
- author: Murray Stokely | - author: Murray Stokely | ||||
prev: books/developers-handbook/introduction | prev: books/developers-handbook/introduction | ||||
next: books/developers-handbook/secure | next: books/developers-handbook/secure | ||||
description: Programming Tools | description: Programming Tools | ||||
tags: ["tools", "Interpreters", "Compilers", "cc", "make", "Debugging", "lldb", "gdb", "Emacs"] | tags: ["tools", "Interpreters", "Compilers", "cc", "make", "Debugging", "lldb", "gdb", "clang", "Emacs"] | ||||
--- | --- | ||||
[[tools]] | [[tools]] | ||||
= Programming Tools | = Programming Tools | ||||
:doctype: book | :doctype: book | ||||
:toc: macro | :toc: macro | ||||
:toclevels: 1 | :toclevels: 1 | ||||
:icons: font | :icons: font | ||||
▲ Show 20 Lines • Show All 150 Lines • ▼ Show 20 Lines | |||||
As the edit-compile-run-debug cycle is rather tedious when using separate programs, many commercial compiler makers have produced Integrated Development Environments (IDEs for short). | As the edit-compile-run-debug cycle is rather tedious when using separate programs, many commercial compiler makers have produced Integrated Development Environments (IDEs for short). | ||||
FreeBSD does not include an IDE in the base system, but package:devel/kdevelop[] is available in the Ports Collection and many use Emacs for this purpose. | FreeBSD does not include an IDE in the base system, but package:devel/kdevelop[] is available in the Ports Collection and many use Emacs for this purpose. | ||||
Using Emacs as an IDE is discussed in <<emacs>>. | Using Emacs as an IDE is discussed in <<emacs>>. | ||||
[[tools-compiling]] | [[tools-compiling]] | ||||
== Compiling with `cc` | == Compiling with `cc` | ||||
This section deals with the gcc and clang compilers for C and C++, since they come with the FreeBSD base system. | This section deals with the clang compiler for C and C++, as it's installed with the FreeBSD base system. | ||||
Starting with FreeBSD 10.X `clang` is installed as `cc`. | Starting with FreeBSD 10.X `clang` is installed as `cc`; the GNU compiler package:devel/gcc[gcc] is available in the Ports Collection. | ||||
The details of producing a program with an interpreter vary considerably between interpreters, and are usually well covered in the documentation and on-line help for the interpreter. | The details of producing a program with an interpreter vary considerably between interpreters, and are usually well covered in the documentation and on-line help for the interpreter. | ||||
Once you have written your masterpiece, the next step is to convert it into something that will (hopefully!) run on FreeBSD. | Once you have written your masterpiece, the next step is to convert it into something that will (hopefully!) run on FreeBSD. | ||||
This usually involves several steps, each of which is done by a separate program. | This usually involves several steps, each of which is done by a separate program. | ||||
[.procedure] | [.procedure] | ||||
. Pre-process your source code to remove comments and do other tricks like expanding macros in C. | . Pre-process your source code to remove comments and do other tricks like expanding macros in C. | ||||
. Check the syntax of your code to see if you have obeyed the rules of the language. If you have not, it will complain! | . Check the syntax of your code to see if you have obeyed the rules of the language. If you have not, it will complain! | ||||
Show All 17 Lines | |||||
will cause [.filename]#foobar.c# to be compiled by all the steps above. | will cause [.filename]#foobar.c# to be compiled by all the steps above. | ||||
If you have more than one file to compile, just do something like | If you have more than one file to compile, just do something like | ||||
[source,bash] | [source,bash] | ||||
.... | .... | ||||
% cc foo.c bar.c | % cc foo.c bar.c | ||||
.... | .... | ||||
Note that the syntax checking is just that-checking the syntax. | Note that the syntax checking is just that - checking the syntax. | ||||
It will not check for any logical mistakes you may have made, like putting the program into an infinite loop, | It will not check for any logical mistakes you may have made, like putting the program into an infinite loop, | ||||
or using a bubble sort when you meant to use a binary sort.footnote:[In case you did not know, a binary sort is an efficient way of sorting things into order and a bubble sort is not.] | or using a bubble sort when you meant to use a binary sort.footnote:[In case you did not know, a binary sort is an efficient way of sorting things into order and a bubble sort is not.] | ||||
There are lots and lots of options for `cc`, which are all in the manual page. | There are lots and lots of options for `cc`, which are all in the manual page. | ||||
Here are a few of the most important ones, with examples of how to use them. | Here are a few of the most important ones, with examples of how to use them. | ||||
`-o _filename_`:: | `-o _filename_`:: | ||||
The output name of the file. If you do not use this option, `cc` will produce an executable called [.filename]#a.out#.footnote:[The reasons for this are buried in the mists of history.] | The output name of the file. If you do not use this option, `cc` will produce an executable called [.filename]#a.out#.footnote:[The reasons for this are buried in the mists of history.] | ||||
▲ Show 20 Lines • Show All 53 Lines • ▼ Show 20 Lines | |||||
`-ansi`:: | `-ansi`:: | ||||
Turn off most, but not all, of the non-ANSI C features provided by `cc`. | Turn off most, but not all, of the non-ANSI C features provided by `cc`. | ||||
Despite the name, it does not guarantee strictly that your code will comply to the standard. | Despite the name, it does not guarantee strictly that your code will comply to the standard. | ||||
`-pedantic`:: | `-pedantic`:: | ||||
Turn off _all_ ``cc``'s non-ANSI C features. | Turn off _all_ ``cc``'s non-ANSI C features. | ||||
Without these flags, `cc` will allow you to use some of its non-standard extensions to the standard. | Without these flags, `cc` will allow you to use some of its non-standard extensions to the standard. | ||||
Some of these are very useful, but will not work with other compilers-in fact, | Some of these are very useful, but will not work with other compilers - in fact, | ||||
one of the main aims of the standard is to allow people to write code that will work with any compiler on any system. | one of the main aims of the standard is to allow people to write code that will work with any compiler on any system. | ||||
This is known as _portable code_. | This is known as _portable code_. | ||||
Generally, you should try to make your code as portable as possible, | Generally, you should try to make your code as portable as possible, | ||||
as otherwise you may have to completely rewrite the program later to get it to work somewhere else-and who knows what you may be using in a few years time? | as otherwise you may have to completely rewrite the program later to get it to work somewhere else - and who knows what you may be using in a few years time? | ||||
[source,bash] | [source,bash] | ||||
.... | .... | ||||
% cc -Wall -ansi -pedantic -o foobar foobar.c | % cc -Wall -ansi -pedantic -o foobar foobar.c | ||||
.... | .... | ||||
This will produce an executable [.filename]#foobar# after checking [.filename]#foobar.c# for standard compliance. | This will produce an executable [.filename]#foobar# after checking [.filename]#foobar.c# for standard compliance. | ||||
Show All 20 Lines | |||||
[source,bash] | [source,bash] | ||||
.... | .... | ||||
% c++ -o foobar foobar.cc | % c++ -o foobar foobar.cc | ||||
.... | .... | ||||
+ | + | ||||
This will both produce an executable [.filename]#foobar# from the C++ source file [.filename]#foobar.cc#. | This will both produce an executable [.filename]#foobar# from the C++ source file [.filename]#foobar.cc#. | ||||
=== Common `cc` Queries and Problems | === Common `cc` Queries and Problems | ||||
==== I am trying to write a program which uses the sin() function and I get an error like this. What does it mean? | |||||
[source,bash] | |||||
.... | |||||
/var/tmp/cc0143941.o: Undefined symbol `_sin' referenced from text segment | |||||
.... | |||||
When using mathematical functions like `sin()`, you have to tell `cc` to link in the math library, like so: | |||||
[source,bash] | |||||
.... | |||||
% cc -o foobar foobar.c -lm | |||||
.... | |||||
==== All right, I wrote this simple program to practice using -lm. All it does is raise 2.1 to the power of 6. | |||||
[.programlisting] | |||||
.... | |||||
#include <stdio.h> | |||||
int main() { | |||||
float f; | |||||
f = pow(2.1, 6); | |||||
printf("2.1 ^ 6 = %f\n", f); | |||||
return 0; | |||||
} | |||||
.... | |||||
and I compiled it as: | |||||
[source,bash] | |||||
.... | |||||
% cc temp.c -lm | |||||
.... | |||||
like you said I should, but I get this when I run it: | |||||
[source,bash] | |||||
.... | |||||
% ./a.out | |||||
2.1 ^ 6 = 1023.000000 | |||||
.... | |||||
This is not the right answer! What is going on? | |||||
When the compiler sees you call a function, it checks if it has already seen a prototype for it. | |||||
If it has not, it assumes the function returns an int, which is definitely not what you want here. | |||||
==== So how do I fix this? | |||||
The prototypes for the mathematical functions are in [.filename]#math.h#. | |||||
If you include this file, the compiler will be able to find the prototype and it will stop doing strange things to your calculation! | |||||
[.programlisting] | |||||
.... | |||||
#include <math.h> | |||||
#include <stdio.h> | |||||
int main() { | |||||
... | |||||
.... | |||||
After recompiling it as you did before, run it: | |||||
[source,bash] | |||||
.... | |||||
% ./a.out | |||||
2.1 ^ 6 = 85.766121 | |||||
.... | |||||
If you are using any of the mathematical functions, _always_ include [.filename]#math.h# and remember to link in the math library. | |||||
==== I compiled a file called foobar.c and I cannot find an executable called foobar. Where has it gone? | ==== I compiled a file called foobar.c and I cannot find an executable called foobar. Where has it gone? | ||||
Remember, `cc` will call the executable [.filename]#a.out# unless you tell it differently. | Remember, `cc` will call the executable [.filename]#a.out# unless you tell it differently. | ||||
Use the `-o _filename_` option: | Use the `-o _filename_` option: | ||||
[source,bash] | [source,bash] | ||||
.... | .... | ||||
▲ Show 20 Lines • Show All 1,307 Lines • Show Last 20 Lines |