Error LNK2019: unresolved external symbol _Invert_LaplacianZBCS referenced in function _MAIN__ Main.obj

Error LNK2019: unresolved external symbol _Invert_LaplacianZBCS referenced in function _MAIN__ Main.obj

Hi,

I got this error when trying to call a C function in Fortran. The C prototype of the function is

                        void Invert_LaplacianZBCS(double *t,int nx,int ny);

I built an interface in my Fortran program

   INTERFACE
!   
      SUBROUTINE Invert_LaplacianZBCS(t, nx, ny) BIND(C, name='Invert_LaplacianZBCS')
!
         USE, INTRINSIC :: iso_c_binding, ONLY: c_int, c_ptr
!
         IMPLICIT NONE
!
         TYPE(c_ptr), VALUE :: t
!
         INTEGER(c_int) :: nx, ny         
!
      END SUBROUTINE Invert_LaplacianZBCS
!      
   END INTERFACE

Then I called the C function in the main program:

     USE, INTRINSIC :: iso_c_binding

      REAL(KIND=c_double), ALLOCATABLE, DIMENSION(:), TARGET    ::  x
!
      INTEGER(c_int)        ::  m

      CALL Invert_LaplacianZBCS(C_LOC(x), m, m)

x and m were used in previous calculations and I checked that both of them were valid before the execution of the call.

However, an error was still produced when I built the solutions and the buildlog says:

 

Deleting intermediate files and output files for project 'M639 HW4', configuration 'Debug|Win32'.
Compiling with Intel(R) Visual Fortran Compiler XE 12.0.5.221 [IA-32]...
ifort /nologo /debug:full /Od /warn:interfaces /module:"Debug\\" /object:"Debug\\" /Fd"Debug\vc100.pdb" /traceback /check:bounds /libs:static /threads /dbglibs /c /Qvc10 /Qlocation,link,"c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\\bin" "\\pe-file\grads\jie.he\My Documents\Fortran projects\M639 HW4\M639 HW4\Compressed_Row_Storage.f90"
ifort /nologo /debug:full /Od /warn:interfaces /module:"Debug\\" /object:"Debug\\" /Fd"Debug\vc100.pdb" /traceback /check:bounds /libs:static /threads /dbglibs /c /Qvc10 /Qlocation,link,"c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\\bin" "\\pe-file\grads\jie.he\My Documents\Fortran projects\M639 HW4\M639 HW4\Iterative_Methods.f90"
ifort /nologo /debug:full /Od /warn:interfaces /module:"Debug\\" /object:"Debug\\" /Fd"Debug\vc100.pdb" /traceback /check:bounds /libs:static /threads /dbglibs /c /Qvc10 /Qlocation,link,"c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\\bin" "\\pe-file\grads\jie.he\My Documents\Fortran projects\M639 HW4\M639 HW4\Main.f90"
Linking...
Link /OUT:"Debug\M639 HW4.exe" /INCREMENTAL:NO /NOLOGO /MANIFEST /MANIFESTFILE:"\\pe-file\grads\jie.he\My Documents\Fortran projects\M639 HW4\M639 HW4\Debug\M639 HW4.exe.intermediate.manifest" /MANIFESTUAC:"level='asInvoker' uiAccess='false'" /DEBUG /PDB:"\\pe-file\grads\jie.he\My Documents\Fortran projects\M639 HW4\M639 HW4\Debug\M639 HW4.pdb" /SUBSYSTEM:CONSOLE /STACK:100000000 /IMPLIB:"\\pe-file\grads\jie.he\My Documents\Fortran projects\M639 HW4\M639 HW4\Debug\M639 HW4.lib" "Debug\Compressed_Row_Storage.obj" "Debug\Iterative_Methods.obj" "Debug\Main.obj"
Link: executing 'link'
Main.obj : error LNK2019: unresolved external symbol _Invert_LaplacianZBCS referenced in function _MAIN__
Debug\M639 HW4.exe : fatal error LNK1120: 1 unresolved externals


M639 HW4 - 2 error(s), 0 warning(s)

Does anyone know what is going wrong, please? I use Intel(R) Visual Fortran Compiler XE 12.0.5.221.

Thanks,

5 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

How did you make the C function available to the Fortran project? I'm going to guess that while you have the C code as a separate static library project in the solution, you did not make the C project a dependent of the Fortran project. You do this by right-clicking on the Fortran project and selecting Project Dependencies. (At least that's one way.) The output you showed did not indicate any C code linked in.

Steve - Intel Developer Support

Quote:

Steve Lionel (Intel) wrote:

How did you make the C function available to the Fortran project? I'm going to guess that while you have the C code as a separate static library project in the solution, you did not make the C project a dependent of the Fortran project. You do this by right-clicking on the Fortran project and selecting Project Dependencies. (At least that's one way.) The output you showed did not indicate any C code linked in.

 

Thanks for your reply, Steve! I was not aware of these and simply put the .c file in the same directory as the Fortran program and included the file in the Source Files folder in Visual Studio. How can I specify the dependencies? Can you explain a little bit more, please? I just tried to right-click the project => Properties => Custom Build Step => Additional Dependencies, and put in the file name constantInvert.c, but this didn't work.

 

 

Best Reply

You can't mix Fortran and C sources in a project - Microsoft requires that the C code be in a C/C++ static library project. Do you have Visual C++ installed or are you using the Visual Studio Shell provided by Intel Fortran? If the latter, you can't compile C code.

Otherwise,

  • Right click on your VS Solution in Solution Explorer (not the project) Select Add... New Project
  • When the New Project dialog comes up, select Visual C++ > Win32 and then on the right select "Win32 Project"
  • Enter a name for the project and a location if you care, then click OK
  • The Win32 Application Wizard will appear. Click Next
  • Under Application Settings, select Static Library and uncheck all the boxes under Additional Options. Click Finish
  • Right click on your newly created project. Select Add > Existing Item. Browse to your C file and add that
  • Right click on your Fortran project and select Project Dependencies. Check the box for your C project under "Depends on" and click OK.

Now build the solution.

Steve - Intel Developer Support

Thanks, Steve! This solves the problem!  The solution can be built, but I get new problems in debugging. It writes in the C code:

                                    c=(complex *) malloc(sizeof(complex)*np);
                                   d=(complex *) malloc(sizeof(complex)*np);

The malloc function in these two lines fails 9 times out of 10, and returns a null pointer. I thought it was because my least np value is about 0.2 billion, which is rather large, so I set the heap reserve size to 200MB, but this did not help.

______________________________________________________________________________________________________

I have solved this problem. I added the VALUE attribute to every argument I passed to the C function, or else it would pass the address, which is not desirable.

 

Leave a Comment

Please sign in to add a comment. Not a member? Join today