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:

-lstdc++

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

    real::timesval

    interface
        function timestwo(x) bind(c)
        use iso_c_binding
        real(kind=c_float)::timestwo
        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 ", &
                                          timestwo(timesval)

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

Thanks.
Anything more you can provide would be most helpful.

Re: c and c++ project

In your FORTRAN with c code, I replaced timestwo with this

float timestwo(float x)
{
    printf("aaa \n");
    printf("%f \n", x);
    printf("bbb\n");
   
    return x * 2.0;
}

The output ignored the newlines and gave this

aaa 3.000000 bbb

Any thoughts?
K

Re: c and c++ project

I'm guessing that the output is in the internal * Console * tab.  Is that correct?  I'm surprised it's working that way on Windows, but you can probably fix it by adding a carriage return as well:

    printf("aaa \r\n");

I'm wondering, does the code work as expected in an External Console without the carriage return?

Jeff Armstrong
Approximatrix, LLC

Re: c and c++ project

Are the interoperability procedures in this post the same if one only has a *.h file rather than *.c file?  All I know about C language is that *.h is called a header file. 

What I mean by the interoperability procedures is (1) to use iso_c_binding, (2) including the interface, and (3) adding -lstdc++ in the linker for compiler flags in the project options.

Thank you for any assistance you can provide.

Re: c and c++ project

A C header file usually only declares prototypes for C procedures and provides constants.  The availability of the procedures in a header file really depends on what specifically you're talking about.  If you have an accompanying library that corresponds to the C file, then you would just need to create Fortran interfaces based on what the C header declares and link with the corresponding library.

If you're attempting to use functions in the C standard library that are mentioned in C header files, then you don't need to explicitly link to the library.  The Fortran compiler will link to the C standard library regardless.

You only need to add the standard C++ library flag for linking if you're linking with the standard C++ library.  There's a good chance you're not, though.

If you have the accompanying C source files for your header file, you can just add them to your project in Simply Fortran, and they should also be compiled.

To be clear, though, interfacing C routines isn't particularly simple.  I'm providing a very high level overview of what you need.  It could get substantially more complicated quickly if you need to link multiple libraries (or you have to build a C library yourself first...).

Jeff Armstrong
Approximatrix, LLC

Re: c and c++ project

Thank you for the overview.

I can appreciate the difficulties, although the example in this post was relatively simple.

When you say that there is a good chance that I’m not adding the standard C++ library flag for linking, what would that be?

Re: c and c++ project

You only need to link to the standard C++ library if you or your external C/C++ code is using C++.  If you're trying to link to a pure C library, there's no need to add the flag to link to the standard C++ library.  The original post specifically asked about linking to C++, which is why I mentioned it.

Jeff Armstrong
Approximatrix, LLC