我將此ProcessStasts.h
文件包含在其他兩個.h文件中。編譯包含結構的h文件時出現鏈接錯誤
#pragma once
#include <mpi.h>
#include <cstddef>
struct ProcessStats
{
int rank,
itLeft,
crtIt,
processFlag;
float speed;
};
MPI_Datatype MPI_Cust_ProcessStats_create()
{
// set data to create new MPI data type
MPI_Datatype MPI_Cust_ProcessStats;
MPI_Datatype dataTypes[5] = {MPI_INT, MPI_INT, MPI_INT, MPI_INT, MPI_FLOAT};
int blockLengths[5] = {1, 1, 1, 1, 1};
MPI_Aint offsets[5];
offsets[0] = (MPI_Aint) offsetof(ProcessStats, rank);
offsets[1] = (MPI_Aint) offsetof(ProcessStats, itLeft);
offsets[2] = (MPI_Aint) offsetof(ProcessStats, crtIt);
offsets[3] = (MPI_Aint) offsetof(ProcessStats, processFlag);
offsets[4] = (MPI_Aint) offsetof(ProcessStats, speed);
// create new MPI type based on data from above
MPI_Type_create_struct(5, blockLengths, offsets, dataTypes, &MPI_Cust_ProcessStats);
MPI_Type_commit(&MPI_Cust_ProcessStats);
return MPI_Cust_ProcessStats;
}
當我嘗試編譯時,出現此錯誤:error LNK2005: MPI_Cust_ProcessStats_create(void) already defined
。如果我對#include "ProcessStasts.h"
指令和使用ProcessStats結構的行從其中一個文件發表評論,它會正確編譯。我甚至嘗試評論所有依賴於ProcessStats的行,只留下#include "ProcessStasts.h"
語句,我得到這個lnk錯誤。哪裏不對?
當'struct'中有5個字段時,爲什麼MPI結構數據類型構造函數'3'的第一個參數? –
由於出錯,使用更多變量更改結構...與DataType數組[6]相同。謝謝,這讓我看到了一些我以後會發現的bug。 – codiac