473,398 Members | 2,165 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Multiple Definition Compile Error

mikejfe
12
Hi all. I am having multiple definition compiling errors. I included the #ifndef, #define, #endif lines in my header files. I've seen a post on here suggesting using extern. That didn't work for me either. This is a snippet from my header file.

#ifndef ListVars_h
#define ListVars_h

extern int bonesDL = 3;
extern int marrowDL = 4;

#endif

If I include ListVars.h into multiple .cpp files and compile, I get the error. Any ideas? I'm using Dev-C++ with Windows XP.

Thank you!
-Michael
Feb 6 '07 #1
4 2316
Banfa
9,065 Expert Mod 8TB
You can not initialise in a declaration so you need

Expand|Select|Wrap|Line Numbers
  1. extern int bonesDL;
  2. extern int marrowDL;
  3.  
in your header and

Expand|Select|Wrap|Line Numbers
  1. int bonesDL = 3;
  2. int marrowDL = 4;
  3.  
in a source file somewhere
Feb 6 '07 #2
mikejfe
12
Hmm. Well I tried a few different things and I still get the multiple definition error. Maybe I didn't quite understand your response (sorry). The header file now reads:
Expand|Select|Wrap|Line Numbers
  1. #ifndef ListVars_h
  2. #define ListVars_h
  3.  
  4. extern int bonesDL;
  5.  
  6. #endif
  7.  
One thing I tried was adding a new function:
Expand|Select|Wrap|Line Numbers
  1. #include "ListVars.h"
  2.  
  3. void initVars()
  4. {
  5. int bonesDL=3;
  6. }
  7.  
And then I tried:
Expand|Select|Wrap|Line Numbers
  1. #include "ListVars.h"
  2.  
  3. void initVars()
  4. {
  5. bonesDL=3;
  6. }
  7.  
And finally I tried (as a last effort):
Expand|Select|Wrap|Line Numbers
  1. #include "ListVars.h"
  2.  
  3. int bonesDL=3;
  4. void initVars()
  5. {
  6. }
  7.  
I also tried all of these variations without using extern in the header file.

Thanks for your help,
-Michael
Feb 6 '07 #3
horace1
1,510 Expert 1GB
the declaration in your header files looks OK:
Expand|Select|Wrap|Line Numbers
  1. #ifndef ListVars_h
  2. #define ListVars_h
  3.  
  4. extern int bonesDL;
  5.  
  6. #endif
  7.  
it must be defined in only ONE of your .c files
Expand|Select|Wrap|Line Numbers
  1. int bonesDL=3;
  2.  
if you are getting multiple definitions of this identifier you must have it defined in more than one c file

in this version
Expand|Select|Wrap|Line Numbers
  1. #include "ListVars.h"
  2.  
  3. void initVars()
  4. {
  5. int bonesDL=3;
  6. }
  7.  
you define it as a local variable, remove the 'int'
Feb 6 '07 #4
mikejfe
12
It finally compiled! I'm pretty new to writing C++ programs that are more advanced than "Hello World." My grad professor stuck me on a program that requires knowledge of openGL and C++ ... and I've been struggling. Thank you so much for your help!
Feb 6 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: prettysmurfed | last post by:
Hi all I have this, probably stupid question, how to avoid multiple definitions when a header file is included more than once. I thought, when you wrote the header-file and used the...
5
by: Tony Johansson | last post by:
Hello Experts! I just play around just to try to understand this about multiple inheritance. You have all the class definition below and at the bottom you have the main program. So here I...
10
by: PCHOME | last post by:
Hi! Would someone please help me thess C error(in gcc on Linux)? The compiler continues to give me: readLP.o: In function `Input_Problem': readLP.o(.text+0x0): multiple definition of...
8
by: Tapeesh | last post by:
I have a following piece of code. The code was compiled using g++ class A { public : virtual void fn() = 0; }; class B: virtual private A {
2
by: kai | last post by:
Hello, I wrote a little c++-program (hallo world) and I wanted to compile and link this. my steps: 1. set GCC_EXEC_PREFIX=J:\Software\WinProgs\cyggnu\h-i386-cygwin32\lib\gcc-lib\
11
by: lars.uffmann | last post by:
Easily described problem: Using g++ version 3.3.5 under suse 9.3, bla.h: ----------- #ifndef myTEST #define myTEST ZFSInt test; #endif
3
by: Student | last post by:
Hi all, While compiling a program I had this message : tools.o(.data+0x0): multiple definition of `VAR_1' main.o(.data+0x0): first defined here tools.o(.data+0x4): multiple definition of...
8
by: subramanian100in | last post by:
Suppose I have #include <stdio.h> #include <string.h> size_t strlen(const char *str) { printf("from strlen - %s\n", str); return 0; // return some dummy value
3
by: Antonio Rivas | last post by:
Hello all. I've got a problem of multiple definition in a program that at first glance looks correct (I won't type the whole code, just the relevant one and as examples since seems is a linkage...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.