GCC vs VISUAL STUDIO

Status
Not open for further replies.

paragkalra

The Linux Man !
I need your help very urgently.......

Actually my HOD (head of department) is doing PHD....

He has a .c file which can be successfully compiled on LINUX using gcc....

I myself have successfully compiled it on REDHAT

Now he wants to compile the same program on Windows using VISUAL STUDIO...

Is there any way out to solve this problem...

Actually its throwing some error...

Actually I have very little knowledge oF MICROSOFT PRODUCTS ....

Please kindly try to solve my problem as early as possible.....It's very urgent....

Here's the code:
/* cap2d.c
-------
This program takes arguments D and k, the parameters for the
Wierstraus-Mandelbrot (W-M) function. A two-dimensional unit
surface is generated with fract2d(), defined in wm.c. A second
flat surface is placed at a distance CAP_D above the first. Using
the method of finite differences, Laplace's equation is found, and
the capacitance (in the form of electrical flux per unit area) is
written to the file named in OUTFILE as Matlab commands.

As the program runs, it prints the iteration count and the finite
difference tolerance to stdout.

To compile:

gcc capacitance.c wm2d.c -lm -O2 -o ./cap2d

To run:

./cap2d D k

(where 1 < D < 2 and |1-k| < |1-D|.)\n"
*/

#include "wm2d.h"

// For convenience, try keeping NPTS = 1.0 / SCALE
#define NPTS 100 // Surfaces have NPTS x NPTS nodes
#define SCALE 0.01 // Separation of each node

#define L_OS 0.9 // Undershoot factor. See iterate(), below
#define L_IT 50000 // Number of finite difference iterations

#define CAP_D 2.0 // Mean separation of the two surfaces
#define CAP_V 1.0 // Surfaces have potential +/- CAP_V

#define OUTFILE "temp.m"
#define USAGE "Incorrect arguments. Usage: cap2d D k"

/* Macro for periodic boundary conditions, called by iterate(). */
#define WRAP(a) (((a) + NPTS) % NPTS)

/* Function prototypes. Watch out for use of both floats and doubles.
The array phi[][][] uses floats to speed up iterate(), while the
matrix inversion in fract2d() needs doubles to avoid overflow. */
float laplace (double **zlo, double **zhi);
inline float iterate(float ***phi1, float ***phi2, int **surfmin, int **surfmax);
void outfile(double D, float flux);
void lfree (float ***phi1, float ***phi2, int **surfmin, int **surfmax);


int main (int argc, char **argv) {

int i, j;
float flux;
double wm_H, wm_k, **zlo, **zhi;

if (argc == 3) {
wm_H = 2.0 - strtod(argv[1], NULL);
wm_k = strtod(argv[2], NULL);
} else
error(1, 0, USAGE);

/* Each surface is stored in an NPTS x NPTS 2-D array. The array
indices (times SCALE) are the x and y coordinates. The array
contents are the z-coordinates. */

zlo = (double **)xmalloc(NPTS * sizeof(double *));
zhi = (double **)xmalloc(NPTS * sizeof(double *));
for (i = 0; i < NPTS; i++) {
zlo = (double *)xmalloc(NPTS * sizeof(double));
zhi = (double *)xmalloc(NPTS * sizeof(double));
}

/* Fill zlo using the W-M function, with mean zero. */
fract2d(zlo, wm_H, wm_k, NPTS, SCALE);

/* Fill zhi with a simple flat surface z = CAP_D. */
for (i = 0; i < NPTS; i++)
for (j = 0; j < NPTS; j++)
zhi[j] = CAP_D;

flux = laplace (zlo, zhi);
outfile(2.0-wm_H, flux);

freezpt(zlo, NPTS);
freezpt(zhi, NPTS);

exit(0);
}


