Page 1 of 1

Making source code files comunicate

Posted: Sat Jan 21, 2006 12:25 pm
by Fixthestar248
Hi, I`m a newbe, and thought, how do I make source code files communicate. (Newbie to the languages I know ;D) ;

Re:Making source code files comunicate

Posted: Sat Jan 21, 2006 2:11 pm
by earlz
what exactly do you mean "communicate" and which language are you using

the only way i can think of communicating is this:
(in C/C++)

Code: Select all

//SOURCE FILE 1

char my_communicator1;

void my_function1(){
printf("Hi! this is source file #1\n");
printf("my_communicator1=%i\n",my_communicator); //print what the value is
}

//SOURCE FILE 2

extern my_communicator1 //this says that the variable is in a different object file(source file)

extern void my_function1(); //same as above but this is a function

void main(){ //this is the main
my_communicator=4;
my_function1();
my_communicator=243;
my_function1();

}

//END
and also of course this works both ways

keep in mind however that you must link the object files together(compiled source files)


well thats all i got

Re:Making source code files comunicate

Posted: Sun Jan 22, 2006 9:27 am
by Fixthestar248
I`m sorry, I should of talked more, by comunicating(pardon my spelling), I mean like refering to a different file, like in asm, the EXTERN command ( if thats what it`s called, I`ve gotten rusty on my asm)

Re:Making source code files comunicate

Posted: Mon Jan 23, 2006 4:37 am
by Pype.Clicker
simply define the function F1() in source1.c, and call it where you need to. That's all you need

Code: Select all

// source1.c
void myFunc(int x) 
{
     do_something();
}

Code: Select all

// source2.c
int main()
{
     myFunc(20);
     return 0;
}
now, it's likely that you want to make sure source2.c knows how source1.c defined "myFunc". E.g. with the current code, you could perfectly call ``myFunc("Hello World");'' instead and all the compiler could tell you would be "implicit definition of myFunc" ...

To make the definition explicit, what's common to do is to put the definition of the function in a header file.

E.g.

Code: Select all

//mystuff.h
void myFunc(int);

Code: Select all

//source1.c
#include "mystuff.h"
void myFunc(int x) 
 /* compiler will be able to check you're conforming to 
     definition in mystuff.h and warns you otherwise
   */
{
     do_something();
}

Code: Select all

// source2.c
int main()
{
     /* compiler detects you're passing a wrong type of
      *  argument and complains.
      */
     myFunc("Hello World");
     return 0;
}
HTH.