0

I've been working on an existing project and added functionality to it.

I've added a new class and header, but when it comes to building I'm getting surprising errors.

I can't post the exact code because its proprietary but the below describes the issue.

newfile.h:

#include <vector>
#include <string>
#include <boost/foreach.hpp>
#include <iostream>
#include "siblingFile1.h"
#include "siblingFile2.h"
#include "siblingFile3.h"
#include "siblingFile4.h"

using namespace json_spirit;
using namespace std;
using namespace boost;

int usefulVariable1 = 60;
vector<string> usefulVariable2 = {"abc"};
string usefulVariable3 = "abc";
int64 usefulVariable4 = 0;

Object method1(int64 nValue);
CustomType1 method2(int64 nValue);
bool method3(CustomType2 var2, int64 xValue);
bool method4(CustomType1 var1, int64 xValue);
bool method5(CustomType1 var1, long xValue);

with newfile.cpp:

#include <string>
#include <boost/foreach.hpp>
#include <iostream>
#include "siblingFile1.h"
#include "siblingFile2.h"
#include "siblingFile3.h"
#include "siblingFile4.h"
#include "newfile.h"

using namespace json_spirit;
using namespace std;
using namespace boost;

Object method1(int64 nValue) {
        .... does some stuff
}

CustomType1 method2(int64 nValue) {
        .... does some stuff
}

bool method3(CustomType2 var2, int64 xValue)
{
        .... does some stuff
}

bool method5(CustomType1 var1, long xValue)
{
    int64 x = (int64)xValue;
    return method4(x, x);
}

bool method4(CustomType1 var1, int64 xValue)
{
         .... does some stuff
}

I then use a few of these methods throughout the code, in a number of different classes.

I added method5 as a result of the compile errors I was getting to overload and guide to the correct method.

The error are below:

obj/oFile1.o:/path/to/source/src/newfile.h:17: multiple definition of `usefulVariable4'
obj/main.o:/path/to/source/src/newfile.h:17: first defined here
obj/oFile1.o:(.bss+0x40): multiple definition of `usefulVariable2[abi:cxx11]'
obj/main.o:(.bss+0x500): first defined here
obj/oFile1.o:(.bss+0x20): multiple definition of `NothingIChanged[abi:cxx11]'
obj/main.o:(.bss+0x4e0): first defined here
obj/oFile1.o:/path/to/source/src/newfile.h:14: multiple definition of `usefulVariable1'
obj/main.o:/path/to/source/src/newfile.h:14: first defined here
obj/oFile2.o:/path/to/source/src/newfile.h:17: multiple definition of `usefulVariable4'
obj/main.o:/path/to/source/src/newfile.h:17: first defined here
obj/oFile2.o:(.bss+0x40): multiple definition of `usefulVariable2[abi:cxx11]'
obj/main.o:(.bss+0x500): first defined here
obj/oFile2.o:(.bss+0x20): multiple definition of `NothingIChanged[abi:cxx11]'
obj/main.o:(.bss+0x4e0): first defined here
obj/oFile2.o:/path/to/source/src/newfile.h:14: multiple definition of `usefulVariable1'
obj/main.o:/path/to/source/src/newfile.h:14: first defined here
obj/main.o: In function `FunctionImplimentingNewMethod1(... params)':
/path/to/source/src/main.cpp:2289: undefined reference to `method5(CustomType1, long long)'
/path/to/source/src/main.cpp:2301: undefined reference to `method3(CustomType2, long long)'
obj/main.o: In function `FunctionImplimentingNewMethod2(... params)':
/path/to/source/src/main.cpp:575: undefined reference to `method5(CustomType1, long long)'
obj/main.o: In function `FunctionImplimentingNewMethod3(... params)':
/path/to/source/src/main.cpp:643: undefined reference to `method5(CustomType1, long long)'
obj/main.o: In function `aClass1::FunctionImplimentingNewMethod4(... params)':
/path/to/source/src/main.cpp:1547: undefined reference to `method5(CustomType1, long long)'
obj/main.o: In function `aClass1::FunctionImplimentingNewMethod5(... params)':
/path/to/source/src/main.cpp:1575: undefined reference to `method5(CustomType1, long long)'
obj/main.o: In function `FunctionImplimentingNewMethod6(... params)':
/path/to/source/src/main.cpp:1608: undefined reference to `method5(CustomType1, long long)'
obj/main.o:/path/to/source/src/main.cpp:817: more undefined references to `method5(CustomType1, long long)' follow
obj/main.o: In function `FunctionImplimentingNewMethod7(... params)':
/path/to/source/src/main.cpp:2367: undefined reference to `method3(CustomType2, long long)'
obj/oFile2.o: In function `aClass2::FunctionImplimentingNewMethod8()':
/path/to/source/src/oFile2.cpp:861: undefined reference to `method5(CustomType1, long long)'
obj/oFile1.o: In function `FunctionImplimentingNewMethod9(... params)':
/path/to/source/src/oFile1.cpp:298: undefined reference to `method1[abi:cxx11](long long)'
/path/to/source/src/oFile1.cpp:307: undefined reference to `method5(CustomType1, long long)'
collect2: error: ld returned 1 exit status

As can be seen I added method5 and usefulVariable4 to try and overcome the issue. Passing usefulVariable4 as a parameter to force it to use type int64; but it's not having it.

Do I need to update the makefile? I haven't updated any of the CallingFile's headers as the code is always used inside the cpp file method bodies, not types needed in the CallingFile's headers.

CallingFile.cpp:

#include "newfile.h"

... rest of file ...

SomeMethodWhereINeedToAddMyFunctionallity()
{
    ... existing code ...

    method4(localVar, 0) // fails to compile
        or 
    method4(localVar, userfulVariable4) // fails to compile
        or 
    method5(localVar, 0) // fails to compile
}

make version: GNU Make 4.1 Built for x86_64-pc-linux-gnu

What is causing this?

Thank you.

LeosSire
  • 93
  • 2
  • 10
  • Unrelated to your current problem but you should add include guards or `#pragma once` to your header files. You shouldn't use `using namespace` in header files – Alan Birtles May 24 '22 at 11:52

0 Answers0