Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

using c library functions

Sirat_S_
Beginner
1,049 Views

I want to call c library function atof from my fortran program. I m using VS 2008. What would be the syntax for such a calling?

Also i have another c program with its .h nd .cpp files. How to call such a program from fortran? Would that be saved as separate VS project ?

Please advice.

0 Kudos
16 Replies
Steven_L_Intel1
Employee
1,049 Views

Why use atof? Just do a Fortran internal READ of the string, with an appropriate format or even *.

For mixed-language applications, the C code needs to be in a separate project in the same solution. Use a Win32 Project > Static Library as the C++ project type. Then right click on the Fortran project, select Project Dependencies > Dependencies and check the box for the C library project to make it a dependent of the Fortran project.

0 Kudos
JVanB
Valued Contributor II
1,049 Views

You can just write out an interface body for atof and then call it like a normal Fortran function. Intel Fortran automatically links to the C library. Read the section in the ifort docs on mixed language programming.

program P
   use ISO_C_BINDING
   implicit none
   interface
      function atof(str) bind(C,name='atof')
         import
         implicit none
         real(C_DOUBLE) atof
         character(kind=C_CHAR) str(*)
      end function atof
   end interface
   character(20,C_CHAR) S
   real(C_DOUBLE) X
   S = '1.37035999173E2'//C_NULL_CHAR
   X = atof(S)
   write(*,*) 'X = ', X
end program P

 

0 Kudos
Sirat_S_
Beginner
1,049 Views

Thanks @ Steve Lionel

Of course !  If only i knew fortran could do that. (I m new to fortran ).

To the second part of ur response, "separate project in the same solution", pls elaborate. 

Thanks @Repeat Offender. 

0 Kudos
Steven_L_Intel1
Employee
1,049 Views

Take a look at the MixedLanguage samples provided, in particular FortranCallsC.

0 Kudos
Sirat_S_
Beginner
1,049 Views

Sure, thanks.

0 Kudos
Sirat_S_
Beginner
1,049 Views

Pls tellwhere to find this doc u referred me to.

0 Kudos
Steven_L_Intel1
Employee
1,049 Views

We provide many sample projects when you install the product. They can be found in ZIP files under C:\Program Files (x86)\IntelSWTools\samples_2016\en\compiler_f\psxe  Unzip MixedLanguage.zip to your desktop or another writable folder and look at the Fortran_Calls_C sample solution.

0 Kudos
Sirat_S_
Beginner
1,049 Views

@SteveLionel thanks for your kind help.

As you hinted in your reply post suggesting me to rather use internal read or write for converting from character to real for example. Applying the same to reading numbers from file, I have literally failed, since it doesnt work. I assume , such a command needs the string to be in a string as to work.

please help

0 Kudos
Steven_L_Intel1
Employee
1,049 Views

How about you show us an example of what you tried, and how you'd like to use atof. Since atof coverts a string to a real, I am confused by your saying that the value "needs to be in a string" and you suggest it isn't.

0 Kudos
Sirat_S_
Beginner
1,049 Views

of course, i realized i should have been more clear, sorry for that.

i am rather quoting the data. it has mixed data for different variables. both character and real etc.

It is required to read this data on the type basis.

[numerical_para]
 CFL= 2.0            ; CFL number.
 eps= 1.0E-6         ; converge criterion.
 maxsteps= 9E6       ; total iterations.
 scheme= ROE         ; spatial scheme type.
 pre= 0              ; preconditioning? 0=NO

 nrt= 10000           ; real-time steps
 drt= 1.0E-5      ; real-time stepsize, nrt*drt ~ 
 substeps= 10,10     ; subiteration limit

[fluid_para]
 gamma= 1.4          ; ¦Ã, specific ratio.
 R= 287.04           ; gas constant.
 Pr= 0.72            ; Prandtl constant.

[mesh_para]
 scale= 1.4606e-4    ; Re=200. used to calculate Reynold number.

[B.C._para]
 threads= 2, 2       ; total number, and ini-from id.
 airfoil-solid       ; sub-section name.
 free-stream         ; sub-section name.

[airfoil-solid]
 uns= 0              ; non-zero indicates real-time steps.
 dim= 1              ; dimensional or not.
 var= 0              ; variable or not.
 TP=  WALL           ; boundary type, for verification.
 p=   101325.0
 u=   0.0            ; for slip C., use: 'u= a*u'
 v=   0.0            
 T=   300.0
 d=   1.22500
 T_n= 0.0            ; dT/dn, for mixed C., use: 'T_n= k*T'

text after the semi colon are comments, pls ignore it.

please advise.

0 Kudos
Steven_L_Intel1
Employee
1,049 Views

Well, that's a lot more complicated than atof. You need quite a bit of code to parse each line, separate out the components, do the conversion of the values and then somehow "assign" to the "variables". It is certainly possible to do this in Fortran, though Fortran is not best suited to a task such as this. I hope you aren't asking us to write all this for you.

0 Kudos
FortranFan
Honored Contributor II
1,049 Views

Sirat S. wrote:

.. i am rather quoting the data. it has mixed data for different variables. both character and real etc. .. please advise.

It might appear the "parameter" data you show is in INI file format (see here: https://en.wikipedia.org/wiki/INI_file).  You can find a primer on such "input data" files getting used for various things, especially with computers, here: https://en.wikipedia.org/wiki/Configuration_file.

If this is what you're really dealing with, then understand that there are various "parsers" available from open-source/freeware or 3rd party commercial libraries that one can use in one's code, instead of trying to "reinvent the wheel".  For example, if your data is indeed in INI file (I don't know, you need to confirm for yourself), then some Fortran library FiNeR such as this one (https://github.com/szaghi/FiNeR/wiki) might do what what you want.  Its author just posted a query on the Intel Linux forum: you can communicate with him https://software.intel.com/en-us/forums/intel-fortran-compiler-for-linux-and-mac-os-x/topic/606354.

 

0 Kudos
Sirat_S_
Beginner
1,049 Views

@Steve Lionel

Thanks Dr Steve. of course I am not hoping u to have this resolved all the way for me . :)))....but at least i am better informed now.

@Fortran Fan

Thanks for your reply. and yes you are spot on. It IS an INI file. I have posted a query to the author of FiNer already. Never thought if would be so tedious a task to get done in FORTRAN compared to the power of MATLAB. But of course, both have their own advntges an disadvntges.

Thanks again.

 

0 Kudos
mecej4
Honored Contributor III
1,049 Views

It is a little odd for a self-proclaimed Fortran neophyte to attempt write a program to parse a moderately structured data file. This small program may help you get started. It reads the value from the fourth line of your input data file (data modified to integer format).

program fatof
implicit none
character(len=100) :: line = 'maxsteps= 9000000'
integer i,maxsteps
!
i=index(line,'=')
if(i == 0)stop 'Error, no equals sign in input line'
read(line(i+1:),*)maxsteps                 ! does the same task as C's atof()
write(*,*)'Value read = ',maxsteps
end program

If you wish to build on it, you will have to add code to ascertain which block of data is being processed, the type of the data item (read the variable name to the left of '=', scan a table of variable names appropriate for the current block and determine the type -- integer, real, string, etc.) and where to put the data. You will also need to check for errors in the input data and print suitable error messages.

Never thought if would be so tedious a task to get done in FORTRAN compared to the power of MATLAB.

Consider the chances of success with writing a Fortran compiler or a word processor in Matlab! The first version of Matlab was written in Fortran. Could Matlab return the favor?

0 Kudos
Sirat_S_
Beginner
1,049 Views

@mecej4

I am absolutely amused by your reply. I am doing a project on CFD, and I am stuck up tailoring up the code so as to run it. So, miserable as i should be, CFD is out of the window and I am rather being a programmer, so I should be amused if i am happy being the laughing stock.

Anyways, Its Visual Studio 8, Intel Fortran 11, and glitches occuring again and again. I am integrating a fortran project with a C++ project, and the result is

Error    18     error LNK2005: __invalid_parameter already defined in LIBCMTD.lib(invarg.obj)    msvcrtd.lib(MSVCR90D.dll)    
Error    19     error LNK2005: __CrtDbgReportW already defined in LIBCMTD.lib(dbgrptw.obj)    msvcrtd.lib(MSVCR90D.dll)    
Error    20     error LNK2005: "public: virtual __thiscall std::exception::~exception(void)" (??1exception@std@@UAE@XZ) already defined in LIBCMTD.lib(stdexcpt.obj)    msvcrtd.lib(MSVCR90D.dll)    
Error    21     error LNK2005: "public: __thiscall std::exception::exception(void)" (??0exception@std@@QAE@XZ) already defined in LIBCMTD.lib(stdexcpt.obj)    msvcrtd.lib(MSVCR90D.dll)    
Error    22     error LNK2005: "public: __thiscall std::exception::exception(class std::exception const &)" (??0exception@std@@QAE@ABV01@@Z) already defined in LIBCMTD.lib(stdexcpt.obj)    msvcrtd.lib(MSVCR90D.dll)    
Error    23     error LNK2005: "public: __thiscall std::exception::exception(char const * const &)" (??0exception@std@@QAE@ABQBD@Z) already defined in LIBCMTD.lib(stdexcpt.obj)    msvcrtd.lib(MSVCR90D.dll)    
Error    25     fatal error LNK1169: one or more multiply defined symbols found    Debug\NACAPROJECT.exe    

I wonder if any learned one can help me out.

 

0 Kudos
mecej4
Honored Contributor III
1,049 Views

All the errors related to "symbol already defined" are caused by the use of different run-time library options (/MDd versus /MTd, or the VS equivalents, i.e, dynamic debugging libraries versus static run-time debugging libraries selected) when various pieces of your projects were compiled.

If you have to use prebuilt libraries/DLLs without source code available, you have to find out which option was used when they were built, and use the same options to compile your sources.

You can also specify the linker option /FORCE:MULTIPLE, but I recommend against it, since using it often promotes compile/link time errors to run-time errors.

Make sure that you specify only one of the two choices, recompile and rebuild. You may also consider using /MD and /MT, i.e., "release mode', until you get the program to build, since the .OBJ and EXE files will be smaller and builds will be faster. Later, if necessary, you can switch back to debug mode.

0 Kudos
Reply