/* This function creates a 3-D array representing the potential in the
cavity between the surfaces. Using the finite difference method, we
solve Laplace's equation for the cavity.

The function returns the flux, which is
flux = (sum) E . dA
= A * (sum) dE/dz
= 1 * ((sum)dE) / SCALE

Note that the capacitance is simply
C = flux / ( CAP_V * epsilon_0) */
float laplace (double **zlo, double **zhi) {

int i, j, k, m, n, nz;
float tmp;
double zmin, zmax;

int **surfmin = xmalloc(NPTS * sizeof (int *));
int **surfmax = xmalloc(NPTS * sizeof (int *));

float ***phi1 = xmalloc(NPTS * sizeof (float **));
float ***phi2 = xmalloc(NPTS * sizeof (float **));
float ***phi3;

zmin = HUGE_VAL;
zmax = 0.0;
for (i = 0; i < NPTS; i++)
for (j = 0; j < NPTS; j++) {
if (zmin > zlo[j]) zmin = zlo[j];
if (zmax < zhi[j]) zmax = zhi[j];
}

nz = 1 + (int)((zmax - zmin)/SCALE);

for (i = 0; i < NPTS; i++) {
surfmin = (int *)xmalloc(NPTS * sizeof (int));
surfmax = (int *)xmalloc(NPTS * sizeof (int));
phi1 = (float **)xmalloc(NPTS * sizeof (float *));
phi2 = (float **)xmalloc(NPTS * sizeof (float *));

for (j = 0; j < NPTS; j++) {
phi1[j] = (float *)xmalloc(nz * sizeof(float));
phi2[j] = (float *)xmalloc(nz * sizeof(float));
}
}

for (i = 0; i < NPTS; i++) {
for (j = 0; j < NPTS; j++) {

/* surfmin and surfmax contain the index for the lower and upper
surfaces respectively. For example, phi[j][surfmin[j]]
gives the potential of the lower surface. */
surfmin[j] = (int)((zlo[j]-zmin)/SCALE);
surfmax[j] = (int)((zhi[j]-zmin)/SCALE);

if (surfmin[j] > surfmax[j])
error(1, 0, "Surfaces of capacitor meet.");

/* The nodes outside the cavity (including the surface nodes)
are maintained at +/- CAP_V. */
for (m = 0; m <= surfmin[j]; m++)
*(phi1[j] + m) = *(phi2[j] + m) = - CAP_V;

for (n = nz - 1; n >= surfmax[j]; n--)
*(phi1[j] + n) = *(phi2[j] + n) = CAP_V;

/* As an initial guess, we let the potential drop linearly from
the lowest point (zmin) to the highest point (zmax). */
tmp = 1.0 / (n - m);
for (k = m + 1; k < n; k++)
phi1[j][k] = 2 * CAP_V * (k - m) * tmp - CAP_V;
}
}

/* Perform finite difference iterations. tmp is the
"badness-of-fit," which should converge to zero. If it starts
blowing up or oscillating wildly, decrease L_OS. */
for (i = 0; i < L_IT; i++) {
tmp = iterate(phi1, phi2, surfmin, surfmax);
phi3 = phi1; phi1 = phi2; phi2 = phi3;
printf ("\r[%5d] %.2f ", i, tmp);
fflush(stdout);
}
printf("\n");

/* Compute and return the flux. */
tmp = 0.0;
k = nz - 5;
for (i=0; i < NPTS; i++)
for (j=0; j < NPTS; j++)
if (k < surfmin[j] || k > surfmax[j])
error(1, 0, "Surfaces of capacitor meet.");
else
tmp += (phi1[j][k+1] - phi1[j][k]);

lfree(phi1, phi2, surfmin, surfmax);
return (tmp * SCALE);
}


/* This function performs the finite difference step:

phi_{n+1} (x,y,z) = phi_{n} (x,y,z) + L_OS * A(x,y,z)

where L_OS is the (under/over)relaxation and

A(x,y,z) = 1/6 * ( phi_{n} (x+h, y, z) + phi_{n} (x-h, y, z)
+ phi_{n} (x, y+h, z) + phi_{n} (x, y-h, z)
+ phi_{n} (x, y, z+h) + phi_{n} (x, y, z-h) )
- phi_{n} (x,y,z)

phi_{n} is stored in phi1. The function stores phi_{n+1} in phi2. */

inline float iterate(float ***phi1, float ***phi2, int **surfmin, int **surfmax) {

int i, j, k;
float tmp, tol = 0.0;

for (i = 0; i < NPTS; i++)
for (j = 0; j < NPTS; j++)
for (k = surfmin[j] + 1; k < surfmax[j]; k++) {
tmp = phi1[j][k-1] + phi1[j][k+1];
tmp += phi1[WRAP(i-1)][j][k] + phi1[WRAP(i+1)][j][k];
tmp += phi1[WRAP(j-1)][k] + phi1[WRAP(j+1)][k];
tmp *= 0.1666667;
tmp -= phi1[j][k];
tol += tmp;
phi2[j][k] = phi1[j][k] + L_OS * tmp;
}
return tol;
}

void outfile(double D, float flux) {

FILE *out = fopen(OUTFILE, "a");

fprintf(out, "D = %f : ", D);
fprintf(out, "flux = %f\n", flux);

fclose(out);
}

