Skip to content

Commit f2dd1b0

Browse files
authored
Merge pull request #156 from awvwgk/build-tools
Update building programs book
2 parents 178a0da + be77403 commit f2dd1b0

8 files changed

+589
-109
lines changed

learn/building_programs/build_tools.md

Lines changed: 537 additions & 57 deletions
Large diffs are not rendered by default.

learn/building_programs/compiling_source.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ permalink: /learn/building_programs/compiling_source
55
---
66

77
The first step in the build process is to compile the source code. The
8-
output from this step is generally known as the object code - a set of
8+
output from this step is generally known as the object code a set of
99
instructions for the computer generated from the human-readable source
1010
code. Different compilers will produce different object codes from the
1111
same source code and the naming conventions are different.
@@ -54,7 +54,7 @@ Windows.
5454
Some remarks:
5555

5656
* The compiler may complain about the contents of the source file, if it
57-
finds something wrong with it - a typo for instance or an unknown
57+
finds something wrong with it a typo for instance or an unknown
5858
keyword. In that case the compilation process is broken off and you will
5959
not get an object file or an executable program. For instance, if
6060
the word "program" was inadvertently typed as "prgoram":
@@ -78,7 +78,7 @@ Using this compilation report you can correct the source code and try
7878
again.
7979

8080
* The step without "-c" can only succeed if the source file contains a
81-
main program - characterised by the `program` statement in Fortran.
81+
main program characterised by the `program` statement in Fortran.
8282
Otherwise the link step will complain about a missing "symbol", something
8383
along these lines:
8484

@@ -98,9 +98,9 @@ compiler running in a Cygwin environment on Windows.
9898
9999
Compilers also differ in the options they support, but in general:
100100
101-
* Options for optimising the code - resulting in faster programs or
101+
* Options for optimising the code resulting in faster programs or
102102
smaller memory footprints;
103-
* Options for checking the source code - checks that a variable is not
103+
* Options for checking the source code checks that a variable is not
104104
used before it has been given a value, for instance or checks if some
105105
extension to the language is used;
106106
* Options for the location of include or module files, see below;

learn/building_programs/distributing.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ choose from:
1313

1414
__Option 1: Distribute the entire source code__
1515

16-
By far the simplest - for you as a programmer - is this one: you leave it
16+
By far the simplest for you as a programmer is this one: you leave it
1717
up to the user to build it on their own machine. Unfortunately, that
1818
means you will have to have a user-friendly build system in place and
1919
the user will have to have access to suitable compilers. For build systems:
@@ -79,17 +79,17 @@ end module user_functions
7979
* Provide a basic build script with a command like:
8080

8181
```shell
82-
gfortran -o function.dll function.f90 -shared
82+
gfortran -o functions.dll functions.f90 -shared
8383
```
8484

8585
or:
8686

8787
```shell
88-
ifort -exe:function.dll function.f90 -dll
88+
ifort -exe:functions.dll functions.f90 -dll
8989
```
9090

91-
As said, you cannot control that the user has done the right thing - any
92-
DLL "function.dll" with a function `f` would be accepted, but not necessarily
91+
As said, you cannot control that the user has done the right thing any
92+
DLL "functions.dll" with a function `f` would be accepted, but not necessarily
9393
lead to a successful run.
9494

9595
An alternative set-up would be to change the main program into a subroutine

learn/building_programs/include_files.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,14 @@ tabulate/
3434
main/
3535
tabulate.f90
3636
sub/
37-
function.f90
37+
functions.f90
3838
```
3939

40-
Compiling the file "function.f90" with the commands
40+
Compiling the file "functions.f90" with the commands
4141

4242
```shell
4343
$ cd sub
44-
$ gfortran -c function.f90
44+
$ gfortran -c functions.f90
4545
```
4646

4747
leads to this structure:
@@ -51,18 +51,18 @@ tabulate/
5151
main/
5252
tabulate.f90
5353
sub/
54-
function.f90
55-
function.mod
56-
function.o
54+
functions.f90
55+
user_functions.mod
56+
functions.o
5757
```
5858

5959
To successfully compile and subsequently build the program we need to
60-
tell the compiler where it can find the file "function.mod":
60+
tell the compiler where it can find the file "user\_functions.mod":
6161

6262
```shell
6363
$ cd main
6464
$ gfortran -c tabulate.f90 -I ../sub
65-
$ gfortran -o tabulate tabulate.o ../sub/function.o
65+
$ gfortran -o tabulate tabulate.o ../sub/functions.o
6666
```
6767

