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

How to include C++ header files in Fortran Visual Studio project

CoryG
Novice
1,722 Views

I am trying to incorporate a C++ header file into my Fortran project in Visual Studio 2019 using IFORT. This header file includes two other C++ header files. I've included all three under 'Source Files' and enabled the preprocessor. In the Fortran file where I need to reference the C++ header file, I have

 

```

#include 'header.h'

```

 

but when I compile, I just get a "Bad # preprocessor line" warning. How do I make it so this header file can be used in my Fortran files? Do I need to add additional commands for compiling, or should VS be able to handle that automatically?

Labels (2)
0 Kudos
1 Solution
Steve_Lionel
Honored Contributor III
1,640 Views

You can't do this. You'll have to write a Fortran version of the file as a module and USE the module. Fortran has extensive C interoperability features, but be aware that C++ procedures can't use any interface features not in C. (There are some additional limitations.) Any C++ procedures you want to call from Fortran must have the extern "C" prefix

View solution in original post

6 Replies
Steve_Lionel
Honored Contributor III
1,703 Views

Preprocessing is not enabled by default (and unlike on some other platforms, the case of the file type is ignored). You can turn it on in the project properties. Or, you can use INCLUDE. I assume you are aware that the Fortran compiler won't be happy with C/C++ language syntax.

0 Kudos
CoryG
Novice
1,698 Views

That's what I'm trying to find the answer to. It's a C++ file with various constants, types, and functions declared in C++. I need to access those in Fortran. Do I have to compile the header file using the C++ compiler first?

0 Kudos
Steve_Lionel
Honored Contributor III
1,641 Views

You can't do this. You'll have to write a Fortran version of the file as a module and USE the module. Fortran has extensive C interoperability features, but be aware that C++ procedures can't use any interface features not in C. (There are some additional limitations.) Any C++ procedures you want to call from Fortran must have the extern "C" prefix

CoryG
Novice
1,637 Views

 I see. Just for additional details, the C++ header files are used to compile C++ code which will then reference a couple static libraries. Since the purpose of the header file is to let the new code know what the interface to the libraries is, I would have to write a Fortran file that gives the interface instead, and then just include those libraries as external dependencies. Am I understanding this right?

0 Kudos
Steve_Lionel
Honored Contributor III
1,616 Views

Exactly right.

0 Kudos
CoryG
Novice
1,601 Views

Sweet. I understand now. I'll get working on that then. Thanks!

0 Kudos
Reply