several Header and C++ datafiles

Hi all.
I would like to structurise my programm a little bit better. Therefore, I have in my mind to use one general main.cpp where I only call a lot of different subfunctions. All these subfunctions are initialized and collected in a header file subfuncttions.hpp. Additionally to that, there is a header file for all variables, GlobalVariables.hpp

I will fill these subfuntions with life in severel separated .cpp data files such that they can be ordered in a nice way.

ex to explain what I mean:
main.cpp:
1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include "GlobalVariables.hpp"
#include "subfuncttions.hpp"
using namespace std;

int main()
{
variable_ = 5;  // defined globally in "GlobalVariables.hpp"
subfunction1(); // call subfunction defined in "subfuncttions.hpp"
}


subfunctions.cpp:
1
2
3
4
5
6
7
8
9
10
#include <iostream>
// #include "GlobalVariables.hpp"  // HERE IS THE POBLEM
#include "subfuncttions.hpp"
using namespace std;

void subfunction1()
{
cout << "TEST " << endl; ;  // is working!
cout << variable_ << endl; // HERE IS THE PROBLEM
}


My problem is either there is a double definition of the variables when I insert #include "GlobalVariables.hpp", or the "globalVariable" variable_ is not known.

Is there any chance to keep my structure like that and solve the problem such that these variables are known in both .cpp data files?

Thanks for the help
Last edited on
User header guards -
1
2
3
4
5
6
7
8
//header.h
#ifndef YOUR_HEADER_NAME
#define YOUR_HEADER_NAME
/* Your
     header
     code
 */
#endif 


Or, at the very first line of the header
 
#pragma once 


This prevents header files to be included when they were already included in this compilation unit.
Last edited on
Unfortunately, no I receive some proplems by compiling with the iostream, here is the error:

/tmp/cc17Pbhi.o: In function `__static_initialization_and_destruction_0':
/usr/include/c++/4.4/iostream:72: multiple definition of `variable_'


And that for each og my globally defined variables.
You have written subfunctions with double "t" by the way.

What exactly did you do now? I mean, how do your headers look right now?
the double "t" was only a mistake in this forum... sorry.

I copy now my two header files (the C++ files are above...):

subfunctions.hpp:
1
2
3
4
5
6
7
#ifndef SUBFUNCTIONS
#define SUBFUNCTIONS
#include "GlobalVariables.hpp"

void subfunction1();

#endif 


GlobalVariables.hpp:
1
2
3
4
5
6
7
#ifndef GlobalVariables
#define GlobalVariables

int variable_;
double variable2_
 
#endif 

By the way, befor you wonder why I will use this structure for only two variable and und subfunction... For sure afterwards there will appear much more variables.
Last edited on
Question: Do you actually need these globals? Because if not (which is most likely the case), you should probably just remove those - or at least put them into a seperate namespace. It sounds like they are colliding with some other global defined in your version of <iostream>.
maybe it also helps to give the information of the makefil, that I use, there are already things included (like OSI file that I need later on)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
CCC = g++

COMPILER_FLAGS = -g -DABACUS_COMPILER_GCC43
OSI_INC_PATH = ${HOME}/coin-Osi/include/coin/
OSI_LIBRARY_PATH = ${HOME}/coin-Osi/lib/

LP_LIBS= -llapack -lblas -lgsl -lgslcblas -lCoinUtils -lOsi 

CCFLAGS = -O $(COMPILER_FLAGS) -I$(OSI_INC_PATH) -I/usr/include/atlas -I${HOME}Programm/include  //here are my Header files stored

LD_FLAGS =  -L$(OSI_LIBRARY_PATH) $(OSI_LIB_PATH) $(LP_LIBS)  -lm


SOURCES = main.cpp  subfunctions.cpp 

OBJECTS = main.o subfunctions.o 

MC_exe: -$(OBJECTS) 
	$(CCC) -o execute $(SOURCES) $(CCFLAGS) $(LD_FLAGS)

Last edited on
It would be much easier for me to use global variables!
But the problem is that I can in example rename them arbitrarely and they seem to collidate with iostream (even with strange variable names...) So that signalise me that the problem is lying somewhere else?
And, my compiler also try to show me were in iostream, variable_ is also defined, such that there are collidating but in the iostream sourcecode there is no such variable!!!
Yes, probably. Global Variables usually tend to make things harder rather than easier, but you are right in saying that if arbitrary names cause collisions then the problem probably lies somewhere else.

Btw, wrap your code between [ code ] [ /code ] tags for easier readability.
1
2
3
//in GlobalVariables.hpp:
extern int variable_;
extern double variable2_;
1
2
3
//in GlobalVariables.cpp:
int variable_;
double variable2_;
@helios

can you shortly explain what extern means (never see this before).

And in my case, what do you mean with GlobalVariables.cpp?
By only using extern int variable_ in the header, the variables in main.cpp and subfunctions.cpp are unknown.
Last edited on
"extern" tells the compiler to look up the definition of a variable somewhere else, and only makes sure that the name (and type) of the variable is known.
and how does this is helping me (sorry for maybe a stupid question but I don't know how to handle this advice)
And in my case, what do you mean with GlobalVariables.cpp?
It means "put these lines in that file, and create the file if it doesn't exist". By adding this file to the build, the variables will become defined, but only once.
Perfect!
Many thanks to both of you!!! No it works and I can keep that structure for my project!
Topic archived. No new replies allowed.