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

时间:2009-03-20 18:06:02

标签: c++ c makefile

问候每个人

我正在尝试使用gcc,g ++和amp;来编译和运行C,C ++和fortran中的多语言代码。 f77分别在UNIX中。我的程序由两部分组成,一部分在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);


}

1 个答案:

答案 0 :(得分:0)

我建议用批判的眼光审视你的代码,然后检查一下看起来很奇怪的东西。

我会为你做这件事,但目前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;
}

在已经调用OutputFileis_open()之后,如果open()为空,则会测试一半。它看起来好像1)OutputFile不会为空或2)你不应该在之前调用之前的方法来测试它。但我错了。

明白我的意思?