gpt4 book ai didi

c++ - 多语言编译中的I/O问题

转载 作者:行者123 更新时间:2023-11-30 19:50:30 24 4
gpt4 key购买 nike

向大家问好

我正在尝试在 UNIX 中分别使用 gcc、g++ 和 f77 编译和运行 C、C++ 和 fortran 多语言代码。我的程序由两部分组成,一部分是 C 语言,另一部分是 C++ 语言。它们通过用 C+ 编写的 main() 进行交互,而在这种情况下可以忽略 Fortran 代码。

我遇到了许多与此相关的问题,最明显的是我运行可执行文件时发生的段错误。我的前两个主题已经削弱了它,不幸的是,除了完全删除我的程序的任何一半中的任何输入/输出过程之外,似乎没有什么可以彻底解决问题,这是不可行的。

Accessing public class memory from C++ using C Output conflicts between C & C++

当我在程序的两个部分中使用输入/输出时,我需要找到收到段错误的原因。所有源代码均已编译,所有链接均成功,并且我知道每个部分(C 和 C++)在单独链接时都可以工作,不会出现此类段错误(当然,需要进行修改以使它们单独工作)。我已经包含了连接两个部分并执行输入/输出功能的所有代码。

任何帮助将不胜感激。

生成文件

products: SlowDynamic.exe

SlowDynamic.exe: main.o SA.o mersenne.o CFE.o BCs.o EMatrix.o Numbering.o KMatrix.o Solve.o MA_57.o blas.o MA_57_Depend.o Metis.o
f77 -L/usr/sfw/lib -R/usr/sfw/lib -lgcc_s -lstdc++ -o SlowDynamic.exe main.o \
SA.o mersenne.o CFE.o MA_57.o blas.o MA_57_Depend.o Metis.o\
BCs.o EMatrix.o Numbering.o KMatrix.o Solve.o

main.o: main.cpp
g++ -c -o main.o main.cpp

SA.o: SA.cpp
g++ -c -o SA.o SA.cpp

mersenne.o: mersenne.cpp
g++ -c -o mersenne.o mersenne.cpp

CFE.o: CFE.c
gcc -c -o CFE.o CFE.c

MA_57.o: MA_57.f
f77 -c -o MA_57.o MA_57.f

blas.o: blas.f
f77 -c -o blas.o blas.f

MA_57_Depend.o: MA_57_Depend.f
f77 -c -o MA_57_Depend.o MA_57_Depend.f

Metis.o: Metis.f
f77 -c -o Metis.o Metis.f

BCs.o: BCs.c
gcc -c -o BCs.o BCs.c

EMatrix.o: EMatrix.c
gcc -c -o EMatrix.o EMatrix.c

Numbering.o: Numbering.c
gcc -c -o Numbering.o Numbering.c

KMatrix.o: KMatrix.c
gcc -c -o KMatrix.o KMatrix.c

Solve.o : Solve.c
gcc -c -o Solve.o Solve.c

clean:
rm *.o Main.exe *.gpi

main.ccp

#include <iostream>
#include "SA.h"

using namespace std;


int main()
{

Initial.Initialize();

Parent.SA(Initial.Write);

system ("PAUSE");

return 0;
}

SA.h

#ifndef SA_H
#define SA_H

#include <vector>

class SimAnneal {

std::vector< std::vector<float> > DensityDomain;

float Solid_Ele_Num, Void_Ele_Num;
float Solid, Void;

double Energy;
double Time;

void Metropolis (double, int, int);
void Next_State (double, int);
double Schedule (double, int);
double ObjFunction ();
void Distribute ();
void Mutate ();
void Convert ();
void PrintDomain ();
void WriteResults (double, double, double, double, double);

public:
int x_max, y_max;
...
std::vector<float> DensityArray;
std::vector<float> EnergyArray;
...
void SA (int);
void Initialize ();
};

extern SimAnneal Initial, Parent, Child, Mutation, Best;

#endif

SA.cpp

include <math.h>
#include <iostream>
#include <fstream>
#include <time.h>
#include <vector>

#include "SA.h"
#include "CFE.h"
#include "randomc.h"

using namespace std;

SimAnneal Initial, Parent, Child, Mutation, Best;

...

