Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

call C from fortran in powerstation v4 required libraries?

Status
Not open for further replies.

monkeycode

Technical User
Jan 24, 2012
7
GB
I am trying a test build of some old code (hence the old compiler). I need to link C and F files, but I am getting the following error

error LNK2001: unresolved external symbol _flow

for each mention of a C file in the F code. Below is an example of how the f files link to the C files.

INTERFACE

SUBROUTINE flow (from, to, when, howmuch)
!MS$ATTRIBUTES C, ALIAS:'_flow' :: flow
REAL from
REAL to
REAL when
REAL howmuch
END SUBROUTINE flow

END INTERFACE


I am struggling to find any suggestions for the compiler I am using, but I suspect that the code is OK (I am assured the model has built from this code previously) and there is something wrong with what I am doing- I am wondering if I don't have the appropriate libraries to tell the compiler I am trying to link to C? I did attempt to download "fortran.h" and add this to my project file but this did not help, and when I added

include 'fortran.h'

to the relevant f files it caused syntax errors.

here is also the fortran.h file I am using

// Definitions for calling FORTRAN 77 from C++

typedef int INTEGER; // INTEGER 4 bytes
typedef float REAL; // REAL 4 bytes
typedef double DOUBLE_PRECISION; // DOUBLE PRECISION 8 bytes
typedef int LOGICAL; // LOGICAL 4 bytes

#include <f77char.h> // character n bytes
#include <f77cmplx.h> // complex
#include <f77matrx.h> // fmatrix class

// values for LOGICAL
#define FALSE 0
#define TRUE 1

// Macros for portable handling of linkage and calling conventions
//#ifdef F77_STUB_REQUIRED
// Typically, this branch is for Unix computers

// C++ stub functions:
#define SUBROUTINE inline void
#define INTEGER_FUNCTION inline INTEGER
#define REAL_FUNCTION inline REAL
#define LOGICAL_FUNCTION inline LOGICAL
#define DOUBLE_PRECISION_FUNCTION inline DOUBLE_PRECISION

// FORTRAN functions
#define SUBROUTINE_F77 extern "C" void
#define INTEGER_FUNCTION_F77 extern "C" int
#define REAL_FUNCTION_F77 extern "C" float
#define LOGICAL_FUNCTION_F77 extern "C" int
#define DOUBLE_PRECISION_FUNCTION_F77 extern "C" double

//#endif

// Array indexing differences between C++ and fortran
//#define B(i) b[i-1]


Thanks for reading- help greatly appreciated, and let me know if more information would be useful!
 
Hhhmmm...it's been a while, but I have compiled C libraries for use from Fortran...don't remember how exactly I did it, but there is something about that underscore that goes in front of the name...

By the way, I did such compilation and calling of C from Fortran in 3 different platforms (Solaris, Red Hat and Windows) with the 'same' compiler in all 3...I used G95.

Having said that, take a look at the compiler options and see what it is that you could turn off or on...something about that underscore.

I seem to recall not following proper procedure and decided to turn the generation of underscore off during the compilation of Fortran code...I also did not do anything special in the C libraries...and things worked fine.

Try your code with and without the underscore in the name of the function; I am talking about actually hardcoding the underscore and making part of the name in the Fortran sources or wherever...try several combinations...

Hope this helps.


 
1) Are you calling Fortran from C or C from Fortran
2) Is the code being called in a DLL?
 
Thanks salgerman,
xwb;
I am trying to call C from fortran, in a DLL


I have managed to fix the unresolved external symbol errors for the c references (I had not told the compiler to link to the correct files, doh!) but am having problems now with some other unresolved externals
__getstdfilex
__max
__min
these are all in my .obj files from c so I assume the problem is what I am doing there - I am using microsoft eMbedded visual C++ and I get this error for max and min;
warning C4013: 'min' undefined; assuming extern returning int
and also
warning C4013: 'perror' undefined; assuming extern returning int
although perror does not come up as an unresolved when I try to link to F....
 
fixed by adding

void perror(const char *message);

to code- my stdio.h file is empty!
 
also fixed max and min, although, I am still working on the

getstdfilex

part of the error.....
 
I've never used CE before. Find it quite strange that stdio.h is empty but if you are using it for EPROMs then I suppose it makes sense not to assume any kind of I/O.
 
I suspect missing bits of library files may be causing my _getstdfilesx error

I do have


// Std handle defns
#define stdin _getstdfilex(0)
#define stdout _getstdfilex(1)
#define stderr _getstdfilex(2)

// functions for general buffered file handling in either ANSI or Wide
_CRTIMP FILE* __cdecl _getstdfilex(int);

but this does not seem to be enough - can anyone tell me all of the library syntax required for this function to work, or direct me to some good resources on the issue- I'm really struggling!
as it doesn't seem to be a problem until I try to link the files to .f .obj files in microsoft developer, I'm wondering if the problem is there?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top