- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi,
I have an issue, if I would like to link an MSVC static library to a Fortran console program, where the linker setting is "Debug Multithreaded (/libs:static /threads /dbglibs)" Or "Multithreaded".
I'm using now
- Intel(R) Visual Fortran Compiler 19.1.3.311 [Intel(R) 64]
- Microsoft Visual Studio Professional 2019
Version 16.11.9
VisualStudio.16.Release/16.11.9+32106.194
Microsoft .NET Framework
Version 4.8.04084
Installed Version: Professional
Visual C++ 2019 00435-20313-01577-AA305
Microsoft Visual C++ 2019
The code is
Fortran:
program FortranMain
implicit none
interface
function myCPPround(dDummy) bind(C, name="myCPPround") result(iDummy)
use iso_c_binding, only: c_int, c_double
implicit none
real(c_double) :: dDummy
integer(c_int) :: iDummy
end function
end interface
integer*4 :: iRes
real*8 :: dVal
dVal=1.98d0
iRes=myCPPround(dVal)
write(*,'(a,g0,a,i0)')"round(",dVal,")=",iRes
end program
Cpp:
// CPPlibrary.cpp : Defines the functions for the static library.
//
#include "pch.h"
#include "framework.h"
#include <cmath>
// TODO: This is an example of a library function
extern "C" {
int myCPPround(double dVal)
{
return((int)std::round(dVal));
}
}
Fortran Project settings
Linker error:
Error error LNK2019: unresolved external symbol __imp_round referenced in function myCPPround CPPlibrary.lib(CPPlibrary.obj)
If I change the linker setting to
then it works.
But I don't want to add a lot of redist DLL from Intel to my program package, so I would like to use the static linking if it is possible.
How could I overcome this problem?
Thank you for your help!
BR:
Gergö
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
At a minimum you also have to set your C++ project setting to build for static libraries. Unlike Intel Fortran, Microsoft C++ generates different code depending on which set of libraries you use. This is under Code Generation in the C++ project. Make sure that this is set to the same value as you have for Fortran.
Microsoft really doesn't want you to use static libraries - I took the generic C++ console application, changed that setting and built - got numerous unresolved symbol errors during the link step.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi Steve,
thank you, I missed this option in C++ settings, thank you!
this solved my problem!
BR:
Gergö
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
This would be a MS VS problem...
you can fix it yourself:
int myCPPround(double dVal)
{
if(dVal >= 0.0)
return((int)dVal);
return(-((int)(-dVal)));
}
The above assumes you get the values you want for 0.5 and -0.5.
Jim Dempsey
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Would this be of use?
! MyRound.f90
module myMath
contains
function myRound(dVal) result(iVal)
implicit none
real(8) :: dVal
integer :: iVal
if(dVal >= 0.0d0) then
iVal = INT(dVal+0.5D0)
else
iVal = INT(dVal-0.5d0)
endif
end function myRound
end module myMath
program MyRoundTest
use myMath
implicit none
integer :: I
real(8) :: R
do I=-20,20
R = DBLE(I) / 10.0D0
print *, R, myRound(R)
end do
end program MyRoundTest
Jim Dempsey
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi Jim,
thanks, but my issue is less the example, but the concept with interop between Intel Fortran and MSVC.
I just generated a small example, where I was able to reconstruct the linker error. The whole project is much more complicated, and the most part (special the Cpp part) is written by another colleagues. I just wanted to use their static library in my Fortran code.
In fortran I can use the nint() instrict function for the rounding purpose
BR:
Gergö
![](/skins/images/C4F7392A5C68ACA60290C9576F107044/responsive_peak/images/icon_anonymous_message.png)
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page