I have two header files and a main program.
Header files are data.h and flight.h. The main program is calculateflight.c.
The data header file has a set of typedef structs that contains the variables required for the program to run.
I have created a headerfile named flightmodel.h that contains the following:
#ifndef __FLIGHT_MODEL_H
#define __FLIGHT_MODEL_H
#include "Data.h"
void calculateFlight(speedParamsType *speed, spinParamsType *spin,
flightParamsType *fData);
#endif
The data.h contains typedef structs like
typedef struct {
float totalSpin;
float spinAxis;
float backSpin;
float sideSpin;
} spinParamsType;
There are no errors in the struct variables, but I don't know how to call struct inside the main function.
void main()
{
speedParamsType speed;
spinParamsType spin;
flightParamsType fData;
speed.totalSpeed=200.0f;
speed.launchAngle=30.0f;
speed.horizontalAngle=5.0f;
spin.totalSpin=1000.0f;
flightParamsType fData;
fData.carry=
calculateFlightModel(&speed,&spin,&fData);
getch();
}
This is what i have done in the main function.It shows error C2275: 'flightParamsType' : illegal use of this type as an expression
see declaration of 'flightParamsType';
syntax error : missing ';' before identifier 'fData'
Here's one example of usage, note that besides the #include directive, there's nothing special that needs to be done.
#include "flight.h"
int main(int argc, char *argv[])
{
spinParamsType a = {1.0f, 1.0f, 1.0f, 1.0f};
spinParamsType b = {2.0f, 2.0f, 2.0f, 2.0f};
flightParamsType c; // I don't know what kind of members this struct have.
calculateFlight(&a, &b, &c);
return 0;
}
Remember to compile all the source code files and link them together
You have an extra, duplicate declaration of flightParamsType fData between the statements:
spin.totalSpin=1000.0f;
flightParamsType fData;
fData.carry=
calculateFlightModel(&speed,&spin,&fData);
(in addition to the one near the beginning of main). That's illegal.
Related
I have a program spread through 3 different C files, with the 2 header files that come with it. I originally had the header "Player.h" that I used to save the "map" structure (among others):
#ifndef PLAYER_H_INCLUDED
#define PLAYER_H_INCLUDED
typedef struct map{
float x;
float y;
}map;
/// some other functions and structures///
#endif
I wanted to move it to its own header file, "Map.h" so its with the other map-related functions and structures.
#ifndef MAP_H_INCLUDED
#define MAP_H_INCLUDED
typedef struct map{
float x;
float y;
}map;
#endif
I made the Map.c file as normal, wrote a function in it that would work, but I didn"t call for the function in main.c yet.
I made the "map" data structure in main.c as followed:
#include "Player.h"
#include "Map.h"
int main(){
... /// some other stuff being done
map map;
map.x = 0;
map.y = 0;
... /// more stuff being done
callfunctioninplayer(&map, /// other variables///)
}
so I call the function in player.c, where I also included map.h using #included "Map.h". But after trying to compile it, it gave me an error everywhere where I asked for the map* map structure with the errors:
In file included from ./main.c:10:0:
./Player.h:21:55: error: unknown type name ‘map’
void movementPlayer(int* inputPlayer, player* player, map* map, unsigned int countFrames);
./Player.h:21:55: error: unknown type name ‘map’
void movementPlayer(int* inputPlayer, player* player, map* map, unsigned int countFrames);
^~~
In file included from ./Map.c:10:0:
./Player.h:21:55: error: unknown type name ‘map’
void movementPlayer(int* inputPlayer, player* player, map* map, unsigned int countFrames);
I checked if map.h was defined everywhere, it is, and I tested it again after putting the map struct back in player.h. Putting it back in player.h worked, but why didn't it in map.h?
I'm sorry if this is asked a lot, I looked through some posts but couldn't find anyone with the same error
It looks like map is being used in Player.h, but Map.h hasn't been included at the point Player.h is included, and presumably you didn't include Map.h in Player.h.
If Player.h needs definitions in Map.h, then you need to #include "Map.h" in Player.h.
The struct declaration in my main.c file. I have the function prototype declared but not shown.
typedef struct data
{
int t;
float tp, tf, tt;
} reactorData;
int main()
{
reactorData reactorOne[21];
//other stuff
}
This is the function giving me errors in my function.c file. Specifically in the printf() statement.
typedef struct data reactorData; //this is what I have up top
void reactorOutput(reactorData * data)
{
int c;
for (c=0;c<21;c++)
{
printf(" %3d\t %.0f\t %.0f\t %.0f\n",c, data[c].tp, data[c].tf, data[c].tt);
}
}
The error reads:
|error: invalid use of undefined type 'struct data'|
The function itself works perfectly fine/ I've tested it within main. Its only when I have it in functions.c it doesn't work.
New structs and type definition that must be shared across different compile units are best placed in a header file:
// mystructh.h
#ifndef MYSTRUCT_H
#define MYSTRUCT_H
typedef struct data
{
int t;
float tp, tf, tt;
} reactorData;
void reactorOutput(reactorData * data);
// other stuff
#endif
then in the other c files you have to include the header
main.c
#include <stdio.h>
#include "mystruct.h"
int main(void)
{
reactorData reactorOne[21];
// for example
reactorOutput(reactorOne);
//other stuff
}
functions.c
// functions.c
#include "mystruct.h"
void reactorOutput(reactorData * data)
{
int c;
for (c=0;c<21;c++)
{
printf(" %3d\t %.0f\t %.0f\t %.0f\n",c, data[c].tp, data[c].tf, data[c].tt);
}
}
The problem with your version is that struct data is only defined in main.c.
When the compiler compiles functions.c, it doesn't know what struct data is.
That's why you have to use header files live shown above.
I have an ILE C projet on the as400 which, when being linked, gives me either an error of multiple redefinition of global variables or undefined references if I put the global variable extern.
Here is the code in its simplest form:
main:
#include "Header1"
int main(int argc, char** argv){
int x = Foo();
return 0;
}
Header1
#ifndef HEADER1
#define HEADER1
struct MyStruct{
int x;
};
struct MyStruct g_myStruct; /* My global struct variable. */
int Foo(void);
#endif
Header1 implementation
#include "Header1"
#include "Header2"
int Foo(void){
g_myStruct.x = 432;
return Bar();
}
Header2
#ifndef HEADER2
#define HEADER2
int Bar(void);
#endif
Header2 implementation
#include "Header2"
#include "Header1"
int Bar(void){
return g_myStruct.x;
}
Each file compiles fine. Only when I try to link them I get the following error:
Multiple strong definitions . . . . . . . . . : 2
Symbol Type Library Object Bound Identifier
*MODULE MYLIB 1 *YES g_myStruct
*MODULE MYLIB I2 *YES g_myStruct
With the extern keyword in front of my global struct declaration, I get this error:
Unresolved references . . . . . . . . . . . . : 2
Symbol Type Library Object Bound Identifier
*MODULE MYLIB I1 *YES g_myStruct
*MODULE MYLIB I2 *YES g_myStruct
You're including file header1.h in several different source files.
This leads to several different instances of g_myStruct, thus multiple redefinition.
Declare this variable extern in file header1.h, and instantiate it in one of the source files.
For example:
File header1.h:
extern struct MyStruct g_myStruct; /* My global struct variable. */
File header1.c:
struct MyStruct g_myStruct; /* My global struct variable. */
Global variables work pretty much like global functions.
In the header file, you put a declaration. For functions, this looks like:
int Foo(void); // or 'extern int Foo(void);'
For variables, you need extern (this is optional for functions):
extern struct MyStruct g_myStruct;
Then, in the implementation file, you put the definitions:
#include "Header1"
struct MyStruct g_myStruct;
int Foo(void){
...
}
This is a question that I have been reading a lot about but I am still unclear how things work as I am getting building errors or incomplete type lint errors.
I have a project requiring opengl that I am writing low level opengl function, then I want to build on top of those translation [here after referred to as TU] units to build more higher level TU.
for example.
I have a common_structs.h that defines a few things like this:
common_structs.h
#ifndef COMMON_STRUCTS_H
#define COMMON_STRUCTS_H
#include <OpenGL.h>
struct renderable_2D {
...
float angle;
GLshort vertex_count;
GLfloat vertices[12];
GLfloat colors[16];
GLshort indices[6];
GLfloat tex_coords[8];
...
};
struct texture_2D {
...
GLuint texture_id;
...
};
struct default_shader {
const char* vertex_shader_srouce;
const char* fragment_shader_source;
GLuint shader_program;
};
struct camera_2D {....};
#endif /* COMMON_STRUCTS_H */
I declared these structs in their own header file to avoid circular dependencies.
This file isn't compiled but I need to #include opengl to clear the lint warnings for the GLtypes.
Next up there are header and source TU that implement the functionality for these types. Typically
Let's look at renderable_2D for example:
#ifndef RENDERABLE_2D_H
#define RENDERABLE_2D_H
#include "../../common/common_structs.h" //include the definition of renderable_2d
struct renderable_2D* ren2d_new(void);
struct renderable_2D* ren2d_set_uv(struct renderable_2D* out, const float uv[]);
struct renderable_2D* ren2d_set_tint(struct renderable_2D* out, const float r,
const float g, const float b,
const float a);
struct renderable_2D* ren2d_set_alpha(struct renderable_2D* out, const float a);
struct renderable_2D* rend2d_set_z_index(struct renderable_2D* out,
const int index);
struct renderable_2D* rend2d_set_angle(struct renderable_2D* out,
const float angle);
#endif /* RENDERABLE_2D_H */
and the some of the source file for this:
#include "../headers/renderable_2D.h"
#include <stdlib.h>
this file includes the header and implement the functions. Next there's one more header only file.
core_graphics.h
#ifndef CORE_GRAPHICS_H
#define CORE_GRAPHICS_H
#include "renderable_2D/headers/renderable_2D.h"
#include "default_shader/headers/default_shader_2D.h"
#include "default_camera_2D/headers/default_camera_2D.h"
#endif /* CORE_GRAPHICS_H */
this is a header only TU again and will include all the sub TU that make up this part of the library. It's supposed to be built as a shared library so next up the ladder is another shared library that will
#include core_graphics/core_graphics.h
this is where the problem comes in. Say I have another TU main.c, main looks like this.
main.c
#include "core_graphics/core_graphics.h"
#include <stdlib.h>
int main(int argc, char* argv[])
{
struct renderable_2D* rend = (struct renderable_2D*)calloc(1, sizeof(struct renderable_2D));
return 0;
}
the line where I try to calloc the struct I get lint errors saying
invalid application of 'sizeof' to an incomplete type 'struct renderable_2d'
now incomplete types says it cant find the definition of the struct renderable_2D but, from the source listed above, should it get included?
core_graphics.h has the definition of common_structs.h which holds the definition of the struct?
struct renderable_2D --> common_structs.h --> core_graphics.h --> main.c
Okay, I have a main source called main.c, a header file called test.h, and another class called handBeraknare.c.
Im trying to make my code a bit more readable by transfeering some of my methods to the class handBeraknare.c.
So in main.c i have a struct that looks like this:
typedef struct kort{
int draget;
char farg;
int nummer;
struct kort *next;
}kort; `
In main.c i create a couple of these using kort k=(kort*)malloc(sizeof(kort)); and put them into an array. What im trying to achive is to send this array of kort to a function in handBeraknare.c but I get some sort of weird error "in file included from handBeraknare.c".
Im gussing this has to do with the headerfile now knowing what "kort" is (my struct). Anyway, here's some of the code:
// in test.h
int beraknaFarg(kort kortHand[]);
// in handBeraknare.c
#include <stdio.h>
#include "test.h"
int beraknaFarg(kort kortHand[]){
char c = kortHand[0].farg;
int i;
for (i=1;i<5;i++){
if (kortHand[i].farg!=c){
printf("inte färg");
system("pause");
//Spelaren har inte färg. Retunera 0
return 0;
}
}
//Spelaren har färg. Retunera 1
printf("!!!!färg");
system("pause");
return 1;
}
//part of the main class. Calling function test()
// which calls the method beraknaHand which exists in handBeraknare.c
#include "test.h"
...
int main(int argc, char *argv[])
{
test();
}
// the testfunction in my mainclass
void test(){
char farg[4]={'S','K','R','J'};
int nummer[14]={0,2,3,4,5,6,7,8,9,10,11,12,13,14};
kort kortArray[52];
kort kortHand[5];
kort *k;
k=(kort*)malloc(sizeof(kort));
k->farg='s';
k->nummer=5;
kortHand[0]=*k;
k->farg='s';
k->nummer=11;
kortHand[1]=*k;
k->farg='s';
k->nummer=12;
kortHand[2]=*k;
k->farg='s';
k->nummer=11;
kortHand[3]=*k;
k->farg='s';
k->nummer=9;
kortHand[4]=*k;
beraknaFarg(kortHand);
Make test.h to read
typedef struct kort{
int draget;
char farg;
int nummer;
struct kort *next;
} kort;
int beraknaFarg(kort kortHand[]);
and remove the typedef from main.c
You need to define the typedef in the header file, and then include the header file in the C file you want it to use it.
in addition , because it is typedef defintion and not declaration , you need to define it in the C file.
I.E
h file:
typedef strcut
{
int a;
....
}t_struct_type;
c file:
t_struct_type struct_var;
if you want to use struct_var in more than one c file , you need to add the extern keyword in the h file. like this : extern t_strcut_type struct_var