- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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 (SUBROUTINE FortSub01)
Step 3: Add another Fortran Static Lib Project (SUBROUTINE 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
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 (SUBROUTINE FortSub01)
Step 3: Add another Fortran Static Lib Project (SUBROUTINE 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
Link Copied
4 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I do have the following in my C file:
#ifdef __cplusplus
extern "C"
#endif
Why would the compiler link one Fortran file only?
#ifdef __cplusplus
extern "C"
#endif
Why would the compiler link one Fortran file only?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Did you supply extern "C" for each Fortran function?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Aha! That does it! It works now :-)
Many Thanks tim18
Many Thanks tim18

Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page