Subject: [first aid] C and fortran linked problems of mixed programming ?? !! Author: eyein (eyein) Grade: credit value: 100 belongs to the community: C / C C questions Points: 80 Replies: 12 Time: 2004- 03-19 20: 51: 31z
People have made a C and the Fortran program mixed programming, call the subroutine or function of the Fortran program from C :
The document is as follows:
/ * File cmain.c * /
#include
Extern Int __stdcall fact (int N);
Extern void __stdcall Pythagoras (Float A, Float B, Float * C);
Main ()
{
Float C;
Printf ("Factorial Of 7 IS:% D / N", FACT (7));
Pythagoras (30, 40, & c);
Printf ("Hypotenuse IF SIDES 30, 40 IS:% F / N", C);
}
C file forsubs.for
C Integer * 4 Function Fact (n)
Integer * 4 n [Value]
Integer * 4 I, AMT
AMT = 1
Do I = 1, N
AMT = AMT * I
End DO
Fact = AMT
End
Subroutine Pythagoras (A, B, C)
Real * 4 A [Value]
Real * 4 b [value]
Real * 4 C [Reference]
C = SQRT (A * a b * b)
End
Both files are all complie, but in the VC in the VC, there is an error:
-------------------- Configuration: Forsubs - Win32 Debug --------------------
Linking ...
DFOR.LIB (DFORMAIN.OBJ): Error LNK2001: Unresolved External Symbol _Main__
Debug / Forsubs.exe: Fatal Error LNK1120: 1 Unresolved Externals
Error Executing Link.exe.
Forsubs.exe - 2 Error (s), 0 Warning (s)
I don't know why??
The younger brother looks at MSDN, finds related items:
For Example, THE Simple Console Applications FEATURED IN OTHER ARTICLES IN T
His Family Require No Special Options. The CMAIN Application Can Be CREATED
With the folowing commands:
CL / C CMAIN.C
FL32 CMAIN.OBJ FORSUBS.FOR
The Formain Application Can Be Created with The Following Commands:
CL / C CSUBS.C
FL32 FORMAIN.FOR CMAIN.OBJ
Look at the style is bunding in the form of the command line, but in VC and VF, how can I find it???
Please save me !!!!!!!!
Reply to: Eyein (Eyein) () Reputation: 100 2004-03-19 23: 12: 56Z score: 0
Why don't anyone??
Let's help me to help me !!!!!!! Top
Reply to: BM1408 (When UP Cheng Star, I will continue UP) () Reputation: 87 2004-03-19 23: 54: 47Z score: 0
EXTERN "C" .....
You Can Try!
TOP
Reply to: keiy () () Reputation: 100 2004-03-20 09: 09: 28Z score: 80
FortRan What compiler you are using?
TOP
Reply to: keiy () () Reputation: 100 2004-03-20 10: 29: 12Z score: 0
I use DEV-CPP.
C file forsubs.for
C
Integer * 4 Function Fact (n)
Integer * 4 n
Integer * 4 I, AMT
AMT = 1
Do I = 1, N
AMT = AMT * I
End DO
Fact = AMT
End
Subroutine Pythagoras (A, B, C)
Real * 4 a
Real * 4 B
Real * 4 C
C = SQRT (A * a b * b)
End
/ * File cmain.c * /
#include
EXTERN INT FACT_ (INT * N);
EXTERN VOID PYTHAGORAS_ (Float * a, float * b, float * c);
Main ()
{
INT m = 7;
Float A, B, C;
Printf ("Factorial of 7 IS:% D / N", FACT _ (& M));
A = 30; b = 40;
Pythagoras_ (& A, & B, & C);
Printf ("Hypotenuse IF SIDES 30, 40 IS:% F / N", C);
}
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
E: / Temp> gcc -o h h1.c H.for
E: / TEMP> H
Factorial of 7 IS: 5040
Hypotenuse IF Side 30, 40 IS: 50.000000
E: / TEMP>
TOP
Reply to: keiy () () Reputation: 100 2004-03-20 11: 19: 11Z score: 0
I use your program in the command line, the process is as follows (you may be a compilation option problem)
E: / TEMP> FL32-C Forsubs.for
Microsoft (R) Fortran PowerStation Version 4.00
Copyright (c) Microsoft Corp 1982-1995. All Rights Reserved.
Forsubs.for
E: / TEMP> CMAIN.C FORSUBS.OBJ
Microsoft (R) 32-Bit C / C Optimizing Compiler Version 12.00.8804 for 80x86
Copyright (c) Microsoft Corp 1984-1998. All Rights Reserved.
CMAIN.C
Microsoft (R) Incremental Linker Version 6.00.8447
Copyright (c) Microsoft Corp 1992-1998. All Rights Reserved.
/out:cmain.exe
CMAIN.OBJ
Forsubs.obj
E: / Temp> CMAINFACTORIAL OF 7 IS: 5040
Hypotenuse IF Side 30, 40 IS: 50.000000
E: / TEMP>
TOP
Reply to: keiy () () Reputation: 100 2004-03-20 11: 33: 03Z score: 0
I watched your compilation process again, you are compilation order!
Because there is main () in C, it must be used as the main program,
Compilation for Forsubs.for
Then ed the CMAIN.C
Use my process, definitely no problem
(I haven't programmed it for a long time, especially Fortran, I will communicate more in the future!)
TOP
Reply to: Eyein () Reputation: 100 2004-03-20 13: 12: 54Z Score: 0
Command line compile?
I follow your compilation method as follows:
G: / cfd> fl32 -c usecom.f
Compaq Visual Fortran Optimizing Compiler Version 6.5
Copyright 2000 Compaq Computer Corp. All Rights Reserved.
Usecom.f
G: / cfd> CL CMAIN.C Usecom.Obj
Microsoft (R) 32-Bit C / C Optimizing Compiler Version 12.00.8168 for 80x86
Copyright (c) Microsoft Corp 1984-1998. All Rights Reserved.
CMAIN.C
Microsoft (R) Incremental Linker Version 6.00.8168
Copyright (c) Microsoft Corp 1992-1998. All Rights Reserved.
/out:cmain.exe
CMAIN.OBJ
Usecom.obj
Link: Fatal Error LNK1104: Cannot Open File "Dfor.Lib"
How can I appear 'DFOR.LIB'???
Search this file, found in E: / Program Files / Microsoft Visual Studio / DF98 / LIB
How can I not find it??
TOP
Reply to: Eyein (Eyein) () Reputation: 100 2004-03-20 13: 22: 36Z Score: 0
I use COMPAQ VF, is this reason?
If you don't have to be compiled separately, can you do building directly in the VC?
TOP
Reply to: Eyein (Eyein) () Reputation: 100 2004-03-20 13: 43: 15Z Score: 0
Now use the command line to compile as follows: (pass)
G: / cfd> fl32 -c forsubs.for
Compaq Visual Fortran Optimizing Compiler Version 6.5
Copyright 2000 Compaq Computer Corp. All Rights Reserved.
Forsubs.for
G: / cfd> CL CMain.c Forsubs.obj
Microsoft (R) 32-Bit C / C Optimizing Compiler Version 12.00.8168 for 80x86
Copyright (c) Microsoft Corp 1984-1998. All Rights Reserved.
CMAIN.C
Microsoft (R) Incremental Linker Version 6.00.8168
Copyright (c) Microsoft Corp 1992-1998. All Rights Reserved.
/out:cmain.exe
CMAIN.OBJ
Forsubs.obj
G: / cfd> cmain.exe
Factorial of 7 IS: 5040
Hypotenuse IF Side 30, 40 IS: 50.000000
It will illustrate that VF with COMPAQ is a mixed by VC with MS.
But: I wrote a way to use public data blocks between C and FortRan: (the code is as follows :)
//UseCommain.cpp
#include
#include
Struct ilk_type {
Float P;
Float Q;
Float R;
}
Extern ILK_TYPE ILK_;
Extern "C" {void _stdcall usecom_ (int);}
MAIN () {
Char * String = "ABC0";
INT count = 3;
ILK_.p = 1.0;
ILK_.Q = 2.0;
ILK_.R = 3.0;
Usecom_ (count);
Printf ("ILK_.p =% 4.1f, ILK_.Q =% 4.1f, ILK_.R =% 4.1f / N",
ILK_.p, ILK_.Q, ILK_.R);
Return 0;
}
? Usecom.f
Subroutine Usecom (n)
INTEGER N
REAL U, V, W
Common / ILK / U, V, W
N = 3
U = 7.0
v = 8.0
W = 9.0
Return
End subroutine usecom
The result is compiled with the command line:
G: / cfd> fl32 -c usecom.f
Compaq Visual Fortran Optimizing Compiler Version 6.5
Copyright 2000 Compaq Computer Corp. All Rights Reserved.
Usecom.f
G: / cfd> CL UseCommain.cpp Usecom.obj
Microsoft (R) 32-Bit C / C Optimizing Compiler Version 12.00.8168 for 80x86
Copyright (c) Microsoft Corp 1984-1998. All Rights Reserved.
UseCommain.cpp
Microsoft (R) Incremental Linker Version 6.00.8168
Copyright (c) Microsoft Corp 1992-1998. All Rights Reserved.
/out:UseCommain.exe
UseCommain.obj
Usecom.obj
Usecommain.obj: error lnk2001: unresolved external symbol _usecom_ @ 4
Usecommain.obj: error lnk2001: Unresolved External Symbol "Struct ILK_TYPE ILK
_ "(? ilk _ @@ 3uilk_type @@ a)
Usecommain.exe: Fatal Error LNK1120: 2 Unresolved Externals
Is there a problem with my C interface??
Keiy (), help me see ~~, thank you ~~
TOP
Reply to: keiy () () reputation: 100 2004-03-20 19: 24: 46Z score: 01. Back plus _ only GCC must, VC is not
2. Fortran generated is uppercase, so it is also capitalized in C.
3. The structure also uses Extern "C"
The complete procedure is as follows:
//UseCommain.cpp
#include
#include
Struct ilk_type {
Float P;
Float Q;
Float R;
}
Extern "C" {struct ilk_type ilk;}
Extern "C" {void _stdcall usecom (int ";}
MAIN () {
Char * String = "ABC0";
INT count = 3;
ILK.P = 1.0;
ILK.Q = 2.0;
ILK.R = 3.0;
Usecom (count);
Printf ("ILK.P =% 4.1f, ILK.Q =% 4.1f, ILK.R =% 4.1f / N",
ILK.P, ILK.Q, ILK.R);
Return 0;
}
--------------------------------
C usecom.f
Subroutine Usecom (N)
INTEGER N
REAL U, V, W
Common / ILK / U, V, W
N = 3
U = 7.0
v = 8.0
W = 9.0
Return
End subroutine usecom
------------------------------------------------
Compilation process and operation results:
D: / Temp> FL32-C usecom.f
Microsoft (R) Fortran PowerStation Version 4.00
Copyright (c) Microsoft Corp 1982-1995. All Rights Reserved.
Usecom.f
D: / Temp> CL UseCommain.cpp Usecom.obj
Microsoft (R) 32-Bit C / C Optimizing Compiler Version 12.00.8804 for 80x86
Copyright (c) Microsoft Corp 1984-1998. All Rights Reserved.
UseCommain.cpp
Microsoft (R) Incremental Linker Version 6.00.8447
Copyright (c) Microsoft Corp 1992-1998. All Rights Reserved.
/out:UseCommain.exe
UseCommain.obj
Usecom.obj
D: / Temp> Usecommain
ILK.P = 7.0, ilk.q = 8.0, ilk.r = 9.0
D: / Temp>
-------------------------------
In addition, I use it.
Microsoft (R) Fortran PowerStation Version 4.00
It is
Compaq Visual Fortran Optimizing Compiler Version 6.5
It should be compatible, if not. You use
Microsoft (R) Fortran PowerStation Version 4.00 (on the Internet)
TOP
Reply to: Eyein (Eyein) () Reputation: 100 2004-03-21 15: 05-03-21 15: 05: 19Z score: 0 wow ~~, thank you for falling ~~~~~~~~~
Now the problem is solved ~~
Thank you very much ~, I will learn more from you ~~ ^ _ ^ ~~
TOP