void SimAnneal::Initialize ()
{
x_max = ReturnX();
y_max = ReturnY();

EnergyArray.resize(x_max*y_max);
DensityArray.resize(x_max*y_max);

...

Energy = ObjFunction();
}

...

void SimAnneal::PrintDomain ()
{
static ofstream OutputFile;

if (!OutputFile.is_open())
{
char FileName [] = "DensityDomain.txt";

OutputFile.open(FileName);

if (!OutputFile)
{
cerr << "Failed to open " << FileName << endl;
exit(EXIT_FAILURE);
}

//cout << "\nGenerating 'DensityDomain.txt'... \n" << endl;
}

for (int y = 0; y < y_max; y++)
{
for (int x = 0; x < x_max; x++)
{
OutputFile << DensityDomain[y][x] << " ";
}

OutputFile << endl;
}

OutputFile.close();
}

void SimAnneal::WriteResults (double i, double T, double x, double y, double z)
{
static ofstream OutputFile;

if (!OutputFile.is_open()) //check is file has been opened
{
char FileName [] = "Results.txt";

OutputFile.open(FileName);

if (!OutputFile)
{
cerr << "Failed to open " << FileName << endl;
exit(EXIT_FAILURE); //abort program
}

//cout << "\nWriting to file in progress... \n" << endl;

OutputFile << "Iterations" << '\t' << "Temperatures" << '\t' << "Sum Strain Energy" << endl; //<< "SwapNum1" << '\t' << "SwapNum2" << '\t' << "Distance" << endl;
OutputFile << endl;

Initial.Time = (int)time(0);
}

OutputFile << i << '\t' << T << '\t' << z << endl; //'\t' << y << '\t' << z << endl;

if (i == N_max || T <= T_abs)
{
Parent.Time = (int)time(0);

OutputFile << endl
<< "Settings: " << endl
<< "Initial Temperature: " << Initial.Temp << endl
<< "Temperature Iterations: " << i << endl
<< "Step Iterations: " << N_step << endl
<< endl
<< "Results: " << endl
<< "Final Temperature: " << Temp << endl
<< "Minimum: " << Energy << endl
<< "Computational Time (s): " << (Parent.Time-Initial.Time) << endl;

OutputFile.close();
}
}

CFE.h

#ifdef __cplusplus
extern "C" {
#endif

int ReturnX ();
int ReturnY ();
void CFE(float density[], float energy[], int Length);

#ifdef __cplusplus
}
#endif

CFE.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "BCs.h"
#include "EMatrix.h"
#include "Numbering.h"
#include "KMatrix.h"
#include "fg_types.h"
#include "Solve.h"

int ReturnX ()
{
FILE *infile;
infile = fopen("test05", "r");

int elemX,elemY;
fscanf(infile, "%i %i", &elemX, &elemY);

fclose(infile);

return elemX;
}

int ReturnY () { Same but returns elemY }

void CFE(float density[], float energy[])
{
// Extensive use of fscanf(), printf() & fprintf()
// and the following:

FILE *outfile;

outfile = fopen("File.txt", "w");
if(outfile == NULL){

}
else{
for(n=0;n<8;n++)
{
for(m=0;m<8;m++)
{
fprintf(outfile,"%f",KE[n][m]);
fprintf(outfile,"\t");
}
fprintf(outfile,"\n");
}
}

fclose(outfile);


}

最佳答案

我建议以批判的眼光检查你的代码,并检查所有看起来甚至有点奇怪的东西。

我会为你做这件事,但目前 C++ 不在我的轮换范围内,而且我被误报绊倒了。例如,这引起了我的注意:

if (!OutputFile.is_open())
{
char FileName [] = "DensityDomain.txt";

OutputFile.open(FileName);

if (!OutputFile)
{
cerr << "Failed to open " << FileName << endl;
exit(EXIT_FAILURE);
}

//cout << "\nGenerating 'DensityDomain.txt'... \n" << endl;
}

在已经调用了 is_open()open() 之后,您正在测试 OutputFile 是否为 null。在我看来,要么 1) OutputFile 不会为 null,要么 2) 在测试它之前,您不应该调用它的方法。但我错了。

明白我的意思吗?

关于c++ - 多语言编译中的I/O问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/667302/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com