Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
Announcements
FPGA community forums and blogs on community.intel.com are migrating to the new Altera Community and are read-only. For urgent support needs during this transition, please visit the FPGA Design Resources page or contact an Altera Authorized Distributor.

Mixed Language Linking Problem?

sarosh
Beginner
802 Views
Hello,
I'm having trouble compiling a simple mixed-language program that I have created. Using the steps delineated at this link for creating a mixed application:
And combining that with information on how to put Fortran modules ina mixed language program which was located here:
I created the following "simple" program in C++:
-------------------------------------------------------------
#include
extern "C" int EXAMPLE_mp_NUM;
extern "C" __declspec(dllimport) void _stdcall EXAMPLE_mp_SQUARE(int *int_arg);
void main(int argc, char *argv[])
{
EXAMPLE_mp_NUM = 5;
EXAMPLE_mp_SQUARE(&EXAMPLE_mp_NUM);
printf("The squared value is: %d ", EXAMPLE_mp_NUM);
}
----------------------------------------------------------------------
which calls a Fortran subroutine in a Fortran Module:
-------------------------------------------------------------------------
!Fortran part of a DLL Module called by a C Program
MODULE EXAMPLE
!DEC$ ATTRIBUTES DLLEXPORT :: dll_mod
IMPLICIT NONE
INTEGER NUM
CONTAINS
SUBROUTINE SQUARE(NUMBER)
INTEGER NUMBER
NUMBER = NUMBER * NUMBER
WRITE(*, *) NUM
RETURN
END SUBROUTINE SQUARE

END MODULE EXAMPLE
----------------------------------------------------------------------
The error I receive when trying to compile this is the following:
--------------------Configuration: VF_MODdll - Win32 Release--------------------
Linking...
dfor.lib(DFORMAIN.OBJ) : error LNK2001: unresolved external symbol _MAIN__
Release/VF_MODdll.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.
VCC_ModExample.exe - 2 error(s), 0 warning(s)
Does anyone understand why this is happening? I'm using VC++ 6.0 and CVF 6.6
Thanks!
Sarosh
0 Kudos
6 Replies
Jugoslav_Dujic
Valued Contributor II
802 Views
Because you're building fortran .exe instead of fortran .dll.
The simplest solution is torecreate your fortran project for scratch, but this time selecting "Fortran Dynamic Link Library".
Jugoslav
0 Kudos
sarosh
Beginner
802 Views
Hi JugoSlavDujic,
Thanks for the help with that, I appreciate it. I corrected that error, but now I am experiencing another one:
--------------------Configuration: VF_ModDLL - Win32 Release--------------------
Linking...
--------------------Configuration: VC_ModuleExample - Win32 Release--------------------
Build : warning : failed to (or don't know how to) build 'C:Documents and SettingssaroshDesktopVC_ModuleExampleVF_ModDLLReleaseVF_ModDLL.lib'
Linking...
LINK : fatal error LNK1181: cannot open input file ".VF_ModDLLReleaseVF_ModDLL.lib"
Error executing link.exe.
VC_ModuleExample.exe - 1 error(s), 1 warning(s)
Any clue as to what is causing this problem?
Sarosh
PS - Please forgive me if some of these questions seem trivial, but I'm a novice at Windows as well as mixed language programming.
0 Kudos
Jugoslav_Dujic
Valued Contributor II
802 Views

No problem.

As you probably know, the calling .exe must be builtusing .lib file which came out as result of .dll compilation. In this case, there's no .lib file.

The most likely reason is that you did not export anything from the dll. Only symbols (routines, usually) which are explicitly exported from the dll may be called from outside world. The symbols can be exported via !DEC$ATTRIBUTES DLLEXPORT directive or by using .DEF file (see documentation). If nothing is exported, .lib is not produced.

[Tip: you can examine what is exported from a dll using dumpbincommand-line utility with /exports switch, or using more powerful "Dependency Walker" from CVF Program group]

You're exporting non-existing "dll_mod":

!DEC$ ATTRIBUTES DLLEXPORT :: dll_mod

however, your routine is called SQUARE. You should change "dll_mod" to "SQUARE".
By the way, I think the compiler should produce a warning (but it doesn't, at least CVF does not) when trying to import/export non-existing symbols.
Jugoslav
0 Kudos
sarosh
Beginner
802 Views
Hi JugoSlavDujic,
Thanks for the help with the previous problem - I was able to get rid of those messages and problems.
I'm now experiencing a new problem with regards to variables. I followed the naming conventions for variables contained in modules from the links I posted up in my first post, but I receive the following response from the compiler:
--------------------Configuration: VF_ModDLL - Win32 Release--------------------
Compiling Fortran...
C:Documents and SettingssaroshDesktopVC_ModuleExampleVF_ModDLLdll_mod.f90
Linking...
Creating library Release/VF_ModDLL.lib and object Release/VF_ModDLL.exp
--------------------Configuration: VC_ModuleExample - Win32 Release--------------------
Linking...
cmain.obj : error LNK2001: unresolved external symbol _Example_mp_NUM
Release/VC_ModuleExample.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.
VC_ModuleExample.exe - 2 error(s), 0 warning(s)
Any insights into this problem at all? I've tried renaming my variables, adding underscores to the Fortran variable names (and it informs me this is not allowed in Fortran).
Thanks!
Sarosh
0 Kudos
Jugoslav_Dujic
Valued Contributor II
802 Views
Did you dllexport NUM?
Further, the spelling is odd -- the Linker message states "_Example_mp_NUM" (that's supposed to be your C declaration, without leading underscore) but your previous code post says "EXAMPLE_mp_NUM" (which is correct).
By the way, I'm not a fan of dllexporting variables, and I think it should be avoided at any cost. It makes code clumsy. Besides, you don't really need it (you don't need to declare it extern "C" in C++, as a normal local variable would suffice) -- it's already passed through argument-list. I understand this is just a sample, but they could have written it a but more nicer.
In other words, if you remove extern "C" and rename EXAMPLE_mp_NUM to something more sane throughout C++ code, it ought to work.
Jugoslav

Message Edited by JugoslavDujic on 04-05-2004 05:20 PM

0 Kudos
sarosh
Beginner
802 Views

Hi JugoSlavDujic,

Thanks again for your help, my problems on that as well =)

Sarosh

0 Kudos
Reply