I am not sure what I am doing wrong here: I have 3 .cpp files: main_program.cpp, read_input_data.cpp, write_output_data.cpp.main program calls both, readinputdata reads in a string, and writeoutputdata writes it out. Data is being read into the string garbage
which is defined in the namespace "INPUTDATA" present in INPUT.h, while the functions are declared in "FUNCTIONS.h".This is the error I get:
g++ -o f2d ../obj/main_program.o ../obj/read_input_data.o ../obj/write_output_data.o -Wall -Wextra -O3 -Werror -pedantic -Ofast/usr/bin/ld: ../obj/read_input_data.o:(.bss+0x0): multiple definition of `INPUTDATA::garbage[abi:cxx11]'; ../obj/main_program.o:(.bss+0x0): first defined here/usr/bin/ld: ../obj/write_output_data.o:(.bss+0x0): multiple definition of `INPUTDATA::garbage[abi:cxx11]'; ../obj/main_program.o:(.bss+0x0): first defined herecollect2: error: ld returned 1 exit statusmake: *** [Makefile:20: f2d] Error 1
This is what my files look like:main_program.cpp
// main_program.cpp// libraries to include#include <iostream>#include <fstream>#include <string> //Wheres Wallace????// header files#include "../headers/INPUT.h"#include "../headers/FUNCTIONS.h"using namespace INPUTDATA;using namespace std;int main(){ read_input_data(); write_output_data(); return 0;}
read_input_data.cpp
#include <iostream>#include <string> //Wheres Wallace????#include "../headers/INPUT.h"#include "../headers/FUNCTIONS.h"using namespace std;using namespace INPUTDATA;void read_input_data(){ cin>>garbage;}
write_output_data.cpp
#include <iostream>#include <string> //Wheres Wallace????#include "../headers/INPUT.h"#include "../headers/FUNCTIONS.h"using namespace std;using namespace INPUTDATA;void write_output_data(){ cout<<garbage<<endl;}
INPUT.h
#ifndef INPUT_H // include guard#define INPUT_H#pragma once#include <string> //Wheres Wallace????using namespace std;namespace INPUTDATA{ string garbage;}#endif
FUNCTIONS.h
#ifndef FUNCTIONS_H // include guard#define FUNCTIONS_H#pragma oncevoid read_input_data();void write_output_data();#endif
And this is my makefile, but I don't think its the makefile because I tested a separate code using it as well:
IDIR =../headersCXX=g++CFLAGS=-Wall -Wextra -O3 -Werror -pedantic -OfastHDIR=../headersDEPSNAME = INPUT.h FUNCTIONS.hDEPS = $(patsubst %,$(HDIR)/%,$(DEPSNAME))ODIR=../objOBJNAME = main_program.o read_input_data.o write_output_data.oOBJ = $(patsubst %,$(ODIR)/%,$(OBJNAME))$(ODIR)/%.o: %.cpp $(DEPS) $(CXX) -c -o $@ $< $(CFLAGS)all: f2df2d: $(OBJ) $(CXX) -o $@ $^ $(CFLAGS).PHONY: cleanclean: rm -f f2d $(ODIR)/*.o *~ core $(INCDIR)/*~
I really don't know what I am doing wrong here. If anyone can please help, I will be grateful. Thanks!
I am expecting that the error does not occur.