0

A program im writing successfully runs on the IDE visual studio code. However, when attempting to use the Bitvise SSH client to run my program I get a list of errors that I myself cannot understand the problem for. Bitvise it another way to access the CMD client from a remote server, for all intenstive purposes it acts the same as windows CMD. I will provide a screen cap of the errors and a full run down of the parts of my program that I believe are causing the errors. If any further code is required please feel free to ask.

errors screen cap

enter image description here

This error report shows a common error, with something being a placeholder for all instances.

multiple definition of `something' /tmp/ccBhjFYn.o:(.bss+0x0): first defined here

This error DOES NOT happen in visual studio code

From this error report it can be seen the issue is found within driver.cpp and my header.h file. For this reason i wont provide a minimal code for these files, but they are small enough to not require one.

MAIN

int main()
{
    Customer c;
    Part p;
    Builder b;
    const string fileName = "Parts.txt";

    auto partsVec =  readpartFile();
    auto customerVec = readcustomerFile();
    auto builderVec = readbuilderFile();

    fexists(fileName);
    complexity(c, partsVec);
    robotComplexity(partsVec,customerVec);
    writeFile(buildAttempt(b, complexity(c, partsVec), variability(customerVec, builderVec)));


  return 0;
}

HEADER FILE



#include <vector>
#include <string>


struct Customer {
std::string customerName;
std::string projectName;
std::string listofParts;
} myCustomer;

struct Part {
char partCode;
std::string partName;
int maximum;
int minimum;
int complexity;
} myPart;

struct Builder {
std::string builderName;
int ability;
int variability;
} myBuilder;


bool fexists(const std::string filename);

std::vector<Part> readpartFile();

std::vector<Customer> readcustomerFile();

std::vector<Builder> readbuilderFile();

float complexity(const Customer& c, const std::vector<Part>& parts);

void robotComplexity(const std::vector<Part>& vecB, const std::vector<Customer>& vecC);

double variability(const std::vector<Customer>& customerList, const std::vector<Builder>& builderList);

std::vector<double> buildAttempt(Builder b, double variaiblity, double complexityRobot);

void writeFile(std::vector<double> build);

Thankyou for any help. This question may be hard to understand and follow but i did try my best. Any sugguestions to help improve this question are welcome but please be friendly :)

Ramprasath Selvam
  • 3,868
  • 3
  • 25
  • 41
David Ling
  • 145
  • 1
  • 12
  • 1
    `but they are small enough to not require one.` LOL, such confidence, any bets that's where the error is? – john Apr 19 '20 at 08:33
  • @john As clearly stated in the question, if further code is required please ask but be friendly about it. – David Ling Apr 19 '20 at 08:35
  • 1
    You have variable declarations in your header file. If that header file is included in two or more places you will get the errors described. Maybe that's the difference between your VS compilation and SSH compilation – john Apr 19 '20 at 08:35
  • A [mcve] is always required when you ask about a compiler error, there is no need to let us ask for it – 463035818_is_not_an_ai Apr 19 '20 at 08:35
  • 1
    Why are you including `header.h` in the command line ? – John3136 Apr 19 '20 at 08:35
  • 1
    Put the output in the question as **text** not a picture. – Ted Lyngmo Apr 19 '20 at 08:36
  • @John3136 Once compiled all 3 files are placed in a new file called RAT – David Ling Apr 19 '20 at 08:37
  • 1
    @DavidLing One mistake is that you are compiling header.h, a second mistake is the variable declarations in the header file, a third mistake is the lack of include guards in the header file, any more mistakes? Hard to say, but fix those first and see what happens. – john Apr 19 '20 at 08:39
  • @John3136 it is my first project compiling multiple files this way so forgive the ignorance. Is header.h not reqquired in the compilation? – David Ling Apr 19 '20 at 08:39
  • @DavidLing Exactly, never explicitly compile header files, they get compiled by being included in other files. – john Apr 19 '20 at 08:40
  • @john ah okay, that makes sense. Thankyou for your help :) – David Ling Apr 19 '20 at 08:41
  • When the preprocessor finds `#include` it replaces the include statement with the contents of file. After all of the including is done, you have one large file that will be compiled. – user4581301 Apr 19 '20 at 08:42
  • Try this: `g++ -Wall -Wextra -Werror -pedantic -pedantic-errors driver.cpp implementation.cpp -o RAT` and fix the problems one by one. – Ted Lyngmo Apr 19 '20 at 08:45
  • @john okay i attempted to run without compiling the H file and i still get the same error. – David Ling Apr 19 '20 at 08:49
  • @DavidLing That's because your most serious error is that you have variable decalrations in your header file. If you include that header file in two different places, you will get multiple decalrations of those variables, which is exactly what you error message say. – john Apr 19 '20 at 08:50
  • @user4581301 where would my error lie then? is it do with what i am or arent including? – David Ling Apr 19 '20 at 08:50
  • @john knowing this, i only use #include in one other file in my program. – David Ling Apr 19 '20 at 08:52
  • 1
    @DavidLing Two files is enough. See my answer for the correct way to combine global variables with header files. – john Apr 19 '20 at 08:54
  • @john so you're saying dont use #include in any of my files? because as of now only one file includes such header – David Ling Apr 19 '20 at 08:54
  • 2
    @DavidLing No of course not, I've said it several times already, **the problem is that you have global variable definitions in your header file**. Don't do that! – john Apr 19 '20 at 08:55

