自动检测是否需要在函数参数中添加“const”限定符

问题描述 投票:2回答:3

我编写了一个拦截许多MPI函数的PMPI分析库。在我的本地机器上,我有一个OpenMPI安装,一些函数参数有一个const限定符,例如:

int PMPI_Gather(const void *sendbuf, int sendcount, ...)  

很自然,我的PMPI库在相应的钩子函数中也有那些const限定符。但是,我经常运行东西的远程机器有一个MPI安装,其中mpi.h中的函数参数没有const限定符,所以当我编译我的库时,我得到一大堆警告,函数声明是不兼容的。当然,我可以忽略警告,禁止它们或手动删除const限定符。

我想知道是否有一种更优雅的方式来处理这种情况,如果有可能以某种方式检测mpi.h中的函数声明是否有const参数,并在编译期间自动添加或删除剖析库代码中的const限定符,或者它可能是某种配置功能。

c compilation mpi c-preprocessor
3个回答
2
投票

在MPI 3.0中添加了const-C绑定的正确性,即用于const参数的IN指针。您可以执行以下操作:

#if MPI_VERSION >= 3
    #define MPI_CONST const
#else
    #define MPI_CONST
#endif

int PMPI_Gather(MPI_CONST void *sendbuf, int sendcount, ...)

注意:您可以在"diff to 3.0" version of the standard的A.2 C Bindings部分中轻松查看更改。


1
投票

#ifdef ...的替代方法是简单地检查函数的类型:

typedef int PMPI_Gather_noconst (void *sendbuf, int sendcount, ...);
typedef int PMPI_Gather_const (const void *sendbuf, int sendcount, ...);

if( _Generic(PMPI_Gather, PMPI_Gather_noconst*:true, PMPI_Gather_const*:false) )
{
  PMPI_Gather_noconst* stuff;
  ...
}
else
{
  PMPI_Gather_const* stuff;
  ...
}

0
投票

通常在这种情况下,可以在多个位置定义变量或定义使用#ifdef#ifndef。你会有类似的东西:

#ifndef _YOU_CONSTANT #define _YOU_CONSTANT #endif

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