/* Frees memory allocated in laplace(). */
void lfree (float ***phi1, float ***phi2, int **surfmin, int **surfmax) {

int i, j;
for (i = 0; i < NPTS; i++) {
for (j = 0; j < NPTS; j++) {
free(phi1[j]);
free(phi2[j]);
}
free(phi1);
free(phi2);
free(surfmin);
free(surfmax);
}
free(phi1);
free(phi2);
free(surfmin);
free(surfmax);
}


HERE's the ERROR:
------ Build started: Project: cap2d, Configuration: Debug Win32 ------
Compiling...
cap2d.c
e:\d drive backup\parag\final_year_project\code\wm2d.h(2) : fatal error C1083: Cannot open include file: 'math.h': No such file or directory
Build log was saved at "file://e:\D drive backup\parag\final_year_project\code\Debug\BuildLog.htm"
cap2d - 1 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
 

mehulved

18 Till I Die............
I am very much of a noob at programming but the error message explicitly shows that math.h file is missing. math.h is called by wm2d.h so check that code and correct the error.
 

morpheusv6

Journeyman
Can GCC run on the windows platform(windows XP more specifically) or do I have to download linux for that( please recommend if yes(Fedora Core 6 or Ubuntu or any other))?
I am currently using Dev C++ and MS VC++ Express Edition 2005.

But a programming competion I attended a few months back was based on GCC.

Is there any syntax difference between those program which are compiled in MS VC++ and GCC?
Also recommend a good book to learn GCC, I already have knowledge of C and C++.
 

mehulved

18 Till I Die............
Dev C++ uses MingW which stans for minimalistic gnu for windows, it has windows port for gcc. So, you are using gcc.
That's as much as I know.
 

GNUrag

FooBar Guy
Search where is the include file named "math.h" in your VS.Net++ installation, note that path, and give it while compiling.

With gcc/turboc++ its given with the -I/path/to/math.h parameter.

Mostly there should be a GUI interface where you can enter what all directories the preprocessor needs to look at for include libraries.
 

romeo_8693

"The RaCaLaNGeL"©
i read sumwhere that linux source codes can be compiled in windows environment using cygwin!dnt know wats the proc tho....run a search u shud get it..
 

Yamaraj

The Lord of Death
morpheusv6 said:
Can GCC run on the windows platform(windows XP more specifically) or do I have to download linux for that( please recommend if yes(Fedora Core 6 or Ubuntu or any other))?
I am currently using Dev C++ and MS VC++ Express Edition 2005.

But a programming competion I attended a few months back was based on GCC.

Is there any syntax difference between those program which are compiled in MS VC++ and GCC?
Also recommend a good book to learn GCC, I already have knowledge of C and C++.
1. Yes, GCC has a Win32 port that runs on Windows. MinGW, as noted by tech_your_future, is available; but even the latest version (candidate release) comes with GCC-3.4.5. I compile my own GCC, binutils, win32api and mingw-runtime, which in turn is used to build and distribute ntemacs - *ntemacs.sourceforge.net
I have GCC-4.1.1, binutils-2.17 and other latest toolchain for Win32 - built myself of course. If I get enough requests, I may even upload it someplace.

2. GCC will compile all standard C and C++ code, provided they comply with the ANSI/ISO standards. It should be noted, however, that C99 is still not completely supported by any compiler[*]. Same goes for C++; very few compilers support the entire C++98 ISO standard.

3. I recommend official GNU manuals for GCC. You may also want to look at "An Introduction to GCC" and "Using GCC". *www.network-theory.co.uk/docs/gccintro/ Another good book is "Definitive Guide to GCC".


[*] - Comeau compiler is probably the only exception.
 
Last edited:

Zeeshan Quireshi

C# Be Sharp !
well there IS NO math.h in standard C++ , it is in C so you should use :
Code:
#include<cmath>

instead of :
Code:
#include<math.h>

morpheusv6 said:
Can GCC run on the windows platform(windows XP more specifically) or do I have to download linux for that( please recommend if yes(Fedora Core 6 or Ubuntu or any other))?
I am currently using Dev C++ and MS VC++ Express Edition 2005.

But a programming competion I attended a few months back was based on GCC.

Is there any syntax difference between those program which are compiled in MS VC++ and GCC?
Also recommend a good book to learn GCC, I already have knowledge of C and C++.

hey made STANDARD C++ programs will compile on both the compilers perfectly , it is the depricated libraries that pose the problems , most ppl learn C in the name of C++ using OLD style headers , etc .
 
Last edited:
Status
Not open for further replies.
Top Bottom