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

发布于 2024-07-15 05:23:06 字数 6961 浏览 1 评论 0原文

大家好

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

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

使用 C 从 C++ 访问公共类内存 C 和 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);


}

Greeting Everyone

I'm trying to compile and run a multi-language code in C, C++ and fortran using gcc, g++ & f77 respectively in UNIX. My program consists of two parts, one in C, the other in C++. They interface via a main() writen in C+, while the fortran code can be ignored for this case.

I have been having numerous issues with this, most noticabbly a Segmentation Error thats been occuring as I run the executable. The previous two topics of mine have whittled it down, unfortunatly nothing seems to be solving the problem outright other than completely removing any input/output processes in either half of my program, which just isn't feasible.

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

I need to locate the reason why I recieve Segmentation Errors when I use input/outputs in both parts of my program. All sources compile, all link successfully and I know that each part (C & C++) works when linked alone with no such Segmentation Errors (with modifications to let them work alone of course). I've included all the code that interfaces between the two parts and performs input/output functions.

Any help would be much appriciated.

Makefile

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);


}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你的他你的她 2024-07-22 05:23:06

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

我愿意为你做这件事,但目前 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) 在测试它之前,您不应该调用它的方法。 但是我错了。

明白了吗?

I'd suggest going through your code with a critical eye, and checking out everything that looks even remotely odd.

I'd do it for you but C++ isn't in my rotation at the moment and I'm tripping on false positives. For example, this caught my eye:

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;
}

Half way down you're testing if OutputFile is null, after already calling is_open() and open() on it. It looked to me as if either 1) OutputFile won't be null or 2) you shouldn't be calling methods on it before you test it. But I was wrong.

See what I mean?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文