Call a C library from main Fortran code

I have a C library that downloaded it from [here]. This library has some files in the “.dll” format and some files in the “.exe” format. I want to call it from Fortran code, probably as a subroutine. I want to know how can I introduce the library to Fortran that finds it easily. Which command and instruction are proper to call it in the main code.

I use IDE (Geany with the last version of gcc and Plato with an older version of gnu). Please, explain your solution in detail.

I will appreciate any comments that give me a little help and forgive me for writing shortcomings.
(qvoronoi -- Voronoi diagram)

It depends on the compiler you use - apparently you use gcc and gfortran on Windows. On Windows some compilers will require a so-called import library, but the Gnu compilers work directly with the DLL IIRC.
That said, the first taks will be to write an interface for the C routines you want to use. Not all this difficult, if you use the ISO_C_BINDING module and the bind() attribute. The Fortran Wiki ( has more information about that. Here is a page you may find useful: c_interface_module in Fortran Wiki

1 Like

For these compilers, some C functions can be called without using the C interop features of Fortran 2003. For example, the C code

#include <stdlib.h>
void sum_abs_int_(int *in, int *num, int *out) {
    int i,sum;
    sum = 0;
    for (i=0; i < *num; ++i) {sum += abs(in[i]);}
    *out = sum;

void sum_abs_int_val_(int *in, int num, int *out) {
	int i,sum;
	sum = 0;
	for (i=0; i < num; ++i) {sum += abs(in[i]);}
	*out = sum;

void sum_abs_double_(double *in, int *num, double *out) {
    int i;
    double sum;
    sum = 0;
    for (i=0; i < *num; ++i) {sum += abs(in[i]);}
    *out = sum;

double twice_(double *x) {
	return *x*2;

double thrice(double *x) {
	return *x * 3;

can be called from

program xcallc
implicit none
integer, parameter :: n = 3, ivec(n)=[4,-2,7], dp = kind(1.0d0)
integer            :: isum
real(kind=dp)      :: dsum,twice
write (*,"(a,3(1x,i0))") "ivec =",ivec
call sum_abs_int(ivec,n,isum)
call sum_abs_int_val(ivec,%val(n),isum) ! use %val() extension to pass by value
call sum_abs_double(dble(ivec),n,dsum)
end program xcallc

with the compilation script

gfortran -c xcallc.f90
gcc -c cforf.c
gfortran cforf.o xcallc.o
if exist a.exe a.exe


ivec = 4 -2 7
 sum(abs(ivec))=          13
 sum(abs(ivec))=          13
 sum(abs(dble(ivec)))=   13.000000000000000     

and the C function thrice() that does not have an underscore can be called from Fortran code that is compiled with the gfortran -fno-underscoring option. Admittedly the Fortran code is nonstandard since it uses the %val() function, and the above procedure may not work for other pairs of compilers.

Thanks for your comment. One part of my question is about:
I download a library from here. There is a bunch of code in there. I want to know how Fortran find the library and which file was needed for introducing it to Fortran?
I read the links. It described the preparation of variables not introducing it.

There is nothing magic about that .I am afraid: you will have to help the compiler. It does look like you are new to this - perhaps Building programs - Fortran Programming Language helps? I apologize if I underestimate your knowledge, though.

1 Like

Another thing: I had a look at the documentation of the qhull library. It emphasizes the use of qhull via its C++ interface rather than via C. It is certainly possible to use the C API, but it seems it exposes you to the internal structures much more. An alternative is perhaps to interface with the program instead of the library - that would mean to write the relevant input files and read back in the results. That may be a quick(er) way to determine if qhull will help you solve the problems you want to study.


First of all, thanks for the link you sent. That link was very useful. In addition, I will always be eager to learn. So if you have additional resources please add them.

If I understand correctly:
I must first write my original source file.
Then I write my other files either as a module or as a subroutine.
Prepare the file related to the C program.
Compile the attached Fortran files separately. I can convert them to a library file if I want.
Convert file C to dynamic library.
Link them to the original file.
Then build the whole project. Building is done via using software (GUI or CMAKE).
I can finally run it.

My question is here:
It seems to be a subroutine interface with C library file. Then call this subroutine interface in the main program. It is true?