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

Static linking Intel libraries together with MSVC static link

Koczka
Beginner
1,601 Views

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

Koczka_0-1660140066864.png

 

 

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 

Koczka_1-1660140213404.png

 

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ö

 

 

Labels (2)
0 Kudos
5 Replies
Steve_Lionel
Honored Contributor III
1,592 Views

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.

0 Kudos
Koczka
Beginner
1,559 Views

Hi Steve,

 

thank you, I missed this option in C++ settings, thank you!

this solved my problem!

 

BR:

Gergö

 

0 Kudos
jimdempseyatthecove
Honored Contributor III
1,586 Views

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

0 Kudos
jimdempseyatthecove
Honored Contributor III
1,585 Views

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

0 Kudos
Koczka
Beginner
1,559 Views

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ö

 

0 Kudos
Reply