Closed Thread Icon

Preserved Topic: c++ custom header files (quick question) Pages that link to <a href="https://ozoneasylum.com/backlink?for=8992" title="Pages that link to Preserved Topic: c++ custom header files (quick question)" rel="nofollow" >Preserved Topic: c++ custom header files (quick question)\

 
Author Thread
ninmonkey
Nervous Wreck (II) Inmate

From:
Insane since: Nov 2003

posted posted 01-17-2004 16:26

I have a header file with the following

code:
#ifndef ANTSHARE_H
#define ANTSHARE_H
//antshare.h

#include <string>
using namespace std;

//some vars ...

void log(string);
void fps(void);

#endif

I need to include this file in 2 different files. As soon as I do that, I get "multiple definition of 'foo'" for every variable in antshare.h

When I googled, the answer seems to be what I already did, #ifndef, #define, #endif.

thanks,
--monkey

Rooster
Bipolar (III) Inmate

From: the uterus
Insane since: Nov 2002

posted posted 01-17-2004 16:42

Global Variables and extern

"It is possible to create a global variable in one file and access it from another file. In order to do this, the variable must be declared in both files, but the keyword extern must precede the "second" declaration."

[This message has been edited by Rooster (edited 01-17-2004).]

ninmonkey
Nervous Wreck (II) Inmate

From:
Insane since: Nov 2003

posted posted 01-17-2004 17:11

Shouldn't

code:
extern "C++"
{
//some vars
}

Work? It doesn't, but prefixing every variable with "extern" doesn't work either... (then I get in function foo() undefined reference to bar)

Rooster
Bipolar (III) Inmate

From: the uterus
Insane since: Nov 2002

posted posted 01-17-2004 17:34

None of the variables in antshare.h need to be prefixed with 'extern', the variables need to be prefixed with extern when you want to use them within another file that includes the antshare.h.

Example:

code:
#ifndef SOME_FILE_THAT_INCLUDES_ANTSHARE_H
#define SOME_FILE_THAT_INCLUDES_ANTSHARE_H

#include "antshare.h"

//In order to use one of the globals declared in "antshare.h"
//in this file, it needs to decaled 'again' (this time with extern)

extern int someVariableDeclaredInAntShare;

//use someVariableDeclaredInAntShare

#endif //SOME_FILE_THAT_INCLUDES_ANTSHARE_H



[Cell 1303]

Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 01-17-2004 18:43

What you need to realize is that each .cpp file is compiled separately. The #ifdef stuff helps during each of these compilations so that the header file is not read more than once. However, the header file may be read while compiling a.cpp, and then again while compiling b.cpp, since a.cpp and b.cpp are compiled separately.

So, if the header file contains a variable declaration, like "int i;" then you've effectively declared an integer called i in two separate .cpp files at once. That's not allowed.

The solution, as Rooster has pointed out, is to declare the variable in the header file as "extern":

extern int i;

That's very similar to a function prototype. A function prototype says "this function exists in one of the .cpp files being compiled" without actually *creating* the function. An extern variable says "this variable exists somewhere" without actually creating the variable.

To actually create the variable, you must put "int i;" in one of the .cpp files. Since the .cpp files aren't included, the variable will only be created in one place.

« BackwardsOnwards »

Show Forum Drop Down Menu