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,

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:

/* 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.

/* hanasu.h */

#ifndef HANASU_H
#define HANASU_H

void hanashite( int count );

#endif

Now you can use it in your main program:

/* 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

Hope this helps.

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

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

renderCode.c

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:

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:

#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

...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. :D

I'm back.

...and solved again.

...

Phew. It works. yeey.

Glad to have not been of help. :idea: :)

Thanks, I'm not Japanese but Dutch. Where'd you get that idea?

Dutch people say "hai"?

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"

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"

This question has already been answered. Start a new discussion instead.