0

gcc 6.4.0 Netbeans 8.2 cygwin64

I don't know how to fix a multiply defined symbol problem. My include file has guards and there are no other symbols with the same name. The linker shows that the symbol, testoutput, is multiply defined. Whatever name I use in the declaration 'ofstream ' comes back multiply defined. I can avoid the issue by creating a stringstream variable and passing to to a output function but that seems like a really poor idea. Can I pass an i/o variable around?

Test code and linker output included:

#ifndef COMMON_H
#define COMMON_H
# include <fstream>

   using namespace std;
   ofstream testout;

#endif  /* COMMON_H */


#ifndef FILE_H
#define FILE_H
# include "Common.h"
class myClass {
public:
   void outStuff();
};

#endif /* FILE_H */

# include "File.h"
# include "Common.h"

void myClass::outStuff() {
   testout << "another thing" << endl;
};

# include "Common.h"
using namespace std;
int main(int argc, char** argv) {
   testout.open("test.out");
   testout << "out" << endl;
}

======================== UUILD ============================== CLEAN SUCCESSFUL (total time: 255ms) cd 'C:\home\skidmarks\Projects\Test\Test' c:\cygwin64\bin\make.exe -f Makefile CONF=Debug "/usr/bin/make" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf make[1]: Entering directory '/c/home/skidmarks/Projects/Test/Test' "/usr/bin/make" -f nbproject/Makefile-Debug.mk dist/Debug/Cygwin64-Windows/test.exe make[2]: Entering directory '/c/home/skidmarks/Projects/Test/Test' mkdir -p build/Debug/Cygwin64-Windows rm -f "build/Debug/Cygwin64-Windows/File.o.d" g++ -Wall -Wunused-variable -c -g -MMD -MP -MF "build/Debug/Cygwin64-Windows/File.o.d" -o build/Debug/Cygwin64-Windows/File.o File.cpp mkdir -p build/Debug/Cygwin64-Windows rm -f "build/Debug/Cygwin64-Windows/main.o.d" g++ -Wall -Wunused-variable -c -g -MMD -MP -MF "build/Debug/Cygwin64-Windows/main.o.d" -o build/Debug/Cygwin64-Windows/main.o main.cpp mkdir -p dist/Debug/Cygwin64-Windows g++ -o dist/Debug/Cygwin64-Windows/test build/Debug/Cygwin64-Windows/File.o build/Debug/Cygwin64-Windows/main.o ../../../../../cygwin64/usr/local/lib/libgslip.a build/Debug/Cygwin64-Windows/main.o:/c/home/skidmarks/Projects/Test/Test/Common.h:8: multiple definition of `testout' build/Debug/Cygwin64-Windows/File.o:/c/home/skidmarks/Projects/Test/Test/Common.h:8: first defined here collect2: error: ld returned 1 exit status

lostbits
  • 928
  • 1
  • 9
  • 23
  • Every translation unit where you include `common.h` gets an `ofstream testout;` declaration. That's multiple definitions of the same object with external linkage. – StoryTeller - Unslander Monica Oct 12 '17 at 21:11
  • My 'real' question was how to use a ofstream object across multiple translation units. Your redirect allowed me to rethink the issue (thanks) and to insert "extern ofstream testout" in Common.h and "ofstream testout" in main.cpp which solved my problem. I think that the title should be changed and the 'duplicate' answer referenced for background. But I think that this is a different question. Boy, do I feel dumb! – lostbits Oct 12 '17 at 21:43
  • You can just ask a followup if the question isn't too broad. The immediate issue is fixed however. I think the duplicate fits. – StoryTeller - Unslander Monica Oct 12 '17 at 21:46

0 Answers0