C++ 多语言编译中的I/O问题
问候大家 我尝试在UNIX中编译使用C语言、C++语言和FORTRAN语言,分别使用GCC、G++和F77。我的程序由两部分组成,一个在C,另一个在C++中。它们通过用C+编写的main()接口,而在这种情况下可以忽略fortran代码 我一直对此有很多问题,最明显的是在运行可执行文件时出现了一个分段错误。我的前两个主题已经把它删减了,不幸的是,除了完全删除我的程序的任何一半中的任何输入/输出过程之外,似乎没有什么能彻底解决这个问题,这是不可行的 我需要找到在程序的两个部分中使用输入/输出时收到分段错误的原因。所有源代码都编译成功,所有链接都成功,我知道每个部分(C&C++)在单独链接时都可以工作,没有这样的分段错误(当然还有修改,让它们单独工作)。我已经包含了这两个部分之间的接口以及执行输入/输出功能的所有代码 任何帮助都是非常感激的 生成文件C++ 多语言编译中的I/O问题,c++,c,makefile,C++,C,Makefile,问候大家 我尝试在UNIX中编译使用C语言、C++语言和FORTRAN语言,分别使用GCC、G++和F77。我的程序由两部分组成,一个在C,另一个在C++中。它们通过用C+编写的main()接口,而在这种情况下可以忽略fortran代码 我一直对此有很多问题,最明显的是在运行可执行文件时出现了一个分段错误。我的前两个主题已经把它删减了,不幸的是,除了完全删除我的程序的任何一半中的任何输入/输出过程之外,似乎没有什么能彻底解决这个问题,这是不可行的 我需要找到在程序的两个部分中使用输入/输出时
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;
}
CFE.c
#包括
#包括
#包括
#包括
#包括“BCs.h”
#包括“EMatrix.h”
#包括“编号.h”
#包括“KMatrix.h”
#包括“fg_类型.h”
#包括“Solve.h”
int ReturnX()
{
文件*填充;
infle=fopen(“test05”、“r”);
int elemX,elemY;
fscanf(填充、%i%i、&elemX、&elemY);
fclose(infle);
返回elemX;
}
int ReturnY(){相同,但返回elemY}
空隙CFE(浮子密度[],浮子能量[])
{
//fscanf()、printf()和fprintf()的广泛使用
//以及以下各项:
文件*输出文件;
outfile=fopen(“File.txt”,“w”);
if(outfile==NULL){
}
否则{
对于(n=0;n我建议用批判的眼光检查代码,检查所有看起来甚至有点奇怪的东西
我会为你做这件事,但是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;
}
如果(!OutputFile.is_open())
{
char FileName[]=“DensityDomain.txt”;
OutputFile.open(文件名);
如果(!OutputFile)
{
Celr与调试器一起运行……是的,请在调试器下运行-您将能够消除90%的猜测。即使只是给出行号将是一个开始……这是一个痛苦的问题。将问题减少到最小的自包含系统,您仍然可以使它失败。在这里输入。顺便说一句,KE是什么?在不知道的情况下,我不得不相信你%f是合适的,我知道你犯了错误。实际上,我将判定这里没有足够的信息来回答,因此这不是一个真正的问题。我投票关闭。他正在检查文件是否能够成功地、完全地打开,他可以说(OutputFile.fail())而不是(!OutputFile)翻译:如果OutputFile尚未打开,请尝试打开它并标记一个错误,如果它不起作用则退出。这很好。
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();
}
}
#ifdef __cplusplus
extern "C" {
#endif
int ReturnX ();
int ReturnY ();
void CFE(float density[], float energy[], int Length);
#ifdef __cplusplus
}
#endif
#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);
}
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;
}