- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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!!!
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thanks so much, I've found how intel fortran compiler compiles these code and I solved the problem!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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!

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page