2

noob question right here. How do you pass values between 2 different cpp files in the same project? Do you make objects? if yes, how does the other cpp file see it? some enlightment pls..

EDIT: some clarifications. I'm trying to interface direct input with a program (of which I have the plugins sdk). I'm trying to interface a joystick with it. It seems that there is no main function when I look through the code, but I might be wrong (like, I might not look in the right files). I know programming, and pointers and stuff, classes. Is there anything I should learn or get into in order to achieve what I want?

jello
  • 715
  • 5
  • 11
  • 21
  • 2
    This is a bit unclear. What is your problem, what do you want to achieve? – Björn Pollex Mar 30 '10 at 20:39
  • 1
    Not to sound arrogant, but your statement `I know programming, and pointers and stuff, classes` gives a way quite a bit on your level of experience. As to your question what you should learn, well, how about Object Oriented Programming? – Björn Pollex Mar 30 '10 at 21:30
  • 1
    isn't OO Programming classes and stuff? i go to school in computer science. it's the basic stuff they teach – jello Mar 30 '10 at 21:47

4 Answers4

4

In all but few cases it's a bad idea to share data among compilation units. A compilation unit, just to get you up to speed with the C++ terminology, usually effectively refers to an implementation file (with extension .cpp, or .cc etc.). The way we have the various compilation units "communicate" with each other is with header files and functions, rather than raw data.

Suppose we have an implementation file main.cc and a second implementation file human.cc. We want main.cc to communicate with human.cc. Here we go:

// main.cc
#include "human.hh"
int main()
{
    make_the_human_dance(60);
    return 0;
}


// human.hh
void make_the_human_dance(int duration);


// human.cc
#include "human.hh"
void make_the_human_dance(int duration)
{
    // define how a human dances properly
    // ...
}

Under the same principle you can use classes for communication. Declare the class in the header file and define the class' methods in the implementation file. Sometimes you must provide the implementation of functions in the header files, but that is already going offtopic.

wilhelmtell
  • 53,297
  • 19
  • 89
  • 128
  • +1 because examples are really the best way to express ideas. I was too lazy for one. – Björn Pollex Mar 30 '10 at 21:31
  • I think I know what classes, headers and implementations are. But I thought i was missing something. like, something I didn't know.... so by your answer, that means that you can't pass values to implementation files without a main? how about when you write plugins? or dlls? – jello Mar 30 '10 at 21:51
  • @jello No, we need a `main()` function if we are to build the program as an executable. Because `main()` is the program's point of entry, the first thing the operating system will look for when we run the built executable. Now, if you really want to share data between any two compilation units then you can most certainly do that. What you do is declare a global variable, and then declare it again with the `extern` keyword in any other compilation unit where you want to use that global variable. I can extend my answer to cover this if you wish, but this technique is usually something to avoid. – wilhelmtell Mar 30 '10 at 22:13
  • @jello Also, there's _always_ something we don't know. This is inherent to C++. ;) – wilhelmtell Mar 30 '10 at 22:13
  • @wilhelmtell thx for the extern. you too SpaceCowboy. i'll try it tomorrow. but when you develop plugins, it's dlls right? not executables? – jello Mar 31 '10 at 00:03
  • @wilhelmtell re `extern`: You _define_ the variable in one translation unit and _declare_ it (preferably by including the declaration) in the ones that want to access it. See http://stackoverflow.com/questions/1410563/1410632#1410632. – sbi Mar 31 '10 at 07:19
3

You could declare a global variable in a header file like so:

extern int GlobalVar;

And in exactly one compilation-unit (cpp-file) you have to initialize it:

int GlobalVar = 5;

Any other compilation unit that includes the header now shares the same instance of the global variable (I hope that syntax is correct, i rarely use it).

One should mention, that your question indicates a general lack of understanding of how programs in C++ should be organized. In short, you usually have a file called main.cpp that contains the entry-point of your program. The rest, in C++, is done in classes most of the time. A class is usually split into two parts, the declaration of the class in a header file, and the implementation of the class in a cpp file. To use a class, you include the corresponding header file. This is the short version, and there is much more to tell, but this should give you a good starting point.

Björn Pollex
  • 70,106
  • 28
  • 177
  • 265
0

Normally you define a function in one cpp file, then declare that function as extern in a header, and include the header in whatever other cpp file needs to use the function. You can then write code that calls the function normally. At link time you need to supply the object files that resulted from both cpp files, and the linker ...links them together, so the function call in one file passes the value correctly as you call the function that was defined in the other cpp file.

Jerry Coffin
  • 437,173
  • 71
  • 570
  • 1,035
  • 1
    Note that `extern` is implicit for function declarations. While you can write `extern void f()`, nobody ever does it and so it might confuse readers. – sbi Mar 30 '10 at 20:43
0

Referencing code in a different file typically makes use of #include

McAden
  • 12,757
  • 4
  • 35
  • 60