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

Calling multiple Fortran files from CMain

kulachi
Beginner
804 Views
I have a simple question. I am creating a multiple-project, mixed-language solution. My Main programme is in C++, and it calls a Fortran Static Lib file. This works nice and easy (thanks to this forum where I learned how to do it :-)

The problem comes up when I want the main C file to call two Fortran Static Libs sequentially. That is, I have something like this:

C File: (Master.cpp)
-----------------------------------------
void FortSub01(char *Str, size_t Str_Len);
void FortSub02(char *Str, size_t Str_Len);
int main(int argc, char *argv[])
{
...
FortSub01(recvbuf,sizeof(recvbuf));
FortSub02(recvbuf,sizeof(recvbuf));
...
}

----------------------------------------
Fortran Files:
----------------------------------------
SUBROUTINE FortSub01(String)
CHARACTER(*), INTENT(IN OUT) :: String
...
WRITE(*,*) String
...
END SUBROUTINE FortSub01

---------------------------------------
SUBROUTINE FortSub02(String)
CHARACTER(*), INTENT(IN OUT) :: String
...
WRITE(*,*) String
...
END SUBROUTINE FortSub02

---------------------------------------

The build log goes as follows:
...
...
4>Compiling...
4>Master.cpp
4>Linking...
4>Master.obj : error LNK2019: unresolved external symbol "void __cdecl SUB02(char *,unsigned int)" (?SUB02@@YAXPADI@Z) referenced in function _main
4>D:MFExpExperimentDebugExperiment.exe : fatal error LNK1120: 1 unresolved externals
4>Build log was saved at "file://d:MFExpExperimentExperimentDebugBuildLog.htm"
4>Experiment - 2 error(s), 0 warning(s)
========== Rebuild All: 2 succeeded, 1 failed, 0 skipped ==========


I constructed this Solution as follows:
Step 1: Create a C++ empty project,
Step 2: Add a new Fortran Static Lib project (S
UBROUTINE FortSub01)
Step 3: Add another Fortran Static Lib Project (S
UBROUTINE FortSub02)

If I comment out any one of the function declarations (and sunsequent calls) in the C file, the project builds successfully. Moreover, I can run this scheme by adding FortSub02 as a subroutine for
SUBROUTINE FortSub01. However, this construction is not feasible for my purpose.

I tried my best to hunt down this error. Could someone help, please!

-Kulachi


0 Kudos
4 Replies
TimP
Honored Contributor III
804 Views

You seem to be mixed up between C and C++. Most C compilers will automatically switch to C++ with a source file named with .cpp. Then, without an extern "C" qualifier for the Fortran functions, C++ mangling will break the linkage.
0 Kudos
kulachi
Beginner
804 Views
I do have the following in my C file:

#ifdef __cplusplus
extern "C"
#endif


Why would the compiler link one Fortran file only?








0 Kudos
TimP
Honored Contributor III
804 Views
Did you supply extern "C" for each Fortran function?
0 Kudos
kulachi
Beginner
804 Views
Aha! That does it! It works now :-)

Many Thanks tim18
0 Kudos
Reply