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

VC++ LNK2001 error when using modules

davidgillis
Beginner
620 Views

Hi,

I am trying to use structures/modules to pass data between vc++ and IVF. As a test, I basically cut-and-pasted the example from "using modules in mixed-language programming" in the users guide. To be more precise, I created a win32 console app and then created a fortran static lib as a subproject. I then created a file 'EXAMPLE.f90' and added the module definition

MODULE EXAMP etc.

I then created a second fortran file containing a subroutine using the module, and gave the module vars values. In my c++ project I then declared the module vars as globals, using the extern "C" dec

extern "C" int EXAMP_mp_I1, EXAMP_mp_I2;

This would compile and link fine, but if I then tried to use the vars in my c++ program, i.e.

std::cout << " EXAMP_mp_I1 = " << EXAMP_mp_I1 << std::endl;

I get link errors

mixedTest error LNK2001: unresolved external symbol _EXAMP_mp_I1

I am assuming this is some sort of name-mangling problem (for example, in the "fortran module names and attributes" part of the users guide, it seemed to suggest that there may be an underscore prefix added to the var?) I have been able to call fortran, etc. from c++ in the past (w/out modules) so I'm pretty sure my projects setup is correct (same run-time libs, etc.) Also, if I never actually use the vars, I can 'watch' them in the debugger and the values are correct.

Any suggestions about what I am doing wrong? I am using VS 2003 with IVF 8.1, running on w2k.

Thanks, Dave

0 Kudos
3 Replies
Steven_L_Intel1
Employee
620 Views

I just tried a test case with IVF 9.1and it worked fine. I don't see anything wrong with what you wrote above - all I can think of is that you failed to get the Fortran .obj or .lib from the module's compilation linked in.

Do a "dumpbin -symbols" on the .obj output of the module compile. It may be that older compilers didn't "externalize" module variables that Fortran wouldn't need global symbols for.

0 Kudos
davidgillis
Beginner
620 Views

Hi Steve,

Thank you very much for the reply. I think the module is getting linked in, but the problem (I think) may be that the c side is "decorating" the variable.

The linker error occurs in the c++ object file -- the output is pasted below (modTest is the cpp file, modLib is the fortran subproject, which includes (only) the module file EXAMP.f90). When I do a dumpbin on the 2 object files, the relevant lines are

(in modTest.obj)

01D 00000000 SECT5 notype External | ??_C@_0BA@GONBHIKL@?5EXAMP_mp_I1?5?$DN?5?$AA@ (`string')
01E 00000000 UNDEF notype External | _EXAMP_mp_I1

and in EXAMP.obj

01D 00000004 UNDEF notype External | EXAMP_mp_I1

Any ideas of what I can do to get around this? Thanks again,

Dave

output from the build:

------ Rebuild All started: Project: modLib, Configuration: Debug Win32 ------

Deleting intermediate files and output files for project 'modLib', configuration 'Debug|Win32'.

Compiling with Intel Fortran 8.1...

EXAMP.f90

D:Visual Studio ProjectsTest ProjectsmodTestmodLibEXAMP.f90(6) : Warning: The structure length is not a multiple of its largest element; could create misalignments for arrays of this type. [MYDATA]

Creating library...

Build log written to "file://D:Visual Studio ProjectsTest ProjectsmodTestmodLibDebugBuildLog.txt"

modLib build succeeded.

------ Rebuild All started: Project: modTest, Configuration: Debug Win32 ------

Deleting intermediate files and output files for project 'modTest', configuration 'Debug|Win32'.

Compiling...

stdafx.cpp

Compiling...

modTest.cpp

Linking...

modTest.obj : error LNK2001: unresolved external symbol _EXAMP_mp_I1

Debug/modTest.exe : fatal error LNK1120: 1 unresolved externals

Build log was saved at "file://d:Visual Studio ProjectsTest ProjectsmodTestmodTestDebugBuildLog.htm"

modTest - 2 error(s), 0 warning(s)

---------------------- Done ----------------------

Rebuild All: 1 succeeded, 1 failed, 0 skipped

0 Kudos
Steven_L_Intel1
Employee
620 Views
Ah, the problem is that the old Fortran compiler is not properly decorating the variable name with the leading underscore. If you use a current Fortran compiler it should work.
0 Kudos
Reply