LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-20-2006, 03:26 PM   #1
bendeco13
Member
 
Registered: Oct 2004
Distribution: Fedora 7
Posts: 232

Rep: Reputation: 30
make multiple definition of ...??????????????


I've been working on this program and it grew very large, so I split it up into individual files. Now I'm trying to set it up with make, but I keep getting this error:
Code:
g++    -c -o test.o test.cpp
g++    -c -o test_config.o test_config.cpp
g++ -o make_test main.o test.o test_config.o
test_config.o:(.bss+0x0): multiple definition of `FVAR'
test.o:(.bss+0x0): first defined here
test_config.o:(.bss+0x4): multiple definition of `SVAR'
test.o:(.bss+0x4): first defined here
collect2: ld returned 1 exit status
make: *** [all] Error 1
These are defined in one header file 'test_options.h' and both test.cpp and test_config.cpp include this file. I used the #ifndef, #define statements, and make 'seems' to ignore this. Here's 'test_options.h':

Code:
#ifndef _TEST_OPTIONS_H
#define _TEST_OPTIONS_H

#include <string>

string FVAR = "FirstVar";
string SVAR = "SecondVar";

#endif
Does anyone know how to fix this?
I tried simply removing '#include "test_options.h"' from the main_config.cpp file, since you would think that it is already included since test.cpp has already included it, but when I do this I get this error:
Code:
g++    -c -o main.o main.cpp
g++    -c -o test.o test.cpp
g++    -c -o test_config.o test_config.cpp
test_config.cpp: In function ‘int Test_Config(std::string)’:
test_config.cpp:9: error: ‘FVAR’ was not declared in this scope
test_config.cpp:10: error: ‘SVAR’ was not declared in this scope
make: *** [test_config.o] Error 1
Any help would be greatly appreciated.
Thanks in advance,
Bendeco
 
Old 04-20-2006, 04:03 PM   #2
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
Header files should not contain definitions of variables.

A variable can be declared multiple times but must be defined only once. Use the keyword extern to declare the variable and then in the source code that will first use the variables they can be defined.

Code:
#ifndef _TEST_OPTIONS_H
#define _TEST_OPTIONS_H

#include <string>

extern string FVAR;
extern string SVAR;

#endif
 
Old 04-20-2006, 04:05 PM   #3
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
That's how it works (making things very simple): When the compiler handles main.cpp, it gets test_options.h, finds the definitions and add them to the list of object. That's OK. Then it forgets what it knows and starts with test.cpp. It finds test_options.h and enters it (#ifdef protects from including the same file multiple times in one file, not different ones). It finds the definitions ... Then linking. The linker gets main.o and test.o. They both have the strings. You get an error.

How to fix it...Move the definitions to one file (.c).
In that file you'd have
Code:
string FVAR = "FirstVar";
string SVAR = "SecondVar";
Remove the definitions from .h. Now, in every .c (or .h) that needs it, use
Code:
extern string FVAR;
extern string SVAR;
 
Old 04-20-2006, 04:50 PM   #4
bendeco13
Member
 
Registered: Oct 2004
Distribution: Fedora 7
Posts: 232

Original Poster
Rep: Reputation: 30
THANKYOU so much,
I got it working now
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
linking problem: "multiple definition of .." qanopus Programming 11 05-03-2012 03:04 AM
Make Multiple Desktops In KDE like those in GNOME ada601 Linux - Software 2 08-17-2004 07:08 PM
how can I make Multiple VPN tunnles through fedora Bassam Linux - Networking 0 08-09-2004 09:24 PM
make with multiple targets eskimo22 Programming 1 02-26-2004 11:42 AM
multiple definition ? lackluster Linux - Software 1 11-27-2003 05:29 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:27 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration