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

Command line ifort fails to link static library-error LNK1120 unresolved external

Patrick_G_
Beginner
1,048 Views

Hi,

I have a static library UTILIB.LIB built in Visual Studio project UTILIB that includes recursive subroutine REPLACE_STRING. I have a project with main program EDITLINE.FOR that calls routine REPLACE_STRING in UTILIB. If I add UTILIB.LIB to the EDITLINE project I can successfully build my executable. However, if I try to compile EDITLINE.FOR and link from the command line within the Intel Development window (C:\Program Files (x86)\Intel\Composer XE 2013 SP1>) it always fails with the  LNK1120 unresolved external reference error for REPLACE_STRING. I have copied of both the main program EDITLINE.FOR and library UTILIB>LIB in the same folder. I have tried several things:

- adding complete path for my library(c:\fortran\UTILIB.LIB) to the LIB environment variable

- added the complete path to the ifort command: ifort c:\fortran\EDITLINE.FOR c:\fortran\UTILIB.LIB

- added a link option(getting desperate): ifort c:\fortran\EDITLINE.FOR /link c:\fortran\UTILIB.LIB

Any other suggestions? My PC configuration info is below. Thanks - Pat G.

DELL PC, Intel Core I5, 64-bit
Windows 7 Enterprise
Visual Studio 2010, 10.0.40219.1 SP1Rel

0 Kudos
1 Solution
mecej4
Honored Contributor III
1,048 Views

I think that the .OBJ/.LIB file whose symbols include _PAT_SHOWSUM is a 64-bit version, and the one which has PAT_SHOWSUM is a 32-bit version. You cannot mix 32-bit objects/libraries with 64-bit objects/libraries. 

Look at your VS project settings and make sure that you have specified the same target architecture for all parts, i.e., all for ia32 or all for x64.

View solution in original post

0 Kudos
5 Replies
Martyn_C_Intel
Employee
1,048 Views

Have you tried using Dumpbin /symbols on your object file and library, to see whether the symbols for REPLACE_STRING match?  If they are big files, use /OUT:filename  to write the output to a file, and then FIND to search it.

 Do you get unresolved references for functions that are not declared as recursive? If not, are you compiling both caller and callee with /recursive ?   (I haven't tested whether this matters, but it might). Also, if REPLACE_STRING is not part of a module that is USEd,  try creating an interface for REPLACE_STRING and placing it in the calling routine.

0 Kudos
Patrick_G_
Beginner
1,048 Views

I have tried to isolate the problem by starting over and stripping things down to the most basic examples. I have a simple program in one file and it calls a simple subroutine in the other file. See below.

Main program file Pat_TestProg.for:
 program Pat_TestProg
c-----------------------------------------------------------
c simple test program to call a routine in a static library
c-----------------------------------------------------------
 real zval1, zval2
 zval1 = 1.4
 zval2 = .2
 call Pat_ShowSum(zval1, zval2)
 stop
 end

Subroutine file Pat_ShowSum:
 subroutine Pat_ShowSum(zarg1, zarg2)
 real zarg1, zarg2
 write(*,*)'Sum=', zarg1 + zarg2
 return
 end

I can compile, link and execute this code successfully using three methods.
1) Compile each file, and link with no library:
ifort /c Pat_TestProg.for Pat_ShowSum.for
ifort /exe:Pat_TestProg0 Pat_TestProg.obj Pat_ShowSum.obj

2) Compile subroutine and create static library from command line, then compile main program and link with library:
ifort /c Pat_ShowSum.for
lib /out:Pat_Lib2.lib Pat_ShowSum.obj
ifort /exe:Pat_TestProg2.exe Pat_TestProg.for Pat_Lib2.lib

3)  Create a Visual Studio Static Library project Pat_Lib1 that includes the identical subroutine Pat_ShowSum. Then create a Visual Studio Fortran Application project that includes main program Pat_TestProg and link to Pat_Lib1. I can then build a Pat_TestProg.exe that also executes successfully.

However, if I try to do this from the command line with compile and link it fails with the "fatal error LNK1120: 1 unresolved externals" error message because it cannot find my subroutine Pat_ShowSum:
4) Compile main program and link to library created in Visual Studio:
ifort /exe:Pat_TestProg1.exe Pat_TestProg.for Pat_Lib1.lib

I believe that Martyn has correctly suggested that the cause for failure is the lack of an INTERFACE for my subroutine. I must confess ignorance of how to use INTERFACE but I noticed that the Visual Studio Static Library project always creates a file called "pat_showsum__genmod.f90" that contains the INTERFACE code. As Martyn suggested I dumped each library and notice that the Pat_Lib1.lib which fails has symbol "_PAT_SHOWSUM" with a leading underscore but the Pat_Lib2.lib which works ok has symbol "PAT_SHOWSUM". See dumps below. At this point I do not know how to fix the problem other than using one of the less desirable but successful methods I described in 1) - 3) above ,and need to move on. Thanks - Pat G.

