简体   繁体   中英

error: variable not declared inside function

The problem is that MPI structure declared inside main() is not visible inside functions. Compiler gives following error:

my_prog.c: In function ‘void get(int)’:
my_prog.c:585:21: error: ‘MPI_GETSET’ was not declared in this scope
MPI_Send(&msg, 1, MPI_GETSET, rank, 0, MPI_COMM_WORLD);

Code sections are:

struct mpi_send{
  int job;
  int z;
  int img;
  short grp;
  short stp;
};

mpi_send msg;

int main(int argc, char** argv) {
    MPI_Init(&argc, &argv);
...
    MPI_Datatype MPI_GETSET;
    MPI_Datatype type[5] = {MPI_INT,MPI_INT,MPI_INT,MPI_SHORT,MPI_SHORT};
    int blocklen[5] = {1,1,1,1,1};
    MPI_Aint disp[5];
    disp[0]=sizeof(int); disp[1]=sizeof(int); disp[2]=sizeof(int);
    disp[3]=sizeof(short); disp[4]=sizeof(short);
    MPI_Type_create_struct(5, blocklen, disp, type, &MPI_GETSET);
    MPI_Type_commit(&MPI_GETSET);
...
    get(13);
}

void get(int z){
...
    MPI_Send(&msg, 1, MPI_GETSET, rank, 0, MPI_COMM_WORLD);
    MPI_Recv(&msg, 1, MPI_GETSET, rank, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
...
}

I would appreciate clarification why I can not use MPI_GETSET inside functions. Thanks in advance.

The scope of MPI_GETSET is the main() subroutine.

A direct consequence is this variable is not defined inside the get() subroutine.

You basically have two options in order to move forward :

  • pass MPI_GETSET as a parameter of the get() subroutine
  • declare MPI_GETSET as a global variable

If your compiler is recent enough (note you might need some extra flags), a third option would be to declare the get() subroutine inside the main() subroutine. (feel free to refer to Nested function in C )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM