problem with multiple source file

P

pereges

I hae compiled and executed codes of vector.c, reader.c , test.c
seperately and they can be execute in proper manner and give correct
outputs but when i tried to integrate them all into a single project
using pelles C it is giving two many errors.

/* vector.h */

#ifndef vector_h
#define vector_h

#include "common.h"

typedef struct vector_struct
{
double x, y, z;

} vector;

double vector_squared_length(vector* a) ;
double vector_length(vector *a);
vector *vector_negate(vector *a);
double vector_dot(vector *a, vector *b);
double vector_distance(vector *a, vector *b);
vector *vector_normalize( vector *a);
vector *vector_scale(vector *a, double newlength);
vector *vector_add(vector *a, vector *b, vector *c);
vector *vector_sub(vector *a, vector *b, vector *c);
vector *vector_cross(vector *a, vector *b, vector *c);

#endif

++++++++++++++++++++++++++++++++++++++

/* reader.h */

#ifndef reader_h
#define reader_h

#include "common.h"

typedef vector vertex;

typedef struct triangle_struct
{
int v0, v1, v2;

}triangle;

typedef struct object_struct
{
int nvert,ntri;
vertex *vert;
triangle *tri;

}object;

int read(object **);

#endif

+++++++++++++++++++++++++++++++++++++

/* common.h */
#ifndef common_h
#define common_h

#ifndef reader_h
#include "reader.h"
#endif

#ifndef vector_h
#include "vector.h"
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

void foo();

#endif

+++++++++++++++++++++++++++++++++++++++++

/* vector.c */

#include "common.h"

double vector_squared_length(vector* a)
{
return((a->x) * (a->x) + (a->y) * (a->y) + (a->z) * (a->z));
}

double vector_length(vector *a)
{
return (sqrt(vector_squared_length(a)));
}

vector *vector_negate(vector *a)
{
a->x = -a->x;
a->y = -a->y;
a->z = -a->z;

return(a);
}

double vector_dot(vector *a, vector *b)
{
return((a->x)*(b->x) + (a->y)*(b->y) + (a->z) *(b->z));
}

double vector_distance(vector *a, vector *b)
{
vector dv;
vector_sub(a, b, &dv);
return vector_length(&dv);
}

vector *vector_normalize( vector *a)
{
double len = vector_length(a);
if( len!= 0.0 )
{
a->x /= len;
a->y /= len;
a->z /= len;
}

return(a);
}

vector *vector_scale( vector *a, double newlength )
{
double length = vector_length(a);

if( length != 0.0 )
{
a->x *= newlength/length ;
a->y *= newlength/length ;
a->z *= newlength/length ;

}

return(a);
}

vector *vector_add( vector *a, vector *b, vector *c)
{
c->x = a->x + b->x ;
c->y = a->y + b->y ;
c->z = a->z + b->z;

return(c);
}

vector *vector_sub( vector *a, vector *b, vector *c)
{
c->x = a->x - b->x;
c->y = a->y - b->y;
c->z = a->z - b->z;

return(c);
}

vector *vector_cross( vector *a, vector *b, vector *c )
{
c->x = (a->y * b->z) - (a->z * b->y) ;
c->y = (a->z * b->x) - (a->x * b->z) ;
c->z = (a->x * b->y) - (a->y * b->x) ;

return(c);
}

++++++++++++++++++++++++++++++++++++++++++++++

/* common.c */

#include "common.h"

void foo(void)
{

}

+++++++++++++++++++++++++++++++++++++++++++++++

#include "common.h"

int reader(object **obj)
{
enum
{
FIRST_LINE,
SECOND_LINE,
THIRD_LINE,
FOURTH_LINE,
VERTICES,
GAP,
TRIANGLES,
LAST_LINE

} state;


int vertex_count = 0;
int triangle_count = 0;
FILE *zeus_file;
char data_line[120];

state = FIRST_LINE;
zeus_file = fopen("sphere.zeus", "r");

if(zeus_file == NULL)
{
perror(" File open error!");
return(EXIT_FAILURE);
}

while (state != LAST_LINE)
{


if (fgets(data_line, 120, zeus_file) == NULL)
{
perror("zeus file read failed");
return -1;
}
switch (state)
{
case (FIRST_LINE):
/* do nothing */
state = SECOND_LINE;
break;
case (SECOND_LINE):
/* do nothing */
state = THIRD_LINE;
break;
case (THIRD_LINE):
sscanf(data_line, "%d %*d %d", &((*obj)->nvert),
&((*obj)->ntri));
(*obj)->vert = calloc(sizeof(vertex),
(*obj)->nvert);
(*obj)->tri = calloc(sizeof(triangle),
(*obj)->ntri);
if( (*obj)->vert == NULL || (*obj)->tri == NULL)
{
perror("Memory cannot be allocated!");
return -1;
}
state = FOURTH_LINE;
break;
case (FOURTH_LINE):
/* do nothing */
state = VERTICES;
break;
case (VERTICES):
sscanf(data_line, "%lf %lf %lf",&((*obj)-
vert[vertex_count].x),&((*obj)->vert[vertex_count].y),&((*obj)-
vert[vertex_count].z));
vertex_count += 1;
if (vertex_count >= ((*obj)->nvert))
state = GAP;
break;
case (GAP):
state = TRIANGLES;
break;
case (TRIANGLES):
sscanf(data_line, "%d %d %d",&((*obj)-
tri[triangle_count].v0),&((*obj)->tri[triangle_count].v1),&((*obj)-
tri[triangle_count].v2));
triangle_count += 1;
if (triangle_count >= (*obj)->ntri )
state = LAST_LINE;
break;
}

}

return
0;

}


++++++++++++++++++++++++++++++++++++++++++++++

/* test.c */

#include "common.h"

void test_vector(void)
{
vector *a;
vector *b;

a = malloc(sizeof(vector));
b = malloc(sizeof(vector));
a->x = 1; a->y = 3; a-> z= 9;
b->x = 2; b->y = 5; b->z = 7;
printf("Vector dot is %f \n",vector_dot(a,b));

}


void test_reader(void)
{
int i;
object o, *obj;
obj = &o;
i = reader(&obj);

if(i == EXIT_FAILURE)
printf("error");
else
{
printf("Printing the vertices and the triangles \n\n");
for(i=0; i said:
vert.y, obj->vert.z);

for(i=0; i said:
tri.v1,obj->tri.v2);

}

}

int main(void)
{
test_vector();
test_reader();

return 0;
}
 
B

Barry Schwarz

I hae compiled and executed codes of vector.c, reader.c , test.c
seperately and they can be execute in proper manner and give correct
outputs but when i tried to integrate them all into a single project
using pelles C it is giving two many errors.

330 lines of code and not a single indication of what the errors are.
Do you think we can guess? Are the errors from the compiler, the
linker, or during execution?
/* vector.h */

#ifndef vector_h
#define vector_h

#include "common.h"

snip


#endif

++++++++++++++++++++++++++++++++++++++

/* reader.h */

#ifndef reader_h
#define reader_h

#include "common.h"

snip

#endif

+++++++++++++++++++++++++++++++++++++

/* common.h */
#ifndef common_h
#define common_h

#ifndef reader_h
#include "reader.h"
#endif

Since reader.h already has "include guards", the #if and #endif are
not needed here.

snip
void foo();

This is not a prototype and therefore serves no purpose.
#endif

+++++++++++++++++++++++++++++++++++++++++

/* vector.c */
snip


++++++++++++++++++++++++++++++++++++++++++++++

/* common.c */

#include "common.h"

void foo(void)

This differs from the "declaration" in common.h. Make up your mind.
{

}

+++++++++++++++++++++++++++++++++++++++++++++++

What file is this? reader.c?
#include "common.h"

snip

sscanf(data_line, "%lf %lf %lf",&((*obj)-
vert[vertex_count].x),&((*obj)->vert[vertex_count].y),&((*obj)-
vert[vertex_count].z));
vertex_count += 1;
if (vertex_count >= ((*obj)->nvert))
state = GAP;
break;
case (GAP):
state = TRIANGLES;
break;
case (TRIANGLES):
sscanf(data_line, "%d %d %d",&((*obj)-
tri[triangle_count].v0),&((*obj)->tri[triangle_count].v1),&((*obj)-
tri[triangle_count].v2));

Very strange that the line splits occurred between the - and the > of
the token ->. Are you sure that there is no space in your code?

snip
++++++++++++++++++++++++++++++++++++++++++++++

/* test.c */

#include "common.h"

snip


Remove del for email
 
B

Ben Bacarisse

pereges said:
I hae compiled and executed codes of vector.c, reader.c , test.c
seperately and they can be execute in proper manner and give correct
outputs but when i tried to integrate them all into a single project
using pelles C it is giving two many errors.

You could help people out with an error message!

You have got tied up in includes, trying to get common.h to do
everything. It can be done like that, but if when, say, test. include
common.h which needs reader.h which tries to get what it wants from
common.h (you just have to go thought this carefully by hand or using
something like gcc -E to see that it does not work).

Get rid of common.h and have every file include what it needs. You
can have one .h file that includes another if you like that style but
circular includes will tie you in knot.
 
P

pereges

You could help people out with an error message!

You have got tied up in includes, trying to get common.h to do
everything. It can be done like that, but if when, say, test. include
common.h which needs reader.h which tries to get what it wants from
common.h (you just have to go thought this carefully by hand or using
something like gcc -E to see that it does not work).

Get rid of common.h and have every file include what it needs. You
can have one .h file that includes another if you like that style but
circular includes will tie you in knot.

I could post error messages but theres just way too many. I think
change my approach. In my project, vector data structure and
operations are meant to be seen by every module so I will put that it
into common.h along with some common header files like stdio, stdlib ,
string and math which are needed almost every module. Suppose

all.h

a.h a.c
b.h b.h
c.h c.c

Now each of the .h's file should include all.h and every .c file will
include its corresponding .h file. I think then in main.c you need to
include all the headers including all.h
 
B

Ben Bacarisse

I could post error messages but theres just way too many.

The first one is (almost) always the most revealing one.
I think
change my approach. In my project, vector data structure and
operations are meant to be seen by every module so I will put that it
into common.h along with some common header files like stdio, stdlib ,
string and math which are needed almost every module. Suppose

all.h

a.h a.c
b.h b.h
c.h c.c

Now each of the .h's file should include all.h and every .c file will
include its corresponding .h file. I think then in main.c you need to
include all the headers including all.h

I don't see why you object to just including in each .c file what is
needed by that .c file. That is my preferred method for programs of
this sort of size.

There are times when a well organised program can have "grouped" .h
files that pull in related sets of types and functions, but your
program is not at that sort of size yet.

This is just a suggestion. You've got tied up once already, so why
not try the plain method first. You may like it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,979
Messages
2,570,185
Members
46,728
Latest member
FernMcmull

Latest Threads

Top