How to include Intel MKL in Abaqus subroutine - fortran

I am trying to include the in-build subroutines of Intel Math Kernel Library in ABAQUS UMAT.
I included 'mkl.fi' but it is showing the error as shown below (It is ABAQUS generated job file):
Abaqus JOB Job-1
Abaqus 6.13-1
Begin Compiling Abaqus/Standard User Subroutines
3/8/2017 6:14:23 PM
Intel(R) Visual Fortran Intel(R) 64 Compiler for applications running on
Intel(R) 64, Version 17.0.1.143 Build 20161005
Copyright (C) 1985-2016 Intel Corporation. All rights reserved.
C:\Program Files (x86)\IntelSWTools\compilers_and_libraries_2017.1.143\windows\mkl\include\MKL_lapack.FI(29): error #5082: Syntax error, found '&' when expecting one of: * <IDENTIFIER>
SUBROUTINE CGEJSV( JOBA, JOBU, JOBV, JOBR, JOBT, JOBP, &
------------------------------------------------------------------------^
C:\Program Files (x86)\IntelSWTools\compilers_and_libraries_2017.1.143\windows\mkl\include\MKL_lapack.FI(30): error #5082: Syntax error, found '&' when expecting one of: * <IDENTIFIER>
& M, N, A, LDA, SVA, U, LDU, V, LDV, &
------------------------------------------------------------------------^
C:\Program Files (x86)\IntelSWTools\compilers_and_libraries_2017.1.143\windows\mkl\include\MKL_lapack.FI(34): error #5082: Syntax error, found '&' when expecting one of: <IDENTIFIER> %FILL
COMPLEX A( LDA, * ), U( LDU, * ), V( LDV, * ), &
------------------------------------------------------------------------^
C:\Program Files (x86)\IntelSWTools\compilers_and_libraries_2017.1.143\windows\mkl\include\MKL_lapack.FI(43): error #5082: Syntax error, found '&' when expecting one of: * <IDENTIFIER>
SUBROUTINE ZGEJSV( JOBA, JOBU, JOBV, JOBR, JOBT, JOBP, &
------------------------------------------------------------------------^
C:\Program Files (x86)\IntelSWTools\compilers_and_libraries_2017.1.143\windows\mkl\include\MKL_lapack.FI(44): error #5082: Syntax error, found '&' when expecting one of: * <IDENTIFIER>
& M, N, A, LDA, SVA, U, LDU, V, LDV, &
------------------------------------------------------------------------^
C:\Program Files (x86)\IntelSWTools\compilers_and_libraries_2017.1.143\windows\mkl\include\MKL_lapack.FI(48): error #5082: Syntax error, found '&' when expecting one of: <IDENTIFIER> %FILL
DOUBLE COMPLEX A( LDA, * ), U( LDU, * ), V( LDV, * ), &
------------------------------------------------------------------------^
C:\Program Files (x86)\IntelSWTools\compilers_and_libraries_2017.1.143\windows\mkl\include\MKL_lapack.FI(57): error #5082: Syntax error, found '&' when expecting one of: * <IDENTIFIER>
SUBROUTINE DGESVDX( JOBU, JOBVT, RANGE, M, N, A, LDA, VL, VU, &
------------------------------------------------------------------------^
C:\Program Files (x86)\IntelSWTools\compilers_and_libraries_2017.1.143\windows\mkl\include\MKL_lapack.FI(58): error #5082: Syntax error, found '&' when expecting one of: * <IDENTIFIER>
& IL, IU, NS, S, U, LDU, VT, LDVT, WORK, &
------------------------------------------------------------------------^
C:\Program Files (x86)\IntelSWTools\compilers_and_libraries_2017.1.143\windows\mkl\include\MKL_lapack.FI(64): error #5082: Syntax error, found '&' when expecting one of: <IDENTIFIER> %FILL
DOUBLE PRECISION A( LDA, * ), S( * ), U( LDU, * ), &
------------------------------------------------------------------------^
C:\Program Files (x86)\IntelSWTools\compilers_and_libraries_2017.1.143\windows\mkl\include\MKL_lapack.FI(70): error #5082: Syntax error, found '&' when expecting one of: * <IDENTIFIER>
SUBROUTINE SGESVDX( JOBU, JOBVT, RANGE, M, N, A, LDA, VL, VU, &
------------------------------------------------------------------------^
C:\Program Files (x86)\IntelSWTools\compilers_and_libraries_2017.1.143\windows\mkl\include\MKL_lapack.FI(71): error #5082: Syntax error, found '&' when expecting one of: * <IDENTIFIER>
& IL, IU, NS, S, U, LDU, VT, LDVT, WORK, &
------------------------------------------------------------------------^
C:\Program Files (x86)\IntelSWTools\compilers_and_libraries_2017.1.143\windows\mkl\include\MKL_lapack.FI(77): error #5082: Syntax error, found '&' when expecting one of: <IDENTIFIER> %FILL
REAL A( LDA, * ), S( * ), U( LDU, * ), &
------------------------------------------------------------------------^
C:\Program Files (x86)\IntelSWTools\compilers_and_libraries_2017.1.143\windows\mkl\include\MKL_lapack.FI(83): error #5082: Syntax error, found '&' when expecting one of: * <IDENTIFIER>
SUBROUTINE CGESVDX( JOBU, JOBVT, RANGE, M, N, A, LDA, VL, VU, &
------------------------------------------------------------------------^
C:\Program Files (x86)\IntelSWTools\compilers_and_libraries_2017.1.143\windows\mkl\include\MKL_lapack.FI(84): error #5082: Syntax error, found '&' when expecting one of: * <IDENTIFIER>
& IL, IU, NS, S, U, LDU, VT, LDVT, WORK, &
------------------------------------------------------------------------^
C:\Program Files (x86)\IntelSWTools\compilers_and_libraries_2017.1.143\windows\mkl\include\MKL_lapack.FI(91): error #5082: Syntax error, found '&' when expecting one of: <IDENTIFIER> %FILL
COMPLEX A( LDA, * ), U( LDU, * ), VT( LDVT, * ), &
------------------------------------------------------------------------^
C:\Program Files (x86)\IntelSWTools\compilers_and_libraries_2017.1.143\windows\mkl\include\MKL_lapack.FI(97): error #5082: Syntax error, found '&' when expecting one of: * <IDENTIFIER>
SUBROUTINE ZGESVDX( JOBU, JOBVT, RANGE, M, N, A, LDA, VL, VU, &
------------------------------------------------------------------------^
C:\Program Files (x86)\IntelSWTools\compilers_and_libraries_2017.1.143\windows\mkl\include\MKL_lapack.FI(98): error #5082: Syntax error, found '&' when expecting one of: * <IDENTIFIER>
& IL, IU, NS, S, U, LDU, VT, LDVT, WORK, &
------------------------------------------------------------------------^
C:\Program Files (x86)\IntelSWTools\compilers_and_libraries_2017.1.143\windows\mkl\include\MKL_lapack.FI(105): error #5082: Syntax error, found '&' when expecting one of: <IDENTIFIER> %FILL
COMPLEX*16 A( LDA, * ), U( LDU, * ), VT( LDVT, * ), &
------------------------------------------------------------------------^
C:\Program Files (x86)\IntelSWTools\compilers_and_libraries_2017.1.143\windows\mkl\include\MKL_lapack.FI(111): error #5082: Syntax error, found '&' when expecting one of: * <IDENTIFIER>
SUBROUTINE CGESVJ( JOBA, JOBU, JOBV, M, N, A, LDA, SVA, MV, V, &
------------------------------------------------------------------------^
C:\Program Files (x86)\IntelSWTools\compilers_and_libraries_2017.1.143\windows\mkl\include\MKL_lapack.FI(121): error #5082: Syntax error, found '&' when expecting one of: * <IDENTIFIER>
SUBROUTINE ZGESVJ( JOBA, JOBU, JOBV, M, N, A, LDA, SVA, MV, V, &
------------------------------------------------------------------------^
C:\Program Files (x86)\IntelSWTools\compilers_and_libraries_2017.1.143\windows\mkl\include\MKL_lapack.FI(131): error #5082: Syntax error, found '&' when expecting one of: * <IDENTIFIER>
SUBROUTINE DGGSVD3( JOBU, JOBV, JOBQ, M, N, P, K, L, A, LDA, B, &
------------------------------------------------------------------------^
C:\Program Files (x86)\IntelSWTools\compilers_and_libraries_2017.1.143\windows\mkl\include\MKL_lapack.FI(132): error #5082: Syntax error, found '&' when expecting one of: * <IDENTIFIER>
& LDB, ALPHA, BETA, U, LDU, V, LDV, Q, LDQ, WORK,&
------------------------------------------------------------------------^
C:\Program Files (x86)\IntelSWTools\compilers_and_libraries_2017.1.143\windows\mkl\include\MKL_lapack.FI(135): error #5082: Syntax error, found '&' when expecting one of: <IDENTIFIER> %FILL
INTEGER INFO, K, L, LDA, LDB, LDQ, LDU, LDV, M, N, P, &
------------------------------------------------------------------------^
C:\Program Files (x86)\IntelSWTools\compilers_and_libraries_2017.1.143\windows\mkl\include\MKL_lapack.FI(138): error #5082: Syntax error, found '&' when expecting one of: <IDENTIFIER> %FILL
DOUBLE PRECISION A( LDA, * ), ALPHA( * ), B( LDB, * ), &
------------------------------------------------------------------------^
C:\Program Files (x86)\IntelSWTools\compilers_and_libraries_2017.1.143\windows\mkl\include\MKL_lapack.FI(139): error #5082: Syntax error, found '&' when expecting one of: <IDENTIFIER> %FILL
& BETA( * ), Q( LDQ, * ), U( LDU, * ), &
------------------------------------------------------------------------^
C:\Program Files (x86)\IntelSWTools\compilers_and_libraries_2017.1.143\windows\mkl\include\MKL_lapack.FI(145): error #5082: Syntax error, found '&' when expecting one of: * <IDENTIFIER>
SUBROUTINE SGGSVD3( JOBU, JOBV, JOBQ, M, N, P, K, L, A, LDA, B, &
------------------------------------------------------------------------^
C:\Program Files (x86)\IntelSWTools\compilers_and_libraries_2017.1.143\windows\mkl\include\MKL_lapack.FI(146): error #5082: Syntax error, found '&' when expecting one of: * <IDENTIFIER>
& LDB, ALPHA, BETA, U, LDU, V, LDV, Q, LDQ, WORK,&
------------------------------------------------------------------------^
C:\Program Files (x86)\IntelSWTools\compilers_and_libraries_2017.1.143\windows\mkl\include\MKL_lapack.FI(149): error #5082: Syntax error, found '&' when expecting one of: <IDENTIFIER> %FILL
INTEGER INFO, K, L, LDA, LDB, LDQ, LDU, LDV, M, N, P, &
------------------------------------------------------------------------^
C:\Program Files (x86)\IntelSWTools\compilers_and_libraries_2017.1.143\windows\mkl\include\MKL_lapack.FI(152): error #5082: Syntax error, found '&' when expecting one of: <IDENTIFIER> %FILL
REAL A( LDA, * ), ALPHA( * ), B( LDB, * ), &
------------------------------------------------------------------------^
C:\Program Files (x86)\IntelSWTools\compilers_and_libraries_2017.1.143\windows\mkl\include\MKL_lapack.FI(153): error #5082: Syntax error, found '&' when expecting one of: <IDENTIFIER> %FILL
& BETA( * ), Q( LDQ, * ), U( LDU, * ), &
------------------------------------------------------------------------^
AFEM.for(170): catastrophic error: Too many errors, exiting
compilation aborted for AFEM.for (code 1)
Abaqus Error: Problem during compilation - D:\Google Drive\Friday_May_27_2016\Mtech Thesis Job\All new work\TA\AFEM.for
Abaqus/Analysis exited with errors
In case when MKL library is not included, job file shows the following errors:
----------
Abaqus JOB Job-1
Abaqus 6.13-1
Abaqus License Manager checked out the following licenses:
Begin Compiling Abaqus/Standard User Subroutines
3/8/2017 6:18:48 PM
Intel(R) Visual Fortran Intel(R) 64 Compiler for applications running on Intel(R) 64, Version 17.0.1.143 Build 20161005
Copyright (C) 1985-2016 Intel Corporation. All rights reserved.
End Compiling Abaqus/Standard User Subroutines
Begin Linking Abaqus/Standard User Subroutines
Creating library standardU.lib and object standardU.exp
AFEM.obj : error LNK2019: unresolved external symbol dgemm referenced in function umat.R
standardU.dll : fatal error LNK1120: 1 unresolved externals
Abaqus Error: Problem during linking - Abaqus/Standard User Subroutines.
This error may be due to a mismatch in the Abaqus user subroutine arguments.
These arguments sometimes change from release to release, so user subroutines
used with a previous release of Abaqus may need to be adjusted.
Abaqus/Analysis exited with errors
The source code, when compiled outside ABAQUS, works fine with no errors.
If I am not able to include this lapack library in ABAQUS UMAT subroutine, I may have to append lapack source code in my code itself. That may invite other problems.
So is there any way to compile these lapack subroutines in ABAQUS successfully.
Note that ABAQUS User subroutine (UMAT) is written in '.for' format.
I have included the UMAT (ABAQUS subroutine) for reference. It has 'mul_big_mat' (that stands for multiplying big matrices) subroutine that uses lapack 'dgemm' subroutine.
C================================================================
SUBROUTINE UMAT(STRESS,STATEV,DDSDDE,SSE,SPD,SCD,
1RPL,DDSDDT,DRPLDE,DRPLDT,STRAN,DSTRAN,
2TIME,DTIME,TEMP,DTEMP,PREDEF,DPRED,MATERL,NDI,NSHR,NTENS,
3NSTATV,PROPS,NPROPS,COORDS,DROT,PNEWDT,CELENT,
4DFGRD0,DFGRD1,NOEL,NPT,KSLAY,KSPT,KSTEP,KINC)
C
INCLUDE 'ABA_PARAM.INC'
C
CHARACTER*80 MATERL
DIMENSION STRESS(NTENS),STATEV(NSTATV),
1DDSDDE(NTENS,NTENS),DDSDDT(NTENS),DRPLDE(NTENS),
2STRAN(NTENS),DSTRAN(NTENS),TIME(2),PREDEF(1),DPRED(1),
3PROPS(NPROPS),COORDS(3),DROT(3,3),
4DFGRD0(3,3),DFGRD1(3,3)
C
DIMENSION EELAS(6),EPLAS(6),FLOW(6)
PARAMETER (ONE=1.0D0,TWO=2.0D0,THREE=3.0D0,SIX=6.0D0)
DATA NEWTON,TOLER/10,1.D-6/
C
C ============================================================================
C UMAT FOR 1D BAR
C
============================================================================
C Note :- This UMAT can only be used by 1D elements
C
C Number of material properties, nprops = 1
C props(1) Young's modulus in x direction
C ---------------------------------------------------------------------
C
! --- local variables
DOUBLE PRECISION E
DOUBLE PRECISION C(ntens, ntens)
integer z
!***********************************************************************
! *** Get Elastic properties
E = props(1)
write(*,*)''
write(*,*) z
write(*,*)''
! *** Elastic stiffness Cijkl *** !
C(1,1) = E
!call hello_world3
CALL mul_big_mat
! *** calculate the stress *** !
stress(1) = C(1,1)*(stran(1) + dstran(1))
! Updating Jacobian matrix
ddsdde(1,1) = C(1,1)
return
END
subroutine hello_world3
implicit none
character*32 text
c
write(*,*)'Hello World'
c
END
subroutine mul_big_mat
c
c
integer m, n, k, i, j
parameter (m=100, k= 200, n=100)
double precision a(m,k), b(k,n), c(m,n), alpha, beta
alpha=1.0
beta=0.0
c matrix a declaration
do i=1,m
do j=1,k
a(i,j)=i+j
enddo
enddo
write(*,*) a(100,200)
c matrix b declaration
do i=1,k
do j=1,n
b(i,j)=i-j
enddo
enddo
write(*,*) b(200,100)
c matrix c declaration
do i=1,m
do j=1,n
c(i,j)=0.0
enddo
enddo
call dgemm('n','n',m,n,k,alpha,a,m,b,k,beta,c,m)
! THIS IS THE SUBROUTINE I WANT TO USE FROM INTEL MKL LIBRARY
! I COULD USE MATMUL (AND THAT WORKS FINE), BUT ITS JUST AN EXAMPLE
! I HAVE TO USE 'DGETRI' AND MANY MORE
write(*,*) c(1,1)
do i=1,m
do j=1,n
write(*,*) ' '
write(*,*) c(i,j)
enddo
enddo
END

It looks like you are compiling a free-form (.f90) file as a fixed-form (old FORTRAN 77 style) file. You cannot use & at the and of the line in fixed form .f or .for file.
Your source file is a .for file (AFEM.for) sou your code should be in the fixed form. You can try whether ABAQUS supports the -free compiler flag to force free-form. If it doesn't you must use the fixed form everywhere.
Be careful not to mix parts of fixed-form and free-form in one file. The file MKL_lapack.FI seems not to be compatible with your original source. You unfortunately do not show how you are including this.
HOWEVER, you should not really need to include the include file at all! You should just link with the right library. Use the Intel MKL Link Line Advisor https://software.intel.com/en-us/articles/intel-mkl-link-line-advisor to derive the correct linker flags.

Related

Fortran coding for the density of states of linear lattice

The written code contains some errors. I am using Visual Studio 2012 and the Intel.Visual.Fortran.Composer.XE.2013.0.089 compiler.
This is the code I wrote
PROGRAM DensityOfState
IMPLICIT NONE
INTEGER :: omega , t0 , a0 ,kx
omega=1 ; t0 =1 ; a0=1
REAL :: kx , eta , w , ek
eta=0.0015
REAL,PARAMETER :: pi=3.14
COMPLEX , PARAMETER ::i=(0,1)
COMPLEX :: Energy
COMPLEX :: Density
COMPLEX :: GreenFunc
LoopEnergy : DO w=-2,+2,0.001
WRITE(*,*) Energy= w +( i * eta)
LoopWaveVector : DO kx=-1000,1000
WRITE(*,*) ek= 2*t0*COS(kx*a0)
WRITE(*,*) GreenFunc= Energy - ek
END DO LoopEnergy
END DO LoopWaveVector
WRITE(*,*) IMAG(GreenFunc)
Density= (1.0/pi*omega)*IMAG(GreenFunc)
WRITE(*,*) Density
END PROGRAM DensityOfState
And this is a list of errors
Error 1 error #5082: Syntax error, found IDENTIFIER 'ENERGY' when expecting one of: ( % [ . =
1>------ Build started: Project: Console3, Configuration: Debug Win32 ------
1>Compiling with Intel(R) Visual Fortran Compiler XE 13.0.0.089 [IA-32]...
1>Source1.f90
error #5082: Syntax error, found IDENTIFIER 'ENERGY' when expecting one of: ( % [ . = =>
error #5082: Syntax error, found IDENTIFIER 'EK' when expecting one of: ( % [ . = =>
error #5082: Syntax error, found IDENTIFIER 'GREENFUNC' when expecting one of: ( % [ . = =>
error #6236: A specification statement cannot appear in the executable section.
error #6236: A specification statement cannot appear in the executable section.
error #6236: A specification statement cannot appear in the executable section.
error #6236: A specification statement cannot appear in the executable section.
error #6236: A specification statement cannot appear in the executable section.
error #6236: A specification statement cannot appear in the executable section.
error #6404: This name does not have a type, and must have an explicit type. [ETA]
error #6404: This name does not have a type, and must have an explicit type. [W]
error #6063: An INTEGER or REAL data type is required in this context. [W]
warning #6041: The increment in a DO is zero or evaluates to zero. [0.001]
error #6458: This name must be the name of a variable with a derived type (structure type) [WRITE]
error #6404: This name does not have a type, and must have an explicit type. [I]
error #6458: This name must be the name of a variable with a derived type (structure type) [WRITE]
warning #7319: This argument's data type is incompatible with this intrinsic procedure; procedure assumed EXTERNAL. [COS]
1 error #6404: This name does not have a type, and must have an explicit type. [COS]
error #6458: This name must be the name of a variable with a derived type (structure type) [WRITE]
error #6404: This name does not have a type, and must have an explicit type. [ENERGY]
error #6404: This name does not have a type, and must have an explicit type. [EK]
error #6606: The block construct names must match, and they do not. [LOOPENERGY]
error #6606: The block construct names must match, and they do not. [LOOPWAVEVECTOR]
error #6404: This name does not have a type, and must have an explicit type. [GREENFUNC]
warning #7319: This argument's data type is incompatible with this intrinsic procedure; procedure assumed EXTERNAL. [IMAG]
error #6404: This name does not have a type, and must have an explicit type. [IMAG]
error #6404: This name does not have a type, and must have an explicit type. [DENSITY]
error #6404: This name does not have a type, and must have an explicit type. [PI]
1>compilation aborted for c:\users\mahdi\documents\visual studio 2012\Projects\Console3\Console3\Source1.f90 (code 1)
WRITE(*,*) Energy= w +( i * eta)
is not a correct Fortran statement. To assign w +( i * eta) to Energy, use just
Energy= w +( i * eta)
To print the value of Energy, do just
WRITE(*,*) Energy

Fortran Call Errors

In my Fortran code, I have the commands:
CALL EXPECTATION(X, 0.5D0*SIGMAZ, Z_EXPECTATION)
WRITE(1,*) T, Z_EXPECTATION
CALL EXPECTATION(X, 0.5D0*SIGMAY, Y_EXPECTATION)
WRITE(4,*) T, Y_EXPECTATION
for which I get the errors:
Error: Symbol ‘sigmay’ at (1) has no IMPLICIT type
Error: Symbol ‘sigmaz’ at (1) has no IMPLICIT type
But I get no error for a similar command:
CALL EXPECTATION(X, 0.5D0*SIGMAX, X_EXPECTATION)
WRITE(3,*) T, X_EXPECTATION
What could be the issue? I am calling the variables from the same subroutine and I have defined them to be
COMPLEX*16, DIMENSION(2,2) :: SIGMAX, SIGMAY SIGMAZ
You forgot a comma in the declaration between SIGMAY and SIGMAZ. Since Fortran doesn't care about whitespaces, it created only two complex variables: SIGMAX and SIGMAYSIGMAZ.
Just add the comma and your code should compile fine.

Is this "may be used uninitialized" warning bogus?

When I compile this silly fortran routine:
SUBROUTINE MY (C,M,NA,A,NB,B,N,V,I1,I2)
IMPLICIT NONE
INTEGER :: M, NA, NB, N, I, J, I1, I2
REAL :: C, A(NA,M), B(NB,M), V(N), X
IF(M.GT.15) THEN
DO I=1,N
X=C*V(I)
CALL DAXPY(M,X,A(I1,1),NA,B(I2,1),NB)
END DO
ELSE
DO I=1,N
X=C*V(I)
DO J=1,M
B(I2,J)=B(I2,J)+A(I1,J)
END DO
END DO
END IF
END SUBROUTINE MY
with gfortran -fsanitize=address -O2 -Wall -c a.f90 (gcc version 4.8.4) I get this warning:
a.f90: In function ‘my’:
a.f90:4:0: warning: ‘x’ may be used uninitialized in this function [-Wmaybe-uninitialized]
REAL :: C, A(NA,M), B(NB,M), V(N), X
^
and I can't see how x could ever be uninitialized (only if N < 1, but then it's never used either). The warning goes away without -fsanitize=address or -O2. Is any of these options changing my code in some "unsafe" way (such that the warning is issued)? Do more recent gfortran version give the same message?

Fortran C binding -Linking errror LNK2019: unresolved external symbol

I have a Fortran code that has to call the Clipper Library, for which I downloaded and I compile the original C++ source code. I have written a wrapping subroutine in C++ that reads:
extern "C" void wrapUNIintersNM(
int Ndry[1],
double xclip[5],
double yclip[5],
double POLYStoBEjoinedX[13][100],
double POLYStoBEjoinedY[13][100],
int NPOLYtoBEjoined[1],
int VERTtoBEjoined[13],
double POLYSunionX[13][100],
double POLYSunionY[13][100],
int NPOLYSunion[1],
int VERTunion[13],
double POLYintersX[13][100],
double POLYintersY[13][100],
int NPOLYSinters[1],
int VERTinters[13],
double absMAXx[1],
double absMAXy[1])
{
............
............
}
I created a module for the binding:
module Cplusplus
use,intrinsic :: ISO_C_Binding
interface
subroutine wrapUNIintersNM(Ndry,xclip,yclip,&
POLYStoBEjoinedX,POLYStoBEjoinedY,NPOLYStoBEjoined,VERTtoBEjoined,&
POLYSunionX,POLYSunionY,NPOLYSunion,VERTunion, &
POLYintersX,POLYintersY,NPOLYSinters,VERTinters,&
absMAXx,absMAXy) bind(C,name="wrapUNIintersNM") ! bind(C,name="wrapperClipper") add name if you wanna retain uppercase letter, otherwise are all made lowercase and C++ is case sensitive
import :: c_double, c_int
real(kind=c_double), dimension(5) :: xclip,yclip
real(kind=c_double), dimension(100,13) :: POLYintersX,POLYintersY,POLYStoBEjoinedX,POLYStoBEjoinedY,&
POLYSunionX,POLYSunionY
real(kind=c_double) :: absMAXx(1),absMAXy(1)
integer(kind=c_int), dimension(13) :: VERTinters,VERTunion,VERTtoBEjoined
integer(kind=c_int) :: Ndry,NPOLYStoBEjoined,NPOLYSinters,NPOLYSunion
end subroutine
end interface
end module Cplusplus
I have a subroutine update that calls wrapUNIintersNM this way
subroutine update
use Cplusplus
.........
...........
CALL wrapUNIintersNM(Ndry,xclip,yclip, &
POLYStoBEjoinedX,POLYStoBEjoinedY,NPOLYStoBEjoined,VERTtoBEjoined,& !polygons to join (union)
POLYSunionX,POLYSunionY,NPOLYSunion,VERTunion, & !union of polygons
POLYintersX,POLYintersY,NPOLYSinters,VERTinters,& !intersection of union
absMAXx,absMAXy)
..........
...........
return
end
Everything compiles fine, but at a linking stage I get this error:
error LNK1120: 1 unresolved externals example.dll
error LNK2019: unresolved external symbol _wrapUNIintersNM referenced in function _UPDATE
Any suggestion?
thanks

Create .lib file with c++ and Fortran / Call c++ code from Fortran / Unresolved external symbol

I am attempting to create a .lib library file that contains Fortran functions that call c++ functions, but I am getting the dreaded "error LNK2019: unresolved external symbol...". The code will eventually be compiled with a bunch of other libraries as a DLL and used in a separate program (PSSE). I am getting the compile error when PSSE attemps to create the DLL using my library. Here is the code I attempting to use, followed by the compiling code. The code should just add two numbers together and output the answer.
fort_code.f
SUBROUTINE TESTCPP ( II, JJ, KK, LL )
INCLUDE 'COMON4.INS'
integer*4, external :: CPPFUNCTION
INTEGER a, b, c, test
IF (.NOT. IFLAG) RETURN
a = ICON(II)
b = ICON(II + 1)
test = CPPFUNCTION( a , b, c )
WRITE ( ITERM, * ) 'C = ', c
RETURN
END
cpp_code.cpp
extern "C" {
void _CPPFUNCTION(int a, int b, int *c);
}
void _CPPFUNCTION(int a, int b, int *c) {
*c = a + b;
}
compile.bat
cl /nologo /MD /c /W3 /O2 /FD /EHsc /errorReport:prompt /D"MSWINDOWS" /D"WIN32" ^
/D"_WINDOWS" /D"NDEBUG" "cpp_code.cpp"
IFORT /nologo /Od /Oy- /assume:buffered_io /traceback /libs:dll /threads /c /Qip ^
/extend_source:132 /noaltparam /fpscomp:logicals /warn:nodeclarations ^
/warn:unused /warn:truncated_source /Qauto /Op /iface:cvf /define:DLLI ^
/include:"C:\Program Files (x86)\PTI\PSSE32\PSSLIB" ^
/object:"fort_code.OBJ" ^
"fort_code.f"
lib /out:fort_cpp.lib fort_code.obj cpp_code.obj
When the PSSE program attempts to create the DLL, this is the output I get:
ifort /nologo /assume:buffered_io /traceback /libs:dll /threads /c /Qip /extend_source:132 /noaltparam /fpscomp:logicals /Qprec /warn:declarations /warn:unused /warn:truncated_source /Qauto /fp:source /iface:cvf /define:DLLI /include:"C:\Program Files (x86)\PTI\PSSE32\PSSLIB" /object:"C:\temp\INIT_620289\11hw2ap_conec.obj" /module:"C:\temp\INIT_620289" "11hw2ap_conec.f"
ifort /nologo /assume:buffered_io /traceback /libs:dll /threads /c /Qip /extend_source:132 /noaltparam /fpscomp:logicals /Qprec /warn:declarations /warn:unused /warn:truncated_source /Qauto /fp:source /iface:cvf /define:DLLI /include:"C:\Program Files (x86)\PTI\PSSE32\PSSLIB" /object:"C:\temp\INIT_620289\11hw2ap_conet.obj" /module:"C:\temp\INIT_620289" "11hw2ap_conet.f"
link /INCREMENTAL:NO /NOLOGO /DLL /SUBSYSTEM:WINDOWS /MACHINE:X86 /ERRORREPORT:PROMPT #"C:\temp\INIT_620289\linkfilestod9p1.txt" /OUT:"C:\temp\INIT_620289\11hw2ap_dsusr.dll" /map:"C:\temp\INIT_620289\11hw2ap_dsusr.map"
fort_cpp.lib(fort_code.obj) : error LNK2019: unresolved external symbol _CPPFUNCTION#12 referenced in function _TESTCPP
C:\temp\INIT_620289\11hw2ap_dsusr.dll : fatal error LNK1120: 1 unresolved externals
ERROR during link(1)... Aborted
conec/conet are simply Fortran calls to the external library functions:
SUBROUTINE CONEC
C
INCLUDE 'COMON4.INS'
C
CALL TESTCPP ( 55791, 0, 0, 0)
C
RETURN
END
SUBROUTINE CONET
C
INCLUDE 'COMON4.INS'
C
IF (.NOT. IFLAG) GO TO 9000
C
C NETWORK MONITORING MODELS
C
C
9000 CONTINUE
C
RETURN
END
I have seen a few different examples of calling c++ functions from Fortran, but they all look slightly different. One thing I noticed was differening uses of the _ before or after the c++ function name. How do I know which to use: _CPPFUNCTION, CPPFUNCTION, or CPPFUNCTION_. Do I need to export the function in c++ using __declspec( dllexport )? Do I need to create an ALIAS:'_CPPFUNCTION' in the Fortran code?
I am using the following compilers:
ifort: IVF IA-32 v12.1.0.233
cl: v16.00.30319.01 x86
Is there something I am missing to link the c++ code properly to the Fortran functions?
The problem with is that there are a lot of options. None, one or two underscores before and or after, string length after a variable or at the end of a list, call by value or call by reference. Capitalize, lowercase or original naming. With just these options, the probability of getting it right is already lower than 1 in 100 (1/3*1/3*1/2*1/2*1/3).
You can reduce it a bit by introspecting the .lib file using the dumpbin utility and manually checking intel fortran default settings and the settings in the project files.
The most elegant way, like some suggested, is to use the combination of bind(C) and iso_c_binding module. The bind(C) statement avoids having to know the name mangling. The iso_c_bindings provides c strings and integer(c_int) instead of integer*4 to ensure compatibility of your types with C. You have to know that fortran calls by reference by default and you can use , value to call by value. This should raise your succes rate all the way back to 1.
Here is a simple example of how to call the add1 function defined in c++ below:
test.f90
program example
use iso_c_binding
implicit none
interface
integer(c_int) function add1(x) bind(C,name="add1")
!DEC$ ATTRIBUTES DLLEXPORT :: add1
use iso_c_binding
integer(c_int), value :: x
end function add1
end interface
call callingadd1()
contains
subroutine callingadd1()
write(*,*) '1+1=', add1(1)
end subroutine callingadd1
end program example
add1.cpp
extern "C" {
int add1(int x);
}
int add1(int x) {
return(x+1);
}
edit an example with only a subroutine. For inclusion in your shared object/dll/dylib.
subroutineonly.f90
subroutine callingadd1() bind(C, name="callingadd1")
!DEC$ ATTRIBUTES DLLEXPORT :: callingadd1
use iso_c_binding
implicit none
interface
integer(c_int) function add1(x) bind(C,name="add1")
!DEC$ ATTRIBUTES DLLEXPORT :: add1
use iso_c_binding
integer(c_int), value :: x
end function add1
end interface
write(*,*) '1+1=', add1(1)
end subroutine callingadd1