6868
The result:
@@ -74,9 +74,9 @@ tabulate/
7474
tabulate.o
7575
tabulate (or tabulate.exe on Windows)
7676
sub/
77-
function.f90
78-
function.mod
79-
function.o
77+
functions.f90
78+
functions.o
79+
user_functions.mod
8080
```
8181

8282
Notes:

learn/building_programs/index.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ author: Arjen Markus, Ondřej Čertík, Milan Curcic, Laurence Kedward, Brad Ric
88
Languages like Fortran, C, C++ and Java, to name but a few, share
99
certain characteristics: you write code in your language of choice but
1010
then you have to build an executable program from that source code.
11-
Other languages are interpreted - the source code is analysed by a
11+
Other languages are interpreted the source code is analysed by a
1212
special program and taken as direct instructions. Two very simple
1313
examples of that type of language: Windows batch files and Linux shell
1414
scripts.
@@ -38,7 +38,7 @@ it is simple to express in source code, a lot of things actually happen
3838
when the executable that is built from this code runs:
3939

4040
* A process is started on the computer in such a way that it can write
41-
to the console - the window (DOS-box, xterm, ...) at which you type the
41+
to the console the window (DOS-box, xterm, ...) at which you type the
4242
program's name.
4343
* It writes the text "Hello!" to the console. To do so it must properly
4444
interact with the console.

learn/building_programs/linking_pieces.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ program tabulate
2929
end program tabulate
3030
```
3131

32-
Note the `use` statement - this will be where we define the function `f`.
32+
Note the `use` statement this will be where we define the function `f`.
3333

3434
We want to make the program general, so keep the
35-
specific source code - the implementation of the function `f` -
35+
specific source code the implementation of the function `f`
3636
separated from the general source code. There are several ways to
3737
achieve this, but one is to put it in a different source file. We can
3838
give the general program to a user and they provide a specific source code.
@@ -60,12 +60,12 @@ program. Because the program "tabulate" depends on the module
6060
first. A sequence of commands to do this is:
6161

6262
```shell
63-
$ gfortran -c function.f90
64-
$ gfortran tabulate.f90 function.o
63+
$ gfortran -c functions.f90
64+
$ gfortran tabulate.f90 functions.o
6565
```
6666

6767
The first step compiles the module, resulting in an object file
68-
"function.o" and a module intermediate file, "function.mod". This module
68+
"functions.o" and a module intermediate file, "user\_functions.mod". This module
6969
file contains all the information the compiler needs to determine that
7070
the function `f` is defined in this module and what its interface is. This
7171
information is important: it enables the compiler to check that you call
@@ -77,13 +77,13 @@ check anything.
7777
The second step invokes the compiler in such a way that:
7878

7979
* it compiles the file "tabulate.f90" (using the module file);
80-
* it invokes the linker to combine the object files tabulate.o and function.o into an
81-
executable program - with the default name "a.out" or "a.exe" (if you
80+
* it invokes the linker to combine the object files tabulate.o and functions.o into an
81+
executable program with the default name "a.out" or "a.exe" (if you
8282
want a different name, use the option "-o").
8383

8484
What you do not see in general is that the linker also adds a number of
8585
extra files in this link step, the run-time libraries. These run-time
86-
libraries contain all the "standard" stuff - low-level routines that do
86+
libraries contain all the "standard" stuff low-level routines that do
8787
the input and output to screen, the `sin` function and much more.
8888

8989
If you want to see the gory details, add the option "-v". This instructs

learn/building_programs/managing_libraries.md

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Libraries contain any number of object files in a compact form, so that
1414
the command-line becomes far shorter:
1515

1616
```shell
17-
$ gfortran -o tabulate tabulate.f90 function.o supportlib.a
17+
$ gfortran -o tabulate tabulate.f90 functions.o supportlib.a
1818
```
1919

2020
where "supportlib.a" is a collection of one, two or many object files,
@@ -104,8 +104,8 @@ a dynamic link library)
104104

105105
There is one more thing to be aware of: On Windows you must
106106
explicitly specify that a procedure is to be _exported_, i.e. is visible
107-
in the dynamic library. There are several ways - depending on the
108-
compiler you use - to achieve this. One method is via a so-called
107+
in the dynamic library. There are several ways depending on the
108+
compiler you use to achieve this. One method is via a so-called
109109
compiler directive:
110110

