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

Unresolved external symbol in vs2019 c++ program when calling fortran code

Colder
Beginner
304 Views

Hello everyone!

  I'm a user of visual studio 2019(VS2019) and I'd like to call a fortran function "hellof" in my C++ program, so I compile the fortran code in VS2019 "static library" and got FLib.lib file. I added the path of lib file into Linker->general->additional depedent directory  and name of lib file into Linker->input->additional dependencies, however when I built my c++ program, there came a error "Link2019 unresolved external symbol _hellof" 

   Here are my fortran code and c++ code:

fortran:

subroutine hellof()
write(*,*) "Hello World!!!"
end

c++ :

extern "C"
{
void hellof();
}

int main()
{
hellof();
}

Note that I'm using the latest oneAPI tools and built C++ in debug mode(×64).

Since I'v tried many methods post on the net and still got confuesd, could you please give me some instructions on how to solve it? Many thanks!!!

 

  

0 Kudos
4 Replies
jimdempseyatthecove
Black Belt
279 Views

Fortran defaults to making the external symbols (hellof in this case) to all upper case.

To make compatible with C calling convention use BIND:

 

          subroutine hellof() BIND(C, NAME="hellof")

 

Jim Dempsey

0 Kudos
Colder
Beginner
236 Views

Thanks so much, I've found how intel fortran compiler compiles these code and I solved the problem!

0 Kudos
Steve_Lionel
Black Belt Retired Employee
260 Views

You don't need the NAME= in this case - just using BIND(C) will make the name lowercase.

The uppercase name is the Intel Fortran default. Other compilers, or the Intel compiler on other platforms, may have different defaults.

0 Kudos
Colder
Beginner
235 Views

Thanks, Steve. Since I used GUN compiler to compile fortran before, which will turn the fortran subroutine name to lowercase and add a '_' after it, thus I usually call fortran function in C++ code with same name as what GNU compiler got(lowercase and _). The case is difference between intel fortran and GNU gfortran. In short, I've found the difference and solved my problems. Thanks for your feedback again! 

0 Kudos
Reply