Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
Announcements
Welcome to the Intel Community. If you get an answer you like, please mark it as an Accepted Solution to help others. Thank you!

LNK2001 error with a simple function

Ibrahim_K_
New Contributor I
128 Views

Friends:

I build a small working prototype of a call from (Vuisual) C++ to Intel fortran. Then I tried to put in to my own project (which is very large). I am getting he symbol mismatch error:

"LNK2001 unresolved external symbol _SHOW

I inserted the following line in my .cpp file calling the function SHOW:

extern "C" float SHOW(int* in, float* out);

or

extern "C" {
	float SHOW(int* in, float* out);
}

and then I created a static fortran library (which build succesfully) containing:

    
    function SHOW(IN,OUT) BIND(C)

    use,intrinsic::ISO_C_BINDING
    
    IMPLICIT NONE
    
    REAL(C_FLOAT) :: SHOW
    
    INTEGER(C_INT) :: IN
    REAL(C_FLOAT) :: OUT
        
    WRITE(*,90) IN
90  FORMAT(/," from FORTRAN: in value = ",I4)
    OUT=199.0
    SHOW=1999.0;
    
    end function SHOW

 

then I pointed the linker to see my generated library in the correct directory. I checked the dependencies between CPP and Fortran. I am still getting the error. My VS 2017 settings must be OK as my prototype project builds.

Any ideas how I should go about isolating this problem.

I. Konuk

 

0 Kudos
1 Solution
gib
New Contributor II
128 Views

Fortran is case-insensitive, so the function name 'SHOW' appears as 'show'.  Change the C++ code to this:

#include <stdio.h>

extern "C" float show(int* in, float* out);

int main(void)
{
	int in = 123;
	float out;
	show(&in, &out);
	printf("out: %f\n",out);
	return(0);
}

I tested this simply by creating a .obj file from the Fortran code:

ifort -c show.f90

then linking this to main.cpp:

cl -c main.cpp show.obj

Your library should behave the same way.

View solution in original post

3 Replies
gib
New Contributor II
129 Views

Fortran is case-insensitive, so the function name 'SHOW' appears as 'show'.  Change the C++ code to this:

#include <stdio.h>

extern "C" float show(int* in, float* out);

int main(void)
{
	int in = 123;
	float out;
	show(&in, &out);
	printf("out: %f\n",out);
	return(0);
}

I tested this simply by creating a .obj file from the Fortran code:

ifort -c show.f90

then linking this to main.cpp:

cl -c main.cpp show.obj

Your library should behave the same way.

View solution in original post

FortranFan
Honored Contributor II
128 Views

gib wrote:

Fortran is case-insensitive, so the function name 'SHOW' appears as 'show'.  Change the C++ code to this: ..

Or another option will be to use the Fortran standard-supported NAME parameter with BIND:

extern "C" {
	float SHOW(int* in, float* out);
}
function SHOW(IN,OUT) bind(C, name="SHOW")

 

Ibrahim_K_
New Contributor I
128 Views

Thank you very much my friend. That was it! I should have paid attention. Inspecting the .OBJ file shows what you indicated.

By the way, I like your second suggestion too to make sure that I do not fell into a similar trap again by explicitly declaring a case sensitive name.

Cheers.

I. Konuk

Reply