Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
New Contributor I
42 Views

LNK2001 error with a simple function

Jump to solution

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

Accepted Solutions
Highlighted
New Contributor II
42 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

0 Kudos
3 Replies
Highlighted
New Contributor II
43 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

0 Kudos
Highlighted
Honored Contributor I
42 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")

 

0 Kudos
Highlighted
New Contributor I
42 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

0 Kudos