1 Answers1

1

This in header.h

struct Customer {
std::string customerName;
std::string projectName;
std::string listofParts;
} myCustomer;

Is a definiton of a global variable myCustomer. As such it does not belong in a header file.

Change the header file to this

struct Customer {
std::string customerName;
std::string projectName;
std::string listofParts;
};

extern Customer myCustomer; // global variable declaration

Then to one of your cpp files (I suggest implementation.cpp) add this

Customer myCustomer; // global variable definition

Or you could just do away with global variables completely (the best solution).

NOTE in some of my comments above, I said you have global variable declarations in your header file. What I meant was you have global variable definitions in your header file. The difference between a definition and a declaration is what is crucial here. It's fine to put declarations in a header file, it's wrong to put a definition. Sorry for any confusion.

john
  • 85,011
  • 4
  • 57
  • 81
  • ah so you're sugguesting dont add std::string customerName; std::string projectName; std::string listofPartss to my struct declarations – David Ling Apr 19 '20 at 09:02
  • just leave it as struct Customer { }; – David Ling Apr 19 '20 at 09:02
  • @DavidLing How can you get that from this answer? – Ted Lyngmo Apr 19 '20 at 09:02
  • 1
    @DavidLing Good grief, no, where does the above say that? I've spelled out exactly what changes you shoudl make. – john Apr 19 '20 at 09:02
  • The problemis not the struct, that is fine. The problem is `myCustomer` and all the other global variables. – john Apr 19 '20 at 09:03
  • Maybe you don't even realise you have declared global variables? If that is the case then you can just delete them. – john Apr 19 '20 at 09:04
  • @john Perhaps you should add your earlier comments about removing `header.h` from the command line and adding a header guard to the answer too so they are not forgotten. – Ted Lyngmo Apr 19 '20 at 09:04
  • @DavidLing You have declared several global variables in your header file `myCustomer`, `myPart` etc. Now maybe you didn't mean to do that. I don't see anywhere in your code that you are using those global variables, but they are the cause of your errors. If in fact you don't need those global variables then you can just delete them. Just delete `myCustomer` etc from your header file instead of the changes I suggested above. – john Apr 19 '20 at 09:07
  • the use of extern doesnt work. Your sugguestion of removing global variables eems to be the better option – David Ling Apr 19 '20 at 09:11
  • However this is an issue, as theose global variables are needed for the program to run. Could i move these somewhere else? – David Ling Apr 19 '20 at 09:11
  • @DavidLing Moving them somewhere else is **exactly** what I described in the answer above. Sorry to be blunt, but what's the problem here? – john Apr 19 '20 at 09:13
  • sorry ive just been working on this all day and my brain is a bit mushy haha. For extern not working, it just gives me the same error as stated in the question. – David Ling Apr 19 '20 at 09:16
  • Well I don't know I've not looking over your shoulder wathcing what you are doing, here's a description on how to use extern https://stackoverflow.com/questions/1433204/how-do-i-use-extern-to-share-variables-between-source-files – john Apr 19 '20 at 09:17
  • @DavidLing All this could have been avoided if you had provided a [mcve] to start with. – Ted Lyngmo Apr 19 '20 at 09:20
  • in regards in moving them somewhere else. Orignally i had them in implementation but caused errors as the h file could not see them. Im aussming the extern method would again be used to solve this problem? – David Ling Apr 19 '20 at 09:21
  • @DavidLing Provide a [mcve]. We don't want to have to picture how your code looks in our heads. – Ted Lyngmo Apr 19 '20 at 09:23
  • @DavidLing Yes, putting them in implementation.cpp and using extern in the header is the correct solution. – john Apr 19 '20 at 09:39
  • @john okay i understand. Then include Customer myCustomer in my header file – David Ling Apr 19 '20 at 09:43
  • @john i have created a new question which i believe is far better worded and easier to under than this one https://stackoverflow.com/questions/61302779/c-how-to-parase-variables-to-a-header-file. This includes my attempt at your resolution and the outcome – David Ling Apr 19 '20 at 10:22