| | |
Multiple Files Problems
Thread Solved |
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,
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.
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:
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.
Now you can use it in your main program:
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:
Hope this helps.
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:
C Syntax (Toggle Plain Text)
/* hanasu.c */ #include <stdio.h> void hanashite( int count ) { for (; count > 0; count--) puts( "wan wan yo!\n" ); }
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.
C Syntax (Toggle Plain Text)
/* hanasu.h */ #ifndef HANASU_H #define HANASU_H void hanashite( int count ); #endif
Now you can use it in your main program:
C Syntax (Toggle Plain Text)
/* woof.c */ #include "hanasu.h" int main() { hanashite( 3 ); return 0; }
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
Last edited by Duoas; May 17th, 2008 at 3:13 pm.
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
renderCode.c
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,
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
C Syntax (Toggle Plain Text)
enum menuOptions {NO_MENU, MAIN_MENU} menu = MAIN_MENU; ... //herein all the draw functions #include "renderCode.c"
renderCode.c
C Syntax (Toggle Plain Text)
void drawMenu(extern enum menuOptions menuType){ ... switch(menuType){ case MAIN_MENU: ...
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,
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:
Any ideas on howto? I've uploaded the update version with header files!
Thanks in advance, again,
PS:
Small view of a header file:
C Syntax (Toggle Plain Text)
obj\Debug\main.o||In function `drawScene':| C:\Code\TetriminoFalls\renderCode.c|6|multiple definition of `drawScene'| obj\Debug\renderCode.o:C:\Code\TetriminoFalls\renderCode.c|6|first defined here| obj\Debug\main.o||In function `drawMenu':| C:\Code\TetriminoFalls\renderCode.c|14|multiple definition of `drawMenu'| ... ||=== 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:
c Syntax (Toggle Plain Text)
#ifndef TETRIMINORENDERH void drawScene(void); void drawMenu(enum menuOptions menuType); void drawText(GLint fontlist, char *text, GLfloat x, GLfloat y, GLfloat z); #define TETRIMINORENDERH #endif
Last edited by Clockowl; May 17th, 2008 at 4:49 pm. Reason: Added preview for header files.
...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.
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.
![]() |
Similar Threads
- Blocking unix files problems (Shell Scripting)
- Moving multiple files to specific subfolders (Visual Basic 4 / 5 / 6)
- Problems With Nero Vision Express (Windows Software)
- init-declarator before "float" (C++)
- Output problems (C++)
- problems writing multiple files (Perl)
- A few problems i've been having - with msn/internet explorer etc (Viruses, Spyware and other Nasties)
- Aurora, DrPmon, MHTMLRedir problems (Viruses, Spyware and other Nasties)
- NTFS problems (OS X)
- System restore, search files& folders doesn't work. HiJackthis log is like reading gr (Viruses, Spyware and other Nasties)
Other Threads in the C Forum
- Previous Thread: Transpose matrix (w\ pointers)
- Next Thread: Clearing Buffer when using the Getch function
| Thread Tools | Search this Thread |
* ansi api array arrays bash binarysearch calculate centimeter changingto char character convert copyanyfile copypdffile createcopyoffile createprocess() csyntax directory dynamic fflush file floatingpointvalidation fork forloop frequency function getlasterror getlogicaldrivestrin givemetehcodez graphics gtkgcurlcompiling gtkwinlinux hardware highest histogram homework i/o ide inches initialization intmain() iso km license linked linkedlist linux linuxsegmentationfault list logical_drives looping loopinsideloop. lowest match matrix microsoft motherboard mqqueue mysql oddnumber odf open opendocumentformat openwebfoundation pdf pointer pointers posix power program programming pyramidusingturboccodes read recursion recv recvblocked repetition reversing scanf scheduling segmentationfault send shape single socketprogramming stack standard strchr string suggestions test unix urboc user variable whythiscodecausesegmentationfault win32api windows.h windowsapi






