Topic: c and c++ project

Can SF be used as a c and c++ ide?
If so, can you explain how?

Re: c and c++ project

Simply Fortran has reasonably good support for the C language (in terms of the File Outline and searching for procedure definitions) and somewhat basic support for C++. It also includes the C and C++ compiler on Windows and macOS.  You can just add the C or C++ files to a project just like any Fortran file.  When you build the project, it should have no problem with those files. 

A project containing C++ files does need an explicit library linking command, though, because of our default linking step.  In Project Options under Compiler flags, one would need to add:


to the Linker box to ensure that the C++ standard library is also linked. 

There's really no additional steps.  You can mix C and Fortran source in the same project (I do this regularly), and Simply Fortran will compile it all.

Jeff Armstrong
Approximatrix, LLC

Re: c and c++ project

Do your mixed language projects have C/C++ calling Fortran functions and vice versa?
Is there any extra interoperability code that is needed?
Can you make a mixed language project available for us to look at?
Or direct us to a web source to learn from that is consistent with SF?

Re: c and c++ project

I'll have to work up some more complex examples, but you'll want to look into using the iso_c_binding module, which allows interfacing Fortran with C (and, hence, a whole collection of C-compatible languages).  For example, I can write a very simple Fortran program that calls a C floating-point routine:

program main
use iso_c_binding
implicit none


        function timestwo(x) bind(c)
        use iso_c_binding
        real(kind=c_float), value::x
        end function timestwo
    end interface

    print *, "Times two via C"

    write(*, '(1X, A21)', advance='no') "Enter a real number: "
    read(*, *) timesval

    write(*, '(1X, A43, F8.3, A4, F8.3)') "The C language tells me that two times ", &
                                          real(timesval, kind=c_float), &
                                          " is ", &

end program main

You'll notice I've written a Fortran interface defining a function, timestwo that is a C function (hence the bind(c) appearing in the function's definition.  Also, I've marked the passed argument x as value because Fortran always passes by reference, while C always passes by value.  I can then create a C file that contains:

float timestwo(float x)
    return x * 2.0;

If I add both files to a Simply Fortran project, it will just work, and the output is what you'd expect:

 Times two via C
 Enter a real number: 6.0
     The C language tells me that two times    6.000 is   12.000

I'll have to find some more complicated examples to display.  But the key is using the iso_c_binding module.  Simply Fortran will just accept a C file added to a project, though, so there is no complication from a development environment perspective.

Jeff Armstrong
Approximatrix, LLC

Re: c and c++ project

Anything more you can provide would be most helpful.