Multiple Files Problems

Thread Solved

Join Date: May 2008
Posts: 374
Reputation: Clockowl is on a distinguished road 
Solved Threads: 27
Clockowl's Avatar
Clockowl Clockowl is offline Offline
Posting Whiz

Multiple Files Problems

 
0
  #1
May 17th, 2008
hai.

I'm trying to compile a program split up across multiple files. The code is far from complete, but I noticed lots of compiler warnings and errors. This ought to be due to splitting the code up: I thought the preprocessor simply copy and pasted the code into main.c when I used #include.

It seems it does not, but I can't get it to work. The code is included in a zip file. The project file is a Code::Blocks one.

I want to learn how to effectively split a program up into multiple files. I've googled a bit, but couldn't find a tutorial that I understood completely.

Thanks in advance,
Last edited by Clockowl; May 17th, 2008 at 1:20 pm. Reason: Refined words.
Attached Files
File Type: zip TetriminoFalls.zip (5.4 KB, 4 views)
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,951
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Multiple Files Problems

 
0
  #2
May 17th, 2008
R U Japanese? (osu?)

You have misunderstood the purpose of #include files.

Each separate module (.c file) should be compiled separately (into a object file: .o or .obj).
Each module should have a header file (.h file) that prototypes what can be found in the object files.

For example, suppose you have a module named "hanasu" that just prints a greeting to the console:
  1. /* hanasu.c */
  2.  
  3. #include <stdio.h>
  4.  
  5. void hanashite( int count )
  6. {
  7. for (; count > 0; count--)
  8. puts( "wan wan yo!\n" );
  9. }

Now, you want to be able to use this procedure in your main program. To do that, the main program must know what the procedure looks like. This is where your header file comes in.
  1. /* hanasu.h */
  2.  
  3. #ifndef HANASU_H
  4. #define HANASU_H
  5.  
  6. void hanashite( int count );
  7.  
  8. #endif

Now you can use it in your main program:
  1. /* woof.c */
  2.  
  3. #include "hanasu.h"
  4.  
  5. int main()
  6. {
  7. hanashite( 3 );
  8. return 0;
  9. }

To compile, make sure Code::Blocks is aware of each .c file in your project. (It has been a long time since I've used Code::Blocks, but there should be a Project menu item where you can specify which files are part of your project.)

On the command-line, you'd use GCC thus:
gcc woof.c hanasu.c -o woof
Hope this helps.
Last edited by Duoas; May 17th, 2008 at 3:13 pm.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 374
Reputation: Clockowl is on a distinguished road 
Solved Threads: 27
Clockowl's Avatar
Clockowl Clockowl is offline Offline
Posting Whiz

Re: Multiple Files Problems

 
0
  #3
May 17th, 2008
Thanks, I'm not Japanese but Dutch. Where'd you get that idea?

Anyway,

I know that you can use multiple files like that, and I've used them like that, putting functions in one file, then including it. Problem is I want some globals to be used in the included files as well. Example:

main.c
  1. enum menuOptions {NO_MENU, MAIN_MENU} menu = MAIN_MENU;
  2. ...
  3. //herein all the draw functions
  4. #include "renderCode.c"

renderCode.c
  1. void drawMenu(extern enum menuOptions menuType){
  2. ...
  3. switch(menuType){
  4. case MAIN_MENU:
  5. ...

I want to be able to use the enum menuOptions in renderCode.c AND in main.c. I tried some things with extern, but I couldn't figure it out. I know it's possible, but how?

Thanks in advance,
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 374
Reputation: Clockowl is on a distinguished road 
Solved Threads: 27
Clockowl's Avatar
Clockowl Clockowl is offline Offline
Posting Whiz

Re: Multiple Files Problems

 
0
  #4
May 17th, 2008
I figured it out thanks to this nice tutorial. Thanks for your help.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 374
Reputation: Clockowl is on a distinguished road 
Solved Threads: 27
Clockowl's Avatar
Clockowl Clockowl is offline Offline
Posting Whiz

Re: Multiple Files Problems

 
0
  #5
May 17th, 2008
I almost fixed it, but there's one problem I can't seem to fix: Multiple definitions. I've marked every header file with it's own "header notifier", but the compiler/linker can't help but scream:
  1. obj\Debug\main.o||In function `drawScene':|
  2. C:\Code\TetriminoFalls\renderCode.c|6|multiple definition of `drawScene'|
  3. obj\Debug\renderCode.o:C:\Code\TetriminoFalls\renderCode.c|6|first defined here|
  4. obj\Debug\main.o||In function `drawMenu':|
  5. C:\Code\TetriminoFalls\renderCode.c|14|multiple definition of `drawMenu'|
  6. ...
  7. ||=== Build finished: 10 errors, 0 warnings ===|

Any ideas on howto? I've uploaded the update version with header files!

Thanks in advance, again,

PS:
Small view of a header file:
  1. #ifndef TETRIMINORENDERH
  2. void drawScene(void);
  3. void drawMenu(enum menuOptions menuType);
  4. void drawText(GLint fontlist, char *text, GLfloat x, GLfloat y, GLfloat z);
  5. #define TETRIMINORENDERH
  6. #endif
Last edited by Clockowl; May 17th, 2008 at 4:49 pm. Reason: Added preview for header files.
Attached Files
File Type: zip TetriminoFallsSRC.zip (5.8 KB, 2 views)
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 374
Reputation: Clockowl is on a distinguished road 
Solved Threads: 27
Clockowl's Avatar
Clockowl Clockowl is offline Offline
Posting Whiz

Re: Multiple Files Problems

 
0
  #6
May 17th, 2008
...and solved again.

Found out that I only need to include the header files. I think I understand how it works now. Compiler compiles all the .c source files to .o object files. The linker then looks to the code and with header files determines what object links to the other ones. So main doesn't need to know WHAT function X does, just what it's name is (so the linker can look it up) and what it's parameters are so it can check parameters.

Phew. It works. yeey.

Byebye.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,951
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Multiple Files Problems

 
0
  #7
May 17th, 2008
I'm back.

...and solved again.

...

Phew. It works. yeey.
Glad to have not been of help.

Thanks, I'm not Japanese but Dutch. Where'd you get that idea?
Dutch people say "hai"?
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 374
Reputation: Clockowl is on a distinguished road 
Solved Threads: 27
Clockowl's Avatar
Clockowl Clockowl is offline Offline
Posting Whiz

Re: Multiple Files Problems

 
0
  #8
May 17th, 2008
No, people on OIFY do.

... It's from my friends. I'm not an active OIFY member due to too much pictures of animals like sung of in Bloodhound Gans's "The Bad Touch"
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 374
Reputation: Clockowl is on a distinguished road 
Solved Threads: 27
Clockowl's Avatar
Clockowl Clockowl is offline Offline
Posting Whiz

Re: Multiple Files Problems

 
0
  #9
May 17th, 2008
No, people on OIFY do.

... It's from my friends. I'm not an active OIFY member due to too much pictures of animals like sung of in Bloodhound Gans's "The Bad Touch"
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC