Compiler errors 子例程的参数列表中出现语法错误

Compiler errors 子例程的参数列表中出现语法错误,compiler-errors,fortran,Compiler Errors,Fortran,我正在尝试使用gfortran编译代码。子程序plumed\u f\u gcmd就是我遇到一些问题的地方。这是一段代码 调用plumed\u f\u gcmd(“setMDTimeUnits”//char(0),timeUnits) 调用plumed_f_gcmd(“setPlumedDat”//char(0),“plumed.dat”//char(0)) 调用plumed_f_gcmd(“setLogFile”//char(0),“plumed.OUT”//char(0)) 调用plumed_

我正在尝试使用gfortran编译代码。子程序
plumed\u f\u gcmd
就是我遇到一些问题的地方。这是一段代码

调用plumed\u f\u gcmd(“setMDTimeUnits”//char(0),timeUnits)
调用plumed_f_gcmd(“setPlumedDat”//char(0),“plumed.dat”//char(0))
调用plumed_f_gcmd(“setLogFile”//char(0),“plumed.OUT”//char(0))
调用plumed_f_gcmd(“setNatoms”//char(0),natms)
调用plumed_f_gcmd(“setMDEngine”//char(0),“dlpoly1.90”//char(0))
编译器抛出以下错误:

调用plumed\u f\u gcmd(“setPlumedDat”//char(0),“plumed.dat”//char(0
1.
错误:参数列表中(1)处的语法错误
调用plumed_f_gcmd(“setMDEngine”//char(0),“dlpoly1.90”//char(0)
1.
错误:参数列表中(1)处的语法错误
首先,我不太明白在子例程中,第二个输入如何可以是一个数字和一个字符串?是否
'//char(0)
以某种方式将字符串更改为另一种数据类型?有人能给我解释一下吗

我的第二个问题是,为什么第三行(PLUMED.OUT)的错误没有发生

<>我不知道怎么做,但也涉及到一些C和C++包装;这是C:

中的子程序。
void羽状\u f\u gcmd(字符*键,void*val){
羽状_gcmd(键,val);
}

如注释中所示,错误消息

错误:参数列表中(1)处的语法错误

最有可能出现这种情况,因为您的Fortran代码是以固定格式编写的,并且编译器只处理前72个字符。若要避免这种情况,请尝试以下选项:

gfortran -ffixed-line-length-none yourcode.f
第三行(带“PLUMED.OUT”)没有出现错误的原因可能是该行在72个字符以内(但非常接近!)


至于
char(0)
(空字符),它附加到Fortran字符串,以便C例程可以将它们作为一个字符串处理。请注意,Fortran中的
/
表示字符串串联(类似于其他语言中的
“hello”+“world”
),而不是将字符串更改为其他数据类型的特殊内容


至于为什么
plumed\u f\u gcmd()
的第二个参数可以是一个数字和一个字符串,我想这个例程可能会读取第一个参数(或“command”),并在某个例程中进行适当的类型转换(请参阅原始的详细信息)。下面是这样一个示例,其中整数、实数和字符串变量被传递到同一个例程
sub()

fort.f90:

program main
    implicit none
    integer       :: intval
    real          :: realval
    character(50) :: str

    intval = 777 ; realval = 3.14 ; str = "world"

    call sub( "ShowInteger" // char(0), 100    )
    call sub( "ShowInteger" // char(0), intval )

    call sub( "ShowReal"    // char(0), 1.0     )
    call sub( "ShowReal"    // char(0), realval )

    call sub( "ShowString"  // char(0), "hello"   // char(0) )
    call sub( "ShowString"  // char(0), trim(str) // char(0) )
end
分段c:

#include <stdio.h>
#include <string.h>

void sub_ ( char* cmd, void* ptr )
{
    printf( "command = %s\n", cmd );

    if ( strcmp( cmd, "ShowInteger" ) == 0 ) printf( "int   : %20d\n",   *((int*)ptr) );
    if ( strcmp( cmd, "ShowReal"    ) == 0 ) printf( "float : %20.5f\n", *((float*)ptr) );
    if ( strcmp( cmd, "ShowString"  ) == 0 ) printf( "str   : %20s\n",    (char*)ptr ); 
}
结果

command = ShowInteger
int   :                  100
command = ShowInteger
int   :                  777
command = ShowReal
float :              1.00000
command = ShowReal
float :              3.14000
command = ShowString
str   :                hello
command = ShowString
str   :                world

(请注意,对于gcc/gfortran以外的编译器,上述C例程可能不可移植。为了使其可移植,请使用现代Fortran的互操作性功能,例如此)。

对我来说,这似乎超出了允许的行长度。这是指固定格式的源代码(文件扩展名/命令行是什么)?在这里的固定格式中,第72列之后的字符被忽略。是的,LRiO所做的更改是破坏性的,我将它们回滚。@francescalus:FORTRAN在每行的开头需要八个空格?TIL。这很好。我真的不明白为什么它在这里很重要,因为上下文是完全清楚的,所有这些空格都浪费了像素,但这很好。@s很遗憾,你也回退了语法高亮修复,所以现在代码被渲染成C++,当你用眼睛看代码时,它显然是错误的,非常混乱。你为什么选择这么做?
command = ShowInteger
int   :                  100
command = ShowInteger
int   :                  777
command = ShowReal
float :              1.00000
command = ShowReal
float :              3.14000
command = ShowString
str   :                hello
command = ShowString
str   :                world