Failing library:
Dump of file Pat_Lib1.lib
File Type: LIBRARY

COFF SYMBOL TABLE

000 00000001 ABS    notype       Static       | @feat.00

001 00000000 SECT1  notype       Static       | .rdata

    Section length   64, #relocs    4, #linenums    0, checksum        0
003 00000000 SECT1  notype       Static       | __STRLITPACK_0
004 00000018 SECT1  notype       Static       | __rtc_frame_desc_2.2
005 00000000 SECT2  notype       Static       | .text
    Section length   C4, #relocs    8, #linenums    0, checksum        0
007 00000000 SECT2  notype ()    External     | _PAT_SHOWSUM
008 00000000 SECT3  notype       Static       | .debug$S
    Section length  424, #relocs    4, #linenums    0, checksum        0
00A 00000000 SECT6  notype       Static       | .debug$T
    Section length   84, #relocs    0, #linenums    0, checksum        0
00C 00000000 UNDEF  notype ()    External     | _for_write_seq_lis
00D 00000000 UNDEF  notype ()    External     | _for_write_seq_lis_xmit
00E 00000000 UNDEF  notype ()    External     | @_RTC_CheckStackVars@8
00F 00000000 UNDEF  notype ()    External     | __RTC_CheckEsp
010 00000008 SECT1  notype       Static       | __STRLITPACK_2
011 00000010 SECT1  notype       Static       | __STRLITPACK_3
012 00000044 SECT1  notype       Static       | __STRLITPACK_1
013 00000000 UNDEF  notype       External     | __fltused
014 00000000 SECT4  notype       Static       | .drectve
    Section length   BA, #relocs    0, #linenums    0, checksum        0
016 00000000 SECT5  notype       Static       | .trace
    Section length   A0, #relocs    2, #linenums    0, checksum        0

String Table Size = 0xBD bytes

  Summary

         424 .debug$S
          84 .debug$T
          BA .drectve
          64 .rdata
          C4 .text
          A0 .trace

Successful library:
Dump of file Pat_Lib2.lib
File Type: LIBRARY

COFF SYMBOL TABLE
000 00000000 SECT1  notype       Static       | .text
    Section length   90, #relocs    5, #linenums    0, checksum        0
002 00000000 SECT1  notype ()    External     | PAT_SHOWSUM
003 00000000 SECT2  notype       Static       | .rdata
    Section length   18, #relocs    0, #linenums    0, checksum        0
005 00000010 SECT2  notype       Static       | __STRLITPACK_0
006 00000000 UNDEF  notype ()    External     | for_write_seq_lis
007 00000000 UNDEF  notype ()    External     | for_write_seq_lis_xmit
008 00000000 SECT3  notype       Static       | .xdata
    Section length    C, #relocs    0, #linenums    0, checksum        0
00A 00000000 SECT4  notype       Static       | .pdata
    Section length    C, #relocs    3, #linenums    0, checksum        0
00C 00000000 SECT2  notype       Static       | __STRLITPACK_2.0.1
00D 00000008 SECT2  notype       Static       | __STRLITPACK_3.0.1
00E 00000000 UNDEF  notype ()    External     | __ImageBase
00F 00000000 UNDEF  notype       External     | _fltused
010 00000000 SECT5  notype       Static       | .drectve
    Section length   B9, #relocs    0, #linenums    0, checksum        0

String Table Size = 0x7A bytes

  Summary

          B9 .drectve
           C .pdata
          18 .rdata
          90 .text
           C .xdata

 

0 Kudos
mecej4
Honored Contributor III
1,049 Views

I think that the .OBJ/.LIB file whose symbols include _PAT_SHOWSUM is a 64-bit version, and the one which has PAT_SHOWSUM is a 32-bit version. You cannot mix 32-bit objects/libraries with 64-bit objects/libraries. 

Look at your VS project settings and make sure that you have specified the same target architecture for all parts, i.e., all for ia32 or all for x64.

0 Kudos
Patrick_G_
Beginner
1,048 Views

My two dumps above got mucked when posted. I tried to insert as snippet of plain-text to preserve fixed-width font but instead got proportional and all CR-LFs removed. If interested please see the attachments here.

0 Kudos
Patrick_G_
Beginner
1,048 Views

Mecej4 pointed me in the right direction - the problem does seem to be a 32-bit vs. 64-bit mis-match. If I opened my command line window using "IA-32" instead of "Intel 64" my compile and link to Pat_Lib1 works. I believe the "underscore" symbols are for 32-bit though as opposed to 64-bit... and a big THANK YOU to Mecej4!

0 Kudos
Reply