0

I getting a link error when trying to call Fortran function from C++.

Firstly, I am using MSVS and Intel Fortran (relevant for symbol underscores and case sensitivity).

I have a Fortran main in project A. The functions in A call a C++ project B. So far so good. The functions in B calls a C++ function "void message(const char* msg, const int msg_max_len);" in project D. All is good.

I want to call a Fortran subroutine MESSAGE in project E instead of the C++ message in project D. In the C++ project B I defined a configuration with the preprocessor macro "_C2F" and added the following to the client code in B:

#ifdef _C2F
extern "C" void _MESSAGE(const char* msg, const int msg_max_len);

#define message _MESSAGE
#endif

after the include header for containing the prototype for message.

I get the link error:

2>lib_physical_properties.lib(pp_asme.obj) : error LNK2019: unresolved external symbol __MESSAGE referenced in function _pp_asme

What do I need to do to correct linkage?

Paul R
  • 208,748
  • 37
  • 389
  • 560
  • Unrelated to your problem, but in C++ all symbols starting with an underscore and followed by an upper-case letter (like `_MESSAGE` or `_C2F`) are *reserved* everywhere. See e.g. [What are the rules about using an underscore in a C++ identifier?](https://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier) for more information. – Some programmer dude Jan 29 '21 at 15:48
  • ISO_C_BINDING on the fortran side is worth looking at, and in particular the string handleing. The whole of ISO_C_BINDING is to make mixed language code easier. If the BIND(C) doesn't have the alias as "_MESSAGE" then the C-side will likely only see "_message" in the fortran.o file. – Holmz Jan 31 '21 at 10:17
  • Did you try it without the underscore? – Rob Feb 01 '21 at 09:41

0 Answers0