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

Linking C/C++ and Fortran, unresolved external symbols

bjdesa
New Contributor I
1,305 Views

 

I am trying to compile use a Fortran static library file with a C++ program but am getting unresolved external errors

I build the static library file using x64 

subroutine bar_ftn ( len_input_file, input_file ) bind( c )
    use, intrinsic :: iso_c_binding, only : c_int
    implicit none
    integer(c_int), value, intent(in) :: len_input_file
    character(len=1), intent(in) :: input_file(len_input_file)

    ! Local declarations (copy c char array into fortran character)
    character(len=len_input_file) :: infile
    integer :: i

    print *, "in bar_ftn"
    print *, len_input_file
    do i=1,len_input_file
    end do
end subroutine bar_ftn

the C++ program is 

#include "stdafx.h"
#include<iostream>
#include<fstream>

using namespace std;
extern "C" void bar_ftn ( int flag_len, char* flag );
static void
DisplayUsage(char* programName);

int main(int argc, char *argv[])
{
    char ctext[]="helloworld abcdefghijklmnopqrstuvwxyz abcdefghijklmnopqrstuvwxyz";
    int ctext_len=sizeof(ctext);

    //Call the Fortran
    bar_ftn( ctext_len, ctext );

    return 0;
}

but when I compile the C++ program I get the following errors

Error    2    error LNK1120: 1 unresolved externals    D:\temp\ConsoleApplication1\Debug\ConsoleApplication1.exe    1    1    ConsoleApplication1
Error    1    error LNK2019: unresolved external symbol _bar_ftn referenced in function _main    D:\temp\ConsoleApplication1\ConsoleApplication1\main.obj    ConsoleApplication1
    3    IntelliSense: cannot open source file "stdafx.h"    d:\temp\main.cpp    1    1    ConsoleApplication1

could you pleat tell me what  I am doing wrong?

0 Kudos
7 Replies
mecej4
Honored Contributor III
1,305 Views

You needed to add the OBJ file (or LIB file, if you have placed the OBJ file in a library and want to use the latter) for the Fortran subroutine to the C++ linking step. Your program is fine. You need to tell Visual Studio (or the command line) what to do to to link in the code for the subroutine when building the C++ EXE.

0 Kudos
TimP
Honored Contributor III
1,305 Views

Apparently you forgot to add the name='bar_ftn' clause in your bind(c).  As a result, the Fortran linkage probably defaults to BAR_FTN (if you didn't use another option to modify it).  You can check with dumpbin /symbols or other such tool.

0 Kudos
bjdesa
New Contributor I
1,305 Views

Could you please tell me the command line compiler I need to compile these two files I am using IFORT and CL?

0 Kudos
bjdesa
New Contributor I
1,305 Views

Could you please tell me the how to use the command line method to compile these two files I am using IFORT and CL?

0 Kudos
mecej4
Honored Contributor III
1,305 Views
I removed the include line for stdafx.h since I do not have that file, and named the Fortran file barf.f90 and the C++ file cpm.cpp.
ifort /c /MT barf.f90
lib /out:barf.lib barf.obj
cl /MT /EHsc cpm.cpp barf.lib

 

0 Kudos
bjdesa
New Contributor I
1,305 Views

but now O get the following error

Microsoft (R) C/C++ Optimizing Compiler Version 17.00.60610.1 for x64
Copyright (C) Microsoft Corporation.  All rights reserved.

main.cpp
Microsoft (R) Incremental Linker Version 11.00.60610.1
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:main.exe
main.obj
lib1.lib
LINK : fatal error LNK1104: cannot open file 'ifmodintr.lib'

0 Kudos
mecej4
Honored Contributor III
1,305 Views

The library in question (ifmodintr.lib) is required because your Fortran code uses one or more modules, and is present in the compiler's LIB/arch subdirectory. When linking using CL.EXE, either specify the full path name of this library file or make sure that the LIB environment variable includes the path to the Fortran compiler's LIB/arch directory.

If you use a command window configured for Intel Fortran, the LIB environment should already be configured to make this happen without any additional input from you. If, on the other hand, you use a command window configured only for MS C/C++, you have to modify %LIB% yourself.

0 Kudos
Reply