111111
```fortran
@@ -129,26 +129,26 @@ we look at the `tabulate` program in the file "tabulate.f90".
129129

130130
## GNU/Linux and gfortran
131131
The `tabulate` program requires a user-defined routine `f`. If we
132-
let it reside in a dynamic library, say "function.dll", we can simply
132+
let it reside in a dynamic library, say "functions.dll", we can simply
133133
replace the implementation of the function by putting another dynamic
134134
library in the directory. No need to rebuild the program as such.
135135

136-
On Cygwin it is not necessary to explicitly export a procedure - all
136+
On Cygwin it is not necessary to explicitly export a procedure all
137137
publically visible routines are exported when you build a dynamic library.
138138
Also, no import library is generated.
139139

140140
Since our dynamic library can be built from a single source file, we
141141
can take a shortcut:
142142

143143
```shell
144-
$ gfortran -shared -o function.dll function.f90
144+
$ gfortran -shared -o functions.dll functions.f90
145145
```
146146

147-
This produces the files "function.dll" and "function.mod". The
147+
This produces the files "functions.dll" and "user\_functions.mod". The
148148
utility `nm` tells us the exact name of the function `f`:
149149

150150
```shell
151-
$ nm function.dll
151+
$ nm functions.dll
152152
...
153153
000000054f9d7000 B __dynamically_loaded
154154
U __end__
@@ -165,21 +165,21 @@ other routine "f" that might be defined in another module.
165165
The next step is to build the program:
166166

167167
```shell
168-
$ gfortran -o tabulate tabulate.f90 function.dll
168+
$ gfortran -o tabulate tabulate.f90 functions.dll
169169
```
170170

171171
The DLL and the .mod file are used to build the executable program
172172
with checks on the function's interface, the right name and the reference
173-
to "a" DLL, called "function.dll".
173+
to "a" DLL, called "functions.dll".
174174

175-
You can replace the shared library "function.dll" by another one, implementing
175+
You can replace the shared library "functions.dll" by another one, implementing
176176
a different function "f". Of course, you need to be careful to use the correct
177177
interface for this function. The compiler/linker are not invoked anymore, so they
178178
can do no checking.
179179

180180
## Windows and Intel Fortran
181181
The setup is the same as with Linux, but on Windows it is necessary
182-
to explicitly export the routines. And an import library is generated -
182+
to explicitly export the routines. And an import library is generated
183183
this is the library that should be used in the link step.
184184

185185
The source file must contain the compiler directive, otherwise the function `f`
@@ -193,27 +193,27 @@ real function f( x )
193193
Again we take a shortcut:
194194

195195
```shell
196-
$ ifort -exe:function.dll function.f90 -dll
196+
$ ifort -exe:functions.dll functions.f90 -dll
197197
```
198198

199-
This produces the files "function.dll", "function.mod" as well as "function.lib" (and two
199+
This produces the files "functions.dll", "user\_functions.mod" as well as "functions.lib" (and two
200200
other files of no importance here). The "dependency walker" program tells us
201201
that the exact name of the function "f" is `FUNCTION_mp_F`. It is also exported, so that
202202
it can be found by the linker in the next step:
203203

204204
```
205-
$ ifort tabulate.f90 function.lib
205+
$ ifort tabulate.f90 functions.lib
206206
```
207207

208208
Note that we need to specify the name of the export library, not the DLL!
209209

210210
(Note also: the Intel Fortran compiler uses the name of the first source file as the
211-
name for the executable - here we do without the `-exe` option.)
211+
name for the executable here we do without the `-exe` option.)
212212

213213
Just as under Cygwin, the DLL and the .mod file are used to build the executable program
214214
with checks on the function's interface, the right name and the reference
215-
to "a" DLL, called "function.dll".
215+
to "a" DLL, called "functions.dll".
216216

217-
You can replace the shared library "function.dll" by another one, but the same
217+
You can replace the shared library "functions.dll" by another one, but the same
218218
caution is required: while the implementation can be quite different, the
219219
function's interface must be the same.

learn/building_programs/runtime_libraries.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ $ ldd tabulate.exe
2121

2222
Other compilers or other versions of the same compiler will probably
2323
require different dynamic libraries. As long as you run the program on
24-
the same computer - or, more accurately, within the same environment -
24+
the same computer or, more accurately, within the same environment
2525
there should be no problem. However, when such a library cannot be
2626
found, you will get (hopefully) an error message and the program stops
2727
immediately.
@@ -39,7 +39,7 @@ _On Linux:_
3939

4040
* The environment variable `LD_LIBRARY_PATH` is used. It consists of a
4141
list of directories to be searched, each directory separated via colons
42-
(:) from the others. For instance: `/usr/lib:/usr/local/lib` - typical
42+
(:) from the others. For instance: `/usr/lib:/usr/local/lib` typical
4343
system directories.
4444
* At the link step you can also use an option to set `RPATH`, a list
4545
of directories that is put into the executable file itself.
@@ -54,7 +54,7 @@ to be searched, but now the separating character is the semicolon (;).
5454
* A set of system directories is searched.
5555

5656
Unfortunately, the details can change from one version of the operating
57-
system to the next. The above is merely an indication - use tools like
57+
system to the next. The above is merely an indication use tools like
5858
"ldd" or "dependency walker" to find out what libraries are loaded and
5959
where they are found.
6060

0 commit comments

Comments
 (0)