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

Unresolved External with C++ -> Fortran Module

david_gregory
Beginner
870 Views
Hello-

Please be gentle, i'm a fortran programmer with almost no C++ experience. I'm sure this is a very obvious question, but I'm trying to access a fortran module from a .cpp as described in the documentation. I'm unable to even reproduce the simple example without getting unresolved external errors.

i have a fortran static lib project called Lib1 that has a file called source1.f90 with the following:

MODULE EXAMP
REAL A(3)
INTEGER I1, I2
CHARACTER(80) LINE
TYPE MYDATA
SEQUENCE
INTEGER N
CHARACTER(30) INFO
END TYPE MYDATA
END MODULE EXAMP


I have a C++ console app project with a file called tryer.cpp with the following:

#include "stdafx.h"
// C code accessing module data in .cpp file
extern "C" float EXAMP_mp_A[3];
extern "C" int EXAMP_mp_I1, EXAMP_mp_I2;
extern "C" char EXAMP_mp_LINE[80];
extern "C" struct {
int N;
char INFO[30];
} EXAMP_mp_MYDATA;
int _tmain(int argc, _TCHAR* argv[])
{
float foo;
foo = EXAMP_mp_A[0];
return 0;
}


Both projects exist in a solution in VS. The fortran library is marked as a dependancy of the console app.

The compile log looks like this:



Creating temporary file "c:Documents and SettingsDavidMy DocumentsVisual Studio Projects ryerDebugRSP000067.rsp" with contents
[
/Od /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /Gm /EHsc /RTC1 /MLd /Yu"stdafx.h" /Fp"Debug/tryer.pch" /Fo"Debug/" /Fd"Debug/vc70.pdb" /W3 /c /Wp64 /ZI /TP
". ryer.cpp"
]
Creating command line "cl.exe @"c:Documents and SettingsDavidMy DocumentsVisual Studio Projects ryerDebugRSP000067.rsp" /nologo"
Creating temporary file "c:Documents and SettingsDavidMy DocumentsVisual Studio Projects ryerDebugRSP000068.rsp" with contents
[
/OUT:"Debug/tryer.exe" /INCREMENTAL /NOLOGO /DEBUG /PDB:"Debug/tryer.pdb" /SUBSYSTEM:CONSOLE /MACHINE:X86 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib "Documents and SettingsDavidMy DocumentsVisual Studio ProjectsLib1debuglib1.lib"
".debugstdafx.obj"
".debug ryer.obj"
]
Creating command line "link.exe @"c:Documents and SettingsDavidMy DocumentsVisual Studio Projects ryerDebugRSP000068.rsp""
0 Kudos
8 Replies
Lorri_M_Intel
Employee
870 Views
Okay, I admit I tried this from the command line, but I didn't get any unresolved externals.
What did you get? (If you said in your original post, I must have missed it ...)
If the undefines are the _EXAMP_mp_*, do a link /dump /symbols lib1.lib
and make sure they're there, and spelled correctly!
- Lorri
0 Kudos
david_gregory
Beginner
870 Views
The error from the linking is this:

tryer.obj : error LNK2001: unresolved external symbol _EXAMP_mp_A
Debug/tryer.exe : fatal error LNK1120: 1 unresolved externals

the link /dump /symbols for Lib1.lib gives this:

01D 00000004 UNDEF notype External | EXAMP_mp_I2
01E 00000004 UNDEF notype External | EXAMP_mp_I1
01F 0000000C UNDEF notype External | EXAMP_mp_A
020 00000060 UNDEF notype External | EXAMP_mp_LINE


Is there an issue that they don't have the leading "_"?
0 Kudos
Lorri_M_Intel
Employee
870 Views
Yes, I would say the issue was that the Fortran library names are created without the leading underscore.
Good. Now we just need to figure out how it got that way! What was the command used to compile the Fortran files? You can get that from the project options in .NET.
- Lorri
0 Kudos
david_gregory
Beginner
870 Views
/nologo /Zi /Od /module:"$(INTDIR)/" /object:"$(INTDIR)/" /traceback /check:bounds /libs:static /dbglibs /libdir:noauto /c

i got this from propeties->fortran->command line in the window marked "all options".
0 Kudos
Steven_L_Intel1
Employee
870 Views
What is the exact compiler version you are using? There have been bugs in this area in the past.
0 Kudos
david_gregory
Beginner
870 Views
I'm using the latest version i think?

Intel Fortran Compiler for 32-bit applications, Version 8.1 Build 20040802 Z Package ID: w_fc_p_8.1.019

I've been playing the command line and have had no success there as well. What command line options are used normally?

Also, if its of any interest, it appears that module procedures (subroutines and functions) have the leading underscore, while the variables do not.

So, if i add a function to the above module like so:

MODULE EXAMP
REAL A(3)
INTEGER I1, I2
CHARACTER(80) LINE
TYPE MYDATA
SEQUENCE
INTEGER N
CHARACTER(30) INFO
END TYPE MYDATA

CONTAINS

integer function foo ()

foo = 1
end function foo

END MODULE EXAMP

And compile with the following command: ifort -c source1.f90

then link /dump /symbols source1.obj gives me this:

00E 00000000 SECT1 notype () External | _EXAMP
00F 00000002 SECT1 notype () External | _EXAMP_mp_FOO
010 00000004 UNDEF notype External | EXAMP_mp_I2
011 00000004 UNDEF notype External | EXAMP_mp_I1
012 0000000C UNDEF notype External | EXAMP_mp_A
013 00000060 UNDEF notype External | EXAMP_mp_LINE
014 00000000 SECT7 notype Static | .drectve

I'd settle for using the command line but i'm apparently missing something. I'm used to developing in a unix environment, so i'm quite content with gVim as my IDE :)
0 Kudos
Steven_L_Intel1
Employee
870 Views
No, that's an old version. 8.1.029 is the latest.
0 Kudos
david_gregory
Beginner
870 Views
So, i tried .029 and the naming is correct. However, i get a compiler error when trying to compile one of my fortran modules which did compile previously under .019. I tried .023 and everything seems to work fine under debug mode, but not under release (it fails on some of the gui libraries).

In any event this is particular issue is resolved. Thanks for your help.
0 Kudos
Reply