NO

Author Topic: two or more code files in one project in Pelles C  (Read 5454 times)

vedro-compota

  • Guest
two or more code files in one project in Pelles C
« on: December 06, 2011, 06:19:31 PM »
hi there!))

Guys  , please give me an example of using two or more code files in one project in Pelles C .
I can't understand how to use #include in this case.

big thanks in advance)

CommonTater

  • Guest
Re: two or more code files in one project in Pelles C
« Reply #1 on: December 06, 2011, 07:45:57 PM »
Source file One.c
Code: [Select]

int multiply(int a, int b)
  { return a * b; }

Header file One.h
Code: [Select]
#ifndef ONE_H
#define ONE_H

int multiply(int a, int b);

#end if

File Main.c
Code: [Select]
#include <stdio.h>
#include "one.h"

int main (void)
  {
     int x = 10;
     int y = 20;
     int result;
     result  = multiply(x,y);

     printf("The result is %d",result);

    return 0;
}

Make a new console project.
Add both .c files.
Build
Run
see Result  = 200



vedro-compota

  • Guest
Re: two or more code files in one project in Pelles C
« Reply #2 on: December 08, 2011, 07:32:29 PM »
thank you) i'll try

vedro-compota

  • Guest
Re: two or more code files in one project in Pelles C
« Reply #3 on: December 18, 2011, 05:30:24 PM »
1) CommenTator , why do we need the definition of constant ONE_ ( we not use this name in Main.c ) ?
How does compiler get know about existance of bond between One.c and One.h - only by function header in One.h?
2) as i understand - it's not necessary to use headers file to compile code from several sources - we need only add it to project?
« Last Edit: December 18, 2011, 06:57:46 PM by vedro-compota »

CommonTater

  • Guest
Re: two or more code files in one project in Pelles C
« Reply #4 on: December 18, 2011, 07:36:42 PM »
The header file carries the function prototype from one file to another.  You are making a promise: "Yes this exists somewhere where the linker can find it".  The compiler trusts you and behaves like the function is present.  The compiler then creates object files for each source file. The linker sees the function called in your compiled object file and looks for it when liking all objects together.

The #ifndef ONE_H ... using the file's name... is called an include guard.  If it is not defined, it defines it... since the entire header file is in the #if statement, it only gets included into each source file once (when the constant is not defined), preventing multiple definition errors.  This is more of a problem in complex program structures, but it's a good habit to get into from the beginning.
 
The attachment shows you how a complex program gets set up as a Pelles C project...



 
« Last Edit: December 18, 2011, 08:01:10 PM by CommonTater »

vedro-compota

  • Guest
Re: two or more code files in one project in Pelles C
« Reply #5 on: December 20, 2011, 09:16:29 AM »
It's very good (full) answer !)  the constant name  - is not dependent on the real file name (as I understand) .
so the preprocessor should firstly  implement "include" statements and only after this  statements like "ifdef" - am i right?

CommonTater

  • Guest
Re: two or more code files in one project in Pelles C
« Reply #6 on: December 20, 2011, 10:18:48 AM »
No.

#ifndef means "if not defined" ... so the only time the header file is read is when the value is not defined.

First time through... value is not defined... #ifndef is executed
Value is defined
prototypes are read

Second time through ... value is defined... $ifndef is NOT excuted
prototypes are not read a second time

vedro-compota

  • Guest
Re: two or more code files in one project in Pelles C
« Reply #7 on: December 20, 2011, 03:23:19 PM »
Quote
Second time through ... value is defined... $ifndef is NOT excuted
prototypes are not read a second time
ok. i've understand you ) thanks)