include multiple files in project

For the life of me I cannot get my project to compile when I split it up into a few different files. Here's some sample code of my problem (Which lies in a global struct)

main.cpp
1
2
3
4
5
6
7
8
#include "header.h"

int main()
{
    do_one(); do_two(); do_three();
    GD.start = 0;
    return 0;
}


one.cpp
1
2
3
4
#include "header.h"

void do_one( ) { GD.start = 5;  return; }
void do_two( ) { GD.start = 10; return; }


three.cpp
1
2
3
#include "header.h"

void do_three() { GD.start = 15; return; }


and the "header.h"
1
2
3
4
5
void do_one();
void do_two();
void do_three();

struct _GD { int start; } GD;



I've tried using extern, and #define header_h_ code, and nothing seems to work. I need this struct to be a global struct -- since the full code this struct is pretty much a collection of a bunch of global variable. When the project was started, different breaks that are in the project, etc.

I get the error -- multiple definitions of _GD -- which I'm pretty sure is because header.h is being added in each instance of the .cpp files, and creating multiple structs with the same data
The problem is that you're not telling the compiler to include one.cpp and three.cpp in the compilation. In the commandline, if you're using g++, you would have to write:
g++ main.cpp one.cpp three.cpp -o (output executable)
to tell the compiler to include one and three in the compilation.

However, if you're using an IDE that isn't GCC-based, I can't help you. Sorry about that.
Last edited on
Your problem is you're making a global variable (and doing it wrong)

 
struct _GD { int start; } GD;


This line does 2 things.

1) it defines a struct whose name is '_GD'
2) it defines an object of that strut whose name is 'GD'

It's effectively the same thing as doing this:

1
2
struct _GD { int start; };
_GD GD;


This creates a global variable of type '_GD'. However it creates this variable in every .cpp file so that the linker doesn't know which version of the variable you meant to use.

Globals are fugly and take a bit of finnagling to get working. My recommendation is to avoid them completely, but if you just want to hack something together to make this work, check out this link for some ideas:

http://cplusplus.com/forum/articles/10627/page2.html

But again I must urge you to avoid using global variables.
Last edited on
Are you sure the error says that *_*GD, not GD, is multiply defined? It would make sense that it complained about multiple definitions of GD, since each .cpp is defining it as a global.

If that's the case, move the definition of GD to a .cpp (any will do) and change the header to this:
struct _GD/*...*/;
extern _GD GD;

EDIT: Arg! One minute too slow.
Last edited on
Thanks I got it to work

What's the preferred method over globals in this situation?

Global Variables that are in my struct --
Time of Start of Project
Serial Number of first product made ( a list is created that uses this)
Serial number of the last product made
Total products
... etc

These global variables are being used in every function of the program and don't want to have to pass local variables between each function.
The best use of global data is to keep information that is not directly related to any functionality in particular. For example, a struct containing command line options, a variable containing how many cores the system has, a variable containing the endianness of the system, etc. The rule of thumb is: if it's data that's not linked to any function in particular and it's written just once and then only read, there's a good chance it should be global. If either or none of these conditions are met, then make it local and pass it around. Good design comes first.
Topic archived. No new replies allowed.