|
How to divide a project? |
Ilyas Salman
Member #6,762
January 2006
|
I searched but didn't found a clear answer or an example. I want to make a seperate file for inputs, so I did a INPUT() function that links to the other file (input.c als tried input.h), but always I had errors, can someone please give me an example for placing the input as a functon or some other method , witch will work. Thanks very much. |
Jonatan Hedborg
Member #4,886
July 2004
|
Im not quite sure i understand the question. How does your file-structure look at the moment? //file.c #include "file.h" void test() { printf("whatever\n"); } //file.h void test(); EDIT: ah yes, also header guards
|
Hrvoje Ban
Member #4,537
April 2004
|
/* input.h - input header */ /* include guard, to prevend this file from being include more then once * inside singe source file */ #ifndef INPUT_H #define INPUT_H /* declaration */ void input(void); #endif
/* input.c - input source file */ #include "input.h" void input(void) { /* ... */ }
/* main.c */ #include "input.h" int main(void) { /* ... */ input(); /* ... */ return 0; } END_OF_MAIN()
|
Ilyas Salman
Member #6,762
January 2006
|
I still have errors, INCREDEMENT_X undeclared,... my codes: if (key[KEY_ESC]) |
Jonatan Hedborg
Member #4,886
July 2004
|
Please use the code tags. Until then i can tell you straight away that it's bad practice to include c files. it's just EVIL. Also, you add increment_x after you include the file, so it never sees them.
|
ReyBrujo
Moderator
January 2001
|
You should use something like this: // input.h #ifndef INPUT_H #define INPUT_H #define INCREMENT_X 0.25 #define INCREMENT_Y 0.25 typedef struct{ double x,y; }VECTOR; extern int user_wants_to_quit; extern volatile int game_time; #endif
-- |
Michael Faerber
Member #4,800
July 2004
|
-- |
Paul Pridham
Member #250
April 2000
|
You know, your life would be so much easier if you just used STL and OpenLayer.
---- |
Ilyas Salman
Member #6,762
January 2006
|
Now, I made paste what ReyBrujo wrote, I had only one error (and few warnings), it didn't recognize the input function, I made the function extern void INPUT(); in main.cpp. Now the program begins but without input. What is the cause? |
Hrvoje Ban
Member #4,537
April 2004
|
You don't make function by using extern (or just ending it with semicolon without body), you tell compiler that it's placed in another file. In ReyBrujo code, in input.h INPUT takes pointer to VECTOR, but in main.cpp it's used without parameters. EDIT: You need to change this in main.cpp while (user_wants_to_quit == FALSE) { INPUT(&sprite_position);
|
ReyBrujo
Moderator
January 2001
|
Oops! Sorry -- |
Ilyas Salman
Member #6,762
January 2006
|
I made the correction, but I have the same problem, I rewrite the code:
// input.h #ifndef INPUT_H #define INPUT_H #define INCREMENT_X 0.25 #define INCREMENT_Y 0.25 typedef struct{ double x,y; }VECTOR; extern int user_wants_to_quit; extern volatile int game_time; #endif
|
ReyBrujo
Moderator
January 2001
|
Add void INPUT(VECTOR *); to input.h. -- |
Ilyas Salman
Member #6,762
January 2006
|
Where exactly? I placed iy before, in the vector and at the end of the vector but I had warnings at the compiler couldn't make an .exe. Thanks. |
ReyBrujo
Moderator
January 2001
|
After the VECTOR. Remember to link against Allegro libraries. -- |
Ilyas Salman
Member #6,762
January 2006
|
I have the same problem:
|
Jonatan Hedborg
Member #4,886
July 2004
|
Don't forget to do a full recompile after you (only) change a header file.
|
Ilyas Salman
Member #6,762
January 2006
|
Yet, I have the same problem, can you please try to compile it on your system? |
ReyBrujo
Moderator
January 2001
|
That is not an error, but a warning. Put a ENTER at the end of all your files. -- |
Ilyas Salman
Member #6,762
January 2006
|
I did what you said, without succes, then I saw that I forgot in input.c the ifndefs, so:
I also recompiled all (with and without the addition of ifndefs to input.c), I have: undefined reference to `INPUT(VECTOR*)' |
ReyBrujo
Moderator
January 2001
|
You don't need the ifdef/endif in the C files, only in the headers (H files). As for the error, have you compiled the input.cpp too? The INPUT(VECTOR *) function is inside input.cpp, if you don't compile it, you will get that undefined error. And remove that END_OF_FUNCTION(INPUT). -- |
Ilyas Salman
Member #6,762
January 2006
|
I don't even have a input.cpp, only the main.cpp, how must it be written? |
ReyBrujo
Moderator
January 2001
|
Your input.cpp file is the one you posted here. Just add it to your project (I am guessing you are using MSVC6). -- |
Ilyas Salman
Member #6,762
January 2006
|
The input.c I changed to input.cpp, and it worked. Thanks!! |
|