为什么我的C预处理程序会使用空格忽略这些宏?

问题描述 投票:1回答:1

我正在尝试构建WRF 4.2,但是由于宏没有被预处理程序扩展而遇到了一些错误。

本质上,WRF由配置并编译源代码的csh脚本构建。该配置过程的一部分采用固定格式的Fortran文件(.F文件),并将其转换为自由格式的Fortran文件(.f90),然后将其编译为目标文件。正在运行的命令是根据用户选择的平台和编译器配置生成的,对我而言,这些配置是Darwin体系结构,GNU(gfortran / gcc)编译器。我正在尝试编译“串行”配置。这是我关注的输出部分:

sed -e "s/^\!.*'.*//" -e "s/^ *\!.*'.*//" module_mp_fast_sbm.F > module_mp_fast_sbm.G
cpp -P -nostdinc -xassembler-with-cpp -I/Users/isaacrowe/Documents/GitHub/WRF_ARW/WRF/inc -DEM_CORE=1 -DNMM_CORE=0 -DNMM_MAX_DIM=2600 -DDA_CORE=0 -DWRFPLUS=0 -DIWORDSIZE=4 -DDWORDSIZE=8 -DRWORDSIZE=4 -DLWORDSIZE=4 -DNONSTANDARD_SYSTEM_SUBR -DMACOS  -DWRF_USE_CLM  -DDM_PARALLEL -DSTUBMPI -DNETCDF -DLANDREAD_STUB=1 -DUSE_ALLOCATABLES -Dwrfmodel -DGRIB1 -DINTIO -DKEEP_INT_AROUND -DLIMIT_ARGS -DBUILD_RRTMG_FAST=0 -DBUILD_RRTMK=0 -DBUILD_SBM_FAST=1 -DSHOW_ALL_VARS_USED=0 -DCONFIG_BUF_LEN=65536 -DMAX_DOMAINS_F=21 -DMAX_HISTORY=25 -DNMM_NEST=0  -I. -traditional-cpp   module_mp_fast_sbm.G  > module_mp_fast_sbm.bb
/Users/isaacrowe/Documents/GitHub/WRF_ARW/WRF/tools/standard.exe module_mp_fast_sbm.bb | cpp -P -nostdinc -xassembler-with-cpp -traditional-cpp > module_mp_fast_sbm.f90
rm -f module_mp_fast_sbm.G module_mp_fast_sbm.bb
time gfortran -o module_mp_fast_sbm.o -c -O2 -ftree-vectorize -funroll-loops -w -ffree-form -ffree-line-length-none -fconvert=big-endian -frecord-marker=4   -I../dyn_em -I../dyn_nmm  -I/Users/isaacrowe/Documents/GitHub/WRF_ARW/WRF/external/esmf_time_f90  -I/Users/isaacrowe/Documents/GitHub/WRF_ARW/WRF/main -I/Users/isaacrowe/Documents/GitHub/WRF_ARW/WRF/external/io_netcdf -I/Users/isaacrowe/Documents/GitHub/WRF_ARW/WRF/external/io_int -I/Users/isaacrowe/Documents/GitHub/WRF_ARW/WRF/frame -I/Users/isaacrowe/Documents/GitHub/WRF_ARW/WRF/share -I/Users/isaacrowe/Documents/GitHub/WRF_ARW/WRF/phys -I/Users/isaacrowe/Documents/GitHub/WRF_ARW/WRF/wrftladj -I/Users/isaacrowe/Documents/GitHub/WRF_ARW/WRF/chem -I/Users/isaacrowe/Documents/GitHub/WRF_ARW/WRF/inc -I/usr/local/Cellar/netcdf/4.7.4/include    module_mp_fast_sbm.f90
module_mp_fast_sbm.f90:6065:4:

 6065 |     DM_BCAST_MACRO_R16 ( FAF1 )
      |    1
Error: Unclassifiable statement at (1)
        0.56 real         0.52 user         0.03 sys

这里发生的是,在最后一步中编译的Fortran文件仍然具有未替换的宏。请记住,其中有数十个这些Fortran文件,这是唯一有问题的文件。

我查看了module_mp_fast_sbm.F中宏的定义位置,并看到了这些定义

module_mp_fast_sbm.F

我还注意到已正确替换了同一宏的其他用法,但是这些实例没有使用宏名称,括号和参数之间的空格。这让我想知道间距是否有所不同。所以我进行了一个实验:

test.c:

#define DM_BCAST_MACRO_R4(A) CALL wrf_dm_bcast_bytes(A, size(A)*R4SIZE)
#define DM_BCAST_MACRO_R8(A) CALL wrf_dm_bcast_bytes(A, size(A)*R8SIZE)
#define DM_BCAST_MACRO_R16(A) CALL wrf_dm_bcast_bytes(A, size(A)*R16SIZE)

首先,我使用了WRF脚本中使用的命令:

#define DM_BCAST_MACRO_R4(A) CALL wrf_dm_bcast_bytes(A, size(A)*R4SIZE)

DM_BCAST_MACRO_R4(3);

// The style used by WRF
DM_BCAST_MACRO_R4 ( 3 );

// Just curious
DM_BCAST_MACRO_R4( 3);

DM_BCAST_MACRO_R4 (3);

我得到了这个输出:

cat test.c | cpp -P -nostdinc -xassembler-with-cpp -traditional-cpp > output_traditional.txt

我以为WRF正在使用的标志可能会导致此问题,所以我尝试了此命令:



CALL wrf_dm_bcast_bytes(3, size(3)*R4SIZE);

// The style used by WRF
DM_BCAST_MACRO_R4 ( 3 );

// Just curious
CALL wrf_dm_bcast_bytes( 3, size( 3)*R4SIZE);

DM_BCAST_MACRO_R4 (3);

并得到此结果:

cat test.c | cpp -P  > output_noflags.txt 

[C0的输出

CALL wrf_dm_bcast_bytes(3, size(3)*R4SIZE);

// The style used by WRF
DM_BCAST_MACRO_R4 ( 3 );

// Just curious
CALL wrf_dm_bcast_bytes( 3, size( 3)*R4SIZE);

DM_BCAST_MACRO_R4 (3);

从我读到的cpp -v来看,宏定义在开括号前不能有空格,但是使用宏可能在括号前有一个空格。那为什么我的预处理器不能替换呢?

c-preprocessor
1个回答
1
投票

[使用c0]时clang cpp的这种行为早在2013年就已经[c0]了,至今没有后续报道。似乎这可能是一个错误,因为gcc的预处理器不执行此操作(Apple clang version 11.0.3 (clang-1103.0.32.62) Target: x86_64-apple-darwin19.5.0 Thread model: posix InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang" -cc1 -triple x86_64-apple-macosx10.15.0 -Wdeprecated-objc-isa-usage -Werror=deprecated-objc-isa-usage -E -disable-free -disable-llvm-verifier -discard-value-names -main-file-name - -mrelocation-model pic -pic-level 2 -mthread-model posix -mframe-pointer=all -fno-strict-return -masm-verbose -munwind-tables -target-sdk-version=10.15.4 -target-cpu penryn -dwarf-column-info -debugger-tuning=lldb -target-linker-version 556.6 -v -resource-dir /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/11.0.3 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk -I /usr/include -I/usr/local/include -internal-isystem /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/local/include -internal-isystem /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/11.0.3/include -internal-externc-isystem /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include -internal-externc-isystem /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include -Wno-objc-signed-char-bool-implicit-int-conversion -Wno-extra-semi-stmt -Wno-quoted-include-in-framework-header -fdebug-compilation-dir /Users/isaacrowe/Desktop -ferror-limit 19 -fmessage-length 270 -stack-protector 1 -fstack-check -mdarwin-stkchk-strong-link -fblocks -fencode-extended-block-signature -fregister-global-dtors-with-atexit -fobjc-runtime=macosx-10.15.0 -fmax-type-align=16 -fdiagnostics-show-option -fcolor-diagnostics -traditional-cpp -o - -x c - clang -cc1 version 11.0.3 (clang-1103.0.32.62) default target x86_64-apple-darwin19.5.0 ignoring nonexistent directory "/usr/include" ignoring nonexistent directory "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/local/include" ignoring nonexistent directory "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/Library/Frameworks" #include "..." search starts here: #include <...> search starts here: /usr/local/include /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/11.0.3/include /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks (framework directory) End of search list. ),并且在3.2版之前的clang似乎也不执行此操作。但是here的预期语义在我所知的任何地方都没有精确定义,并且clang维护人员没有对错误报告做出响应,因此很难确定。也有可能他们只是不在乎-traditional-cpp并认为它已过时。

独立运行reported时,似乎example是您的安装的默认设置。我不知道确切如何关闭它,但是-traditional-cpp可以做到。请注意,-traditional-cpp也避免了传统模式,并且似乎可以正常运行。

您也可以使用没有此“错误”的gcc预处理器。如您所见,在您的系统上-traditional-cpp命令实际上是clang,但是您可以从Homebrew安装真正的gcc。

© www.soinside.com 2019 - 2024. All rights reserved.