Solved

curious about multiple defined symbols.

Posted on 2004-09-08
2
219 Views
Last Modified: 2010-04-01
Let's say I have a header file, that is included by two different .cpp files.

//test.h
#ifndef TEST_H
#define TEST_H

int g_int;
#endif

//test1.cpp
#include "test.h"
extern int g_int;
...

//test2.cpp
#include "test.h"
extern int g_int;

I am getting the link error LNK2005: "int g_int" (?g_int@@3HA) already defined in test1.obj

Now I thought that declaring them as extern in the .cpp files would tell the compiler that the g_int variable was already declared externally.  I know that I can move the g_int declaration to the .cpp file and it will work fine.  I was just curious if there was any way at all to have a global variable in a header file.
0
Comment
Question by:lwinkenb
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 23

Accepted Solution

by:
brettmjohnson earned 125 total points
ID: 12009540
Header files are physically included into the including source in-line,
so your construct is equivalent to:

//test1.cpp
int g_int;
extern int g_int;
...

//test2.cpp
int g_int;
extern int g_int;

You see that g_int is now defined in both test1 and test2.
Generally, the header file contains the declaration ("extern int g_int;")
while the cpp file contains the definition.  You have it backward.

0
 
LVL 5

Author Comment

by:lwinkenb
ID: 12009818
Thanks a lot.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question