- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Exactly right.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Sweet. I understand now. I'll get working on that then. Thanks!
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page