diff --git a/documentation/content/en/books/developers-handbook/tools/_index.adoc b/documentation/content/en/books/developers-handbook/tools/_index.adoc --- a/documentation/content/en/books/developers-handbook/tools/_index.adoc +++ b/documentation/content/en/books/developers-handbook/tools/_index.adoc @@ -6,7 +6,7 @@ prev: books/developers-handbook/introduction next: books/developers-handbook/secure 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]] @@ -173,8 +173,8 @@ [[tools-compiling]] == Compiling with `cc` -This section deals with the gcc and clang compilers for C and C++, since they come with the FreeBSD base system. -Starting with FreeBSD 10.X `clang` is installed as `cc`. +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`; 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. Once you have written your masterpiece, the next step is to convert it into something that will (hopefully!) run on FreeBSD. @@ -208,7 +208,7 @@ % 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, 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.] @@ -278,12 +278,12 @@ 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. -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. This is known as _portable code_. 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] .... @@ -321,79 +321,6 @@ === 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 - -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 -#include - -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? Remember, `cc` will call the executable [.filename]#a.out# unless you tell it differently.