Skip to content

Commit ecf85d0

Browse files
Add pkg-config usage in Makefile
1 parent 1cb5e9a commit ecf85d0

File tree

1 file changed

+36
-7
lines changed

1 file changed

+36
-7
lines changed

README.md

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -242,18 +242,47 @@ stdlib = { git="https://github.com/fortran-lang/stdlib", branch="stdlib-fpm" }
242242
### Using stdlib with a regular Makefile
243243

244244
After the library has been built, it can be included in a regular Makefile.
245+
The recommended way to do this is using the [pkg-config](https://www.freedesktop.org/wiki/Software/pkg-config/) tool, for which an example is shown below.
246+
```make
247+
# Necessary if the installation directory is not in PKG_CONFIG_PATH
248+
install_dir := path/to/install_dir
249+
export PKG_CONFIG_PATH := $(install_dir)/lib/pkgconfig:$(PKG_CONFIG_PATH)
250+
251+
STDLIB_CFLAGS := `pkg-config --cflags fortran_stdlib`
252+
STDLIB_LIBS := `pkg-config --libs fortran_stdlib`
253+
254+
# Example definition of Fortran compiler and flags
255+
FC := gfortran
256+
FFLAGS := -O2 -Wall -g
257+
258+
# Definition of targets etc.
259+
...
260+
261+
# Example rule to compile object files from .f90 files
262+
%.o: %.f90
263+
$(FC) -c -o $@ $< $(FFLAGS) $(STDLIB_CFLAGS)
264+
265+
# Example rule to link an executable from object files
266+
%: %.o
267+
$(FC) -o $@ $^ $(FFLAGS) $(STDLIB_LIBS)
268+
269+
```
270+
271+
The same can also be achieved without pkg-config.
245272
If the library has been installed in a directory inside the compiler's search path,
246-
only a flag `-lfortran_stdlib` is required to link against `libfortran_stdlib.a` or `libfortran_stdlib.so`.
273+
only a flag `-lfortran_stdlib` is required.
247274
If the installation directory is not in the compiler's search path, one can add for example
248275
```make
249-
libdir=${install_dir}/lib
250-
moduledir=${install_dir}/include/fortran_stdlib/<compiler name and version>
276+
install_dir := path/to/install_dir
277+
libdir := $(install_dir)/lib
278+
moduledir := $(install_dir)/include/fortran_stdlib/<compiler name and version>
251279
```
252-
Here it is assumed that the compiler will look for libraries in `libdir` and for module files in `moduledir`.
253-
The library can also be included from a build directory without installation:
280+
The linker should then look for libraries in `libdir` (using e.g.`-L$(libdir)`) and the compiler should look for module files in `moduledir` (using e.g. `-I$(moduledir)`).
281+
Alternatively, the library can also be included from a build directory without installation with
254282
```make
255-
libdir=$(build_dir)/src
256-
moduledir=$(build_dir)/src/mod_files
283+
...
284+
libdir := $(build_dir)/src
285+
moduledir := $(build_dir)/src/mod_files
257286
```
258287

259288
## Documentation

0 commit comments

Comments
 (0)