Skip to content

Commit 643c255

Browse files
authored
Merge pull request #781 from jannisteunissen/master
Add brief explanation on how to include stdlib in a Makefile
2 parents ab2256a + d288010 commit 643c255

File tree

1 file changed

+63
-13
lines changed

1 file changed

+63
-13
lines changed

Diff for: README.md

+63-13
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,30 @@ fpm run --example prog
201201
with `prog` being the name of the example program (e.g., `example_sort`).
202202

203203

204+
## Using stdlib in your project
205+
206+
### Using stdlib with CMake
207+
208+
The stdlib project exports CMake package files and pkg-config files to make stdlib usable for other projects.
209+
The package files are located in the library directory in the installation prefix.
210+
211+
For CMake builds of stdlib you can find a local installation with
212+
213+
```cmake
214+
find_package(fortran_stdlib REQUIRED)
215+
...
216+
target_link_libraries(
217+
${PROJECT_NAME}
218+
PRIVATE
219+
fortran_stdlib::fortran_stdlib
220+
)
221+
```
222+
223+
To make the installed stdlib project discoverable add the stdlib directory to the ``CMAKE_PREFIX_PATH``.
224+
The usual install location of the package files is ``$PREFIX/lib/cmake/fortran_stdlib``.
225+
226+
### Using stdlib with fpm
227+
204228
To use `stdlib` within your `fpm` project, add the following lines to your `fpm.toml` file:
205229
```toml
206230
[dependencies]
@@ -215,25 +239,51 @@ stdlib = { git="https://github.com/fortran-lang/stdlib", branch="stdlib-fpm" }
215239
>
216240
> [see also](https://fpm.fortran-lang.org/spec/metapackages.html)
217241
218-
## Using stdlib in your project
242+
### Using stdlib with a regular Makefile
219243

220-
The stdlib project exports CMake package files and pkg-config files to make stdlib usable for other projects.
221-
The package files are located in the library directory in the installation prefix.
244+
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)
222250

223-
For CMake builds of stdlib you can find a local installation with
251+
STDLIB_CFLAGS := `pkg-config --cflags fortran_stdlib`
252+
STDLIB_LIBS := `pkg-config --libs fortran_stdlib`
224253

225-
```cmake
226-
find_package(fortran_stdlib REQUIRED)
254+
# Example definition of Fortran compiler and flags
255+
FC := gfortran
256+
FFLAGS := -O2 -Wall -g
257+
258+
# Definition of targets etc.
227259
...
228-
target_link_libraries(
229-
${PROJECT_NAME}
230-
PRIVATE
231-
fortran_stdlib::fortran_stdlib
232-
)
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+
233269
```
234270

235-
To make the installed stdlib project discoverable add the stdlib directory to the ``CMAKE_PREFIX_PATH``.
236-
The usual install location of the package files is ``$PREFIX/lib/cmake/fortran_stdlib``.
271+
The same can also be achieved without pkg-config.
272+
If the library has been installed in a directory inside the compiler's search path,
273+
only a flag `-lfortran_stdlib` is required.
274+
If the installation directory is not in the compiler's search path, one can add for example
275+
```make
276+
install_dir := path/to/install_dir
277+
libdir := $(install_dir)/lib
278+
moduledir := $(install_dir)/include/fortran_stdlib/<compiler name and version>
279+
```
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
282+
```make
283+
build_dir := path/to/build_dir
284+
libdir := $(build_dir)/src
285+
moduledir := $(build_dir)/src/mod_files
286+
```
237287

238288
## Documentation
239289

0 commit comments

Comments
 (0)