Topic: c and c++ project
Can SF be used as a c and c++ ide?
If so, can you explain how?
For discussions of all Approximatrix products and related software
You are not logged in. Please login or register.
Approximatrix Forums → User Support → c and c++ project
Can SF be used as a c and c++ ide?
If so, can you explain how?
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.
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?
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.
Thanks.
Anything more you can provide would be most helpful.
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
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?
Approximatrix Forums → User Support → c and c++ project
Powered by PunBB, supported by Informer Technologies, Inc.