944,129 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 7626
  • C RSS
Sep 12th, 2007
0

Multiple files

Expand Post »
I have this C program with multiple files and I want to introduce a new file called enemy.c.
and I'm wondering if I'm going about this right.

game.h
  1. #ifndef GAME_
  2. #define GAME_
  3.  
  4. extern void game (void);
  5. extern void enemyFunc (void);
  6. extern int enemyPosX;
  7.  
  8. #endif
main.c
  1. int main (void)
  2. {
  3. game();
  4. }
game.c
  1. #include "game.h"
  2.  
  3. int enemyPosX = 452;
  4.  
  5. void game (void)
  6. {
  7. enemyFunc();
  8. }
enemy.c
  1. #include "game.h"
  2.  
  3. void enemyFunc (void)
  4. {
  5. enemyPosX -= 4;
  6. }
The program works but I'm wondering if I should make a new file called enemy.h
and put

extern void enemyFunc (void);
in it instead of in game.h, to have a header defining each .c file?
Similar Threads
Reputation Points: 78
Solved Threads: 22
Posting Whiz
Colin Mac is offline Offline
327 posts
since Sep 2006
Sep 12th, 2007
0

Re: Multiple files

>The program works but I'm wondering if I should make a new file called enemy.h
You can if you'd like. I don't see any benefit for it at this point though.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Sep 12th, 2007
0

Re: Multiple files

If I were to do it, I would need to include "game.h" in enemy.c as well seeing as I'm using the variable enemyPosX from it?
Last edited by Colin Mac; Sep 12th, 2007 at 5:22 pm.
Reputation Points: 78
Solved Threads: 22
Posting Whiz
Colin Mac is offline Offline
327 posts
since Sep 2006
Sep 12th, 2007
0

Re: Multiple files

>If I were to do it, I would need to include "game.h" in enemy.c
>as well seeing as I'm using the variable enemyPosX from it?
I would move everything that pertains to enemies into enemy.h. Then you would need to include enemy.h in game.c, which is a smidge more intuitive.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Sep 12th, 2007
0

Re: Multiple files

Thanks for your help.
Reputation Points: 78
Solved Threads: 22
Posting Whiz
Colin Mac is offline Offline
327 posts
since Sep 2006
Mar 27th, 2010
1
Re: Multiple files
Hi I wrote a code that may serve a good example for future visitors. Thought I'll post it anyway even though ur question has been answered.

  1. /* Working with Multiple C files Example program by virgoptrex (c) 2010 */
  2. /* All extern variables and structures defined here */
  3. /*This is lol.h */
  4.  
  5. #ifndef _LOL
  6. #define _LOL
  7.  
  8. extern int howdie;
  9. #define me 10
  10. extern void lol2();
  11. extern void lol3();
  12. extern int a1;
  13. extern int b1;
  14.  
  15.  
  16. extern int hello();
  17.  
  18. typedef struct lol123{
  19.  
  20. int dude;
  21. char alpha;
  22.  
  23. }lol123;
  24.  
  25.  
  26. extern lol123 lolol;
  27.  
  28. #endif

  1. /* Working with Multiple C files Example program by virgoptrex (c) 2010 */
  2. /* always a good practice to make a common group file*/
  3. /* There may be multiple common group files in a project*/
  4. /* This file is common to 'lol1.c','lol2.c','lol3.c'*/
  5. /*This is common.c */
  6.  
  7. #include "lol.h"
  8.  
  9. int hello();
  10. void lol2();
  11. void lol3();
  12. lol123 lolol;
  13.  
  14. int a1;
  15. int b1;
  16.  
  17. int hello(){
  18. int howdie=10;
  19. return howdie;
  20. }

  1. /* Working with Multiple C files Example program by virgoptrex (c) 2010 */
  2. /*This is lol1.c */
  3.  
  4. #include <stdio.h>
  5. #include "lol.h"
  6.  
  7.  
  8. int main()
  9. {
  10. printf ("lol1:hello+me %d\n", hello()+me);
  11. lol2();
  12. lol3();
  13. return 0;
  14. }

  1. /* Working with Multiple C files Example program by virgoptrex (c) 2010 */
  2. /*This is lol2.c */
  3.  
  4. #include <stdio.h>
  5. #include "lol.h"
  6.  
  7. /* please don't write something like 'b1=20' here outside void lol2() function */
  8.  
  9. void lol2()
  10. {
  11. b1=20;
  12. lolol.alpha='a';
  13. lolol.dude =12;
  14. printf ("This is lol2: %d\n", hello());
  15. printf ("lol2: alpha = %c and dude+b1 = %d\n", lolol.alpha, lolol.dude+b1 );
  16. }

  1. /* Working with Multiple C files Example program by virgoptrex (c) 2010 */
  2. /*This is lol3.c */
  3.  
  4. #include <stdio.h>
  5. #include "lol.h"
  6.  
  7.  
  8.  
  9. void lol3()
  10. {
  11. lolol.alpha='b';
  12. lolol.dude =99;
  13. a1=2;
  14. printf ("hello from lol3 %d\n", hello()+me+me);
  15. printf("lol3: alpha %c and dude+a1 %d \n", lolol.alpha, lolol.dude+a1 );
  16.  
  17. }

  1. #The Makefile (c) 2010 by Virgoptrex
  2. sample:
  3. gcc -c lol1.c lol2.c lol3.c common.c
  4. gcc lol1.o lol2.o lol3.o common.o -o lol
  5.  
  6. clean:
  7. rm -r lol1.o lol2.o lol3.o common.o lol
Reputation Points: 10
Solved Threads: 0
Newbie Poster
virgoptrex is offline Offline
1 posts
since Mar 2010

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Passing down a typedef array to a function
Next Thread in C Forum Timeline: mpi master slave sum





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC