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

Feature request: _LANGUAGE_FORTRAN preprocessor macro

ereisch
New Contributor II
617 Views

Most languages that support preprocessing have some manner of predefined macro specifying what the language being compiled is.  Since one can #include a file that can define constants for Fortran, C, and C++, it would be helpful if ifort and ifx predefined a macro that indicated the language being compiled was Fortran.  Example: gfortran defines _LANGUAGE_FORTRAN to 1, g++ (and others) define __cplusplus for C++ source, etc.

It looks like the compiler sets a handful of macros such as __INTEL_COMPILER, but nothing indicating the source is Fortran.  Requesting addition of some additional macros that define the language (it would be even more helpful if the supported language revision were available as a macro value as well).

One example use case:

 

#if defined(_LANGUAGE_FORTRAN)
#define INT64_C(val)  val _8
#define INT32_C(val)  val _4
#define INT16_C(val)  val _2
#define INT8_C(val)   val _1
#else
#include <stdint.h>    /* The above macros are defined in here for C-like languages) */
#endif

#define MY_CONSTANT_1 INT64_C(4194304)

 

This header could then be used to define MY_CONSTANT_1 for all C-like languages and Fortran.

Thanks!

0 Kudos
5 Replies
FortranFan
Honored Contributor II
597 Views

@ereisch ,

See this link:

https://github.com/j3-fortran/fortran_proposals/issues/65#issuecomment-1193038505

 

If you're not on the GitHub site for Fortran proposals, I suggest you become one and share your needs and ideas there.  That will be highly valuable to the Fortran user community.

0 Kudos
Ron_Green
Moderator
594 Views

The macro itself seems a good idea.  Not sure I like the idea of a macro for the Fortran Language Standard would be useful - for example if you have all of a particular Fortran Standard but maybe not some obscure feature that you deferred implementation.  ,Can you set the macro to the that Standard even if you are only missing a few minor features?  Would it have to be set to the lower Standard and ignore all the goodness your code may decide to not use? 

I'll pass along your suggestion to our Standards Committee members at Intel.

ivanp
Novice
587 Views

I doubt this usage example will convince the committee to say anything about the preprocessor.

If you need kind specifiers for specific integers widths, why not use the types available in iso_c_binding?

use, intrinsic :: iso_c_binding, only: c_int8_t, c_int16_t, c_int32_t, c_int64_t

integer(c_int64_t), parameter :: MY_CONSTANT_1 = 4194304

 In the meantime, you can also set up your own compiler wrapper (myifort) and place it somewhere on your PATH:

#!/usr/bin/env bash
ifort -D_LANGUAGE_FORTRAN=1 $@

 

0 Kudos
ereisch
New Contributor II
575 Views
@ivanp - I am not trying to declare variables - that is fine in compilable code. Here, I am using preprocessor macros to define constants to be used program-wide, in a large multi-language application. Using one file to define these across all languages is one of the golden benefits of using header files, and eliminates the [error-prone] task of having to maintain separate files with the same constants for each language.

In this case, I am declaring values that are actually bitfield members of a 64-bit wide variable. But when doing, for example, a bitwise NOT on members, you need to be sure you are interpreting the constant at the correct size, least you do not invert all of the bits you would want to. By declaring the size (using a macro, as I did in the use-case), that ensures you are correctly sizing the constant program-wide. You would do this in C by declaring the constant:
(int64_t)<value>
In Fortran, the equivalent is:
<value>_8

The only way I know of to do this in a language-agnostic way is to use preprocessor macros. But you would need to key them off of the current language. Hence the need for a language macro.
0 Kudos
ivanp
Novice
555 Views

Can't this be part of your build system (e.g. CMake)?

 

 

 

cmake_minimum_required(VERSION 3.12)

project(test VERSION 0.1.0 LANGUAGES Fortran C)

if(CMAKE_Fortran_COMPILER_ID MATCHES "^Intel")
  add_compile_definitions("$<$<COMPILE_LANGUAGE:Fortran>:_LANGUAGE_FORTRAN=1>")
endif()

add_executable(main main.F90)

 

 

You could also get rid of the enclosing if() block. Then the definition would apply to any Fortran compiler.

 

Edit: also a shorter form to check language and compiler ID is available:

 

add_compile_definitions("$<$<COMPILE_LANG_AND_ID:Fortran,Intel>:_LANGUAGE_FORTRAN=1>")

 

 If I build with "make VERBOSE=1", I can see the option being passed to the Intel Fortran compiler:

 

...
[ 50%] Building Fortran object CMakeFiles/main.dir/main.F90.o
/opt/intel/oneapi/compiler/2022.0.2/linux/bin/intel64/ifort -D_LANGUAGE_FORTRAN=1   -c /home/ivan/fortran/20220909/main.F90 -o CMakeFiles/main.dir/main.F90.o
...

 

Edit 2: you could also add the defintion to your Intel Fortran configuration file - ifort.cfg. More info is available at the page "Use Configuration Files" in the Intel Fortran compiler documentation: https://www.intel.com/content/www/us/en/develop/documentation/fortran-compiler-oneapi-dev-guide-and-reference/top/compilation/use-configuration-files.html

 

0 Kudos
Reply