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

Linking VF subroutine to VC++ .NET source

kevin_l_johnson
Beginner
2,176 Views
I have createda source code in VC++ .NET and I would like to use legacy Fortran subroutines. Here are the steps I have taken thus far:
1. Successfully compiled VC++ source code without calls to VF subroutines.
2. Under Project Menu, I selected "Add Existing Item to Project" where I selected my legacy VF subroutine.
3. I added the "extern" command -
extern "C" void lsqfit_(float a, float b, int c, int d);
4. I added the call line -
lsqfit_(a,b,c,d);
5. I compiled successfully.
6. When I tried to build the solution, I got the following error -
unresolved external symbol "void __cdecl LSQFIT_(float.......
0 Kudos
13 Replies
Steven_L_Intel1
Employee
2,176 Views
Assuming your Fortran subroutine is called LSQFIT, and you are using CVF, your C++ declaration of it is incorrect. The routine name does not have a trailing underscore, by default the name is upcased and has the stdcall calling convention. You can adjust the C++ code or use the options /iface:cref /names:lowercase /assume:underscore when compiling the Fortran code. I would also question passing the arguments by value to Fortran, which would typically want to see the addresses passed instead.
0 Kudos
kevin_l_johnson
Beginner
2,176 Views

OK...I have modified my calls as you advised above, but I'm still getting the same link error when I build. Here's my modified version -

VC++ source....

extern

"C" void LSQFIT(float*xp, float*fxp, int*nx, int*maxord);

int

main()

{

float fxp[1000], xp[1000];

int maxord[1], nx[1];

*nx = 10;

*maxord = 3;

LSQFIT(xp,fxp,nx,maxord);

......

CVF subroutine....

SUBROUTINE LSQFIT(XP,FXP,NX,MAXORD)

DIMENSION XP(1),FXP(1),COEFF(11),ERR(10)...

0 Kudos
Steven_L_Intel1
Employee
2,176 Views
You didn't account for STDCALL. Add __stdcall to your C++ declaration of the routine.
0 Kudos
kevin_l_johnson
Beginner
2,176 Views
Hey Steve,
I added the __stdcall to the declaration line but to no avail...I'm keep getting the linking error. When I add this subroutine to the project, do I add it as an "Existing Item"?
Thanks,
Kevin
PS - Here's my modified declaration statement -
extern "C" void __stdcall LSQFIT(float*xp, float*fxp, int*nx, int*maxord);
0 Kudos
Steven_L_Intel1
Employee
2,176 Views
I assume you have built the CVF source as a static library project. You would then add the .lib from that build to your VS.NET project as an existing item. You would also have to add the DF98LIB folder to the C++ libraries path.
If you still get an error, what is the exact text of the linker error now? I am sure it has changed.
0 Kudos
kevin_l_johnson
Beginner
2,176 Views

Hey Steve,

Afteradding the df98LIB folder to Additional Library Directories, I clicked on the build button and got a huge error list consisting mostly of "already defined in *.lib" messages. The last line of the error list says -

ClimbFTDRT fatal error LNK1169: one or more multiply defined symbols found

Does this mean there are duplicate library files in the df98 subdirectory and the VC++ library?

Thanks,

Kevin

0 Kudos
Steven_L_Intel1
Employee
2,176 Views
It means you have "Multiple C Library Syndrome", I think (since you didn't give an example of the actual errors, I can't be sure.) Make sure the Fortran library is built with the same library type (static vs. DLL, threaded vs. non-threaded, debug vs. non-debug) as the C++ code.
0 Kudos
kevin_l_johnson
Beginner
2,176 Views

Hey Steve,

Here aresome errors from the last build -

error LNK2005: ___argc already defined in LIBCMTD.lib(crt0dat.obj)

error LNK2005: ___argv already defined in LIBCMTD.lib(crt0dat.obj)

The above errors are the first 2 errors of 31 that are listed.
Kevin
0 Kudos
Steven_L_Intel1
Employee
2,176 Views
See my above reply, and this article.
0 Kudos
kevin_l_johnson
Beginner
2,176 Views

YOU DA MAN!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

My libraries were out of whack. I rebuilt the fortran subroutines using the same run time libraries that I am using in the VC++ source code, then rebuilt the VC++ project, and baddah bing.

Thanks again Steve,

Kevin

0 Kudos
Steven_L_Intel1
Employee
2,176 Views
Great. Now consider migrating to Intel Visual Fortran and it will become somewhat easier...
0 Kudos
kevin_l_johnson
Beginner
2,176 Views
Now you've got my curiosity...what does Intel Visual Fortran do differently to make this process easier?
Kevin
0 Kudos
Steven_L_Intel1
Employee
2,176 Views
You wouldn't have to use two separate development environments and can debug a mixed-language application. You would click "Build Solution", it would build, and the debugger would understand both languages.
With the CVF+VC.NET combination, you have to use two separate IDEs and mixed-language debugging doesn't work.
0 Kudos
Reply