Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Okay, I have to understand this whole module thing... (C)
#1
Okay, recently the discussion of multimodule programs in C came up...and I'm not sure I understand it completely.
Using Dev-C++, I'd make a new project. Here are my source files that I add to it:
Code:
/* main.c */
int main() {
print_blarg();
return 0;
}
Code:
/* second.c */
#include <stdio.h>
void print_blarg() {
printf ("Blarg!");
}
And then I compile.
Is that right? I thought I'd have to put the #include of stdio.h and the prototype for print_blarg() inside a header file, and include it in main.c - but I was wrong.
f only life let you press CTRL-Z.
--------------------------------------
Freebasic is like QB, except it doesn't suck.
Reply
#2
Header files are used to store defintions and prototypes. The stdio.h header file for example has prototypes for functions such as printf and scanf, and contains definitions for things like SEEK_SET and stdin.

You only need to include stdio.h in a module if that module uses functions or definitions from it. IMHO, it is bad programming practice to globally include stdio.h because lots of things such as a rendering module or math module dont need standard io functions.

How you split your code into modules is very much a matter of programming style and choice. It makes sense to try and group things together in a module that logically make sense to be grouped together.

You only need to provide prototypes and definitions in a header for functions that will be called outside the module, functions that are called internally only are better defined inside the module, for example:
Code:
/* Blarg.h */
#include "blarg.h" /* Contains prototype for blarg() */

void internal_blarg(void);

void blarg(void) {
  internal_blarg();
}

/* This is only ever called by blarg() */
void internal_blarg() {
  printf("Hello World\n");
}

I would split the code you gave into modules as follows. I have added a structure and a #define to show what else usually goes into header files. Notice that both main.c and blarg.c include blarg.h, because both need the prototype for print_blarg. However main.c doesn't need to include stdio.h, because it does use any of the functions or definitions from it.

Code:
/*
* Blarg.h
*
* Contains prototypes and definitions for blarg
*/

#define MAX_BLARG 100

typedef struct blarg_t {
  int a;
  char *b;
}

void print_blarg(void);

Code:
/* Main.c */

#include "blarg.h"

int main() {
  print_blarg();
  return 0;
}
Code:
/* Blarg.c */

#include <stdio.h>
#include "blarg.h"

void print_blarg(void) {
  printf("Hello World\n");
}

To get a better idea of how header files are structured have a look at the standard ones like stdio.h, they should be stored in an include directory (or do a search for .h files) in your compilers directory.
esus saves.... Passes to Moses, shoots, he scores!
Reply
#3
Okay, question: why do you #include blarg.h twice, in main.c and blarg.c?
f only life let you press CTRL-Z.
--------------------------------------
Freebasic is like QB, except it doesn't suck.
Reply
#4
Quote:Okay, question: why do you #include blarg.h twice, in main.c and blarg.c?

the header files contain the prototype and the .c files contain the code.

Then (using dev-c++) the "project" contains all the .c files, with the .h files in an accessable directory. Then, suppose the function
foo() needs to call foo2()....what to dooooo????

simple.....#include "foo2.h"
in the file
"foo.c"

--------------------------

It took me a long time (and some help from LooseCaboose) before I started using *.h files "correctly"....

Once you get it, it's fairly straight-forward.

Cheers...and keep asking and reading the answers.

Also...remember, for simple programms, you don't need to use projects. You can (at least in dev-c++) make single file programs that look like:

#include <needed_stl_headers>

using namespace bla;

function1(); //a declaration
function2(); //a declaration

int main(){
function1();
function2();
return 0;
}

function1(){ // the definition
// body
}

function2(){ //the definition
//body
}

cheers...and don't take my prototype too seriously...for example, every function (and function prototype) needs a return type....
Reply
#5
Oooh..I think I'm getting it now - they both need to include the header because they really are "seperate" modules.
f only life let you press CTRL-Z.
--------------------------------------
Freebasic is like QB, except it doesn't suck.
Reply
#6
Quote:Oooh..I think I'm getting it now - they both need to include the header because they really are "seperate" modules.

Right...I think...if you were refering to my foo() example..


Here's 1 way to do it...

make the following 3 files:

//foo.h
void foo();
// eof foo.h

//foo.cpp
include <iostream>
using namespace std;
void foo(){
cout << "In foo" << endl;
}
// eof foo.cpp

//main.cpp
#include "foo.h"
int main(){
foo();
return 0;
}
// eof main


now...you make a project and you put "main.cpp" and "foo.cpp" in the project, and put "foo.h" in the same directory with the project (or put it in the project...it doesn't matter)...you can now compile the project.


IFFFFFFFFF you want...you can do the same in a single file...no project needed...

//single file version
#include <iostream>
using namespace std;

void foo();

int main(){
foo();
return 0;
}

void foo(){
cout << "In foo" << endl;
}




Hope this helps
Reply
#7
It does! Thanks!
Yeah, from now I was always putting my functions in the same file...the main module...single source code files. :barf:
Got messy when I have more than 5 functions, structs, arrays, whatever...
f only life let you press CTRL-Z.
--------------------------------------
Freebasic is like QB, except it doesn't suck.
Reply
#8
Quote:It does! Thanks!
Yeah, from now I was always putting my functions in the same file...the main module...single source code files. :barf:
Got messy when I have more than 5 functions, structs, arrays, whatever...

Glad to have helped...

This is part of the reason I reccomend Dev-C++...it allows easy use of single-file compilable code and/or projects. Some other IDE's only work with Projects...which is a barrier for the neophyte...and I was one recently enough to remember.....

Good luck. BTW...have you looked at my cpp encryption prog (current version 1056)? I don't suggest it's good code, but as a c++ novice just a few months ahead of you, and as you have recently expressed interest in encryption, you may find it interesting.
Reply
#9
Well - is it too C++y? Because I'm a C man...
But yeah, I'd love to take a look. Not sure I'd understand any of it, but... Smile
f only life let you press CTRL-Z.
--------------------------------------
Freebasic is like QB, except it doesn't suck.
Reply
#10
Mango, which version of DevC++ are you using? Because it causes a hell of trouble when I am using Allegro in a project. It works fine with single modules. Do i need to pass any special parameters to the mingw32 compiler?
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)