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

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

CoryG
Anfänger
3.087Aufrufe

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?

Beschriftungen (2)
0 Kudos
1 Lösung
Steve_Lionel
Geehrter Beitragender III
3.005Aufrufe

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

Lösung in ursprünglichem Beitrag anzeigen

6 Antworten
Steve_Lionel
Geehrter Beitragender III
3.068Aufrufe

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.

CoryG
Anfänger
3.063Aufrufe

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?

Steve_Lionel
Geehrter Beitragender III
3.006Aufrufe

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
Anfänger
3.002Aufrufe

 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?

Steve_Lionel
Geehrter Beitragender III
2.981Aufrufe

Exactly right.

CoryG
Anfänger
2.966Aufrufe

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

Antworten