0

I have been looking through all of the different "Undefined symbol" posts, but I still can't get my particular situation to work. I originally received the undefined error when compiling my greater project, but even when I localize it just to the specific class, I am still getting the errors. I am pretty new to C++ to I feel there is some greater concept on how the compiler and linkers work in C++. I wasn't sure how much of my code to include, but let me know if what I included was excessive. So now for the code:

my header file

#ifndef FCFS_H
#define FCFS_H

#include <vector>
#include <fstream>
#include <iostream>

#include <Process.h>

using namespace std;

class FCFS{

public:
        /* Simple Constructor */

        FCFS(){
                totWaitTime = 0;
                totTurnAround = 0;
                timeElapsed = 0;
                vectorSize = 0;
        }

        /* Simple Accessors */
        inline int returnTotWT() const {return totWaitTime;}
        inline int returnTotTAT() const {return totTurnAround;}
        inline int returnTime() const {return timeElapsed;}
        inline int returnVecSize() const {return vectorSize;}

        /* Mutators */
        void buildMatrix(ifstream& myinfile);
        Process buildProcess(string line);
        vector<string>parser (const string& s, const string& w);

        bool runSchedule(string myinfile, string myoutfile);

private:
        int totWaitTime;
        int totTurnAround;
        int timeElapsed;
        int vectorSize;
        vector <Process> arrivedProc;


};

#endif // FCFS_H_INCLUDE

My .cpp file:

    #include <iostream>
    #include <fstream>
    #include <vector>
    #include <string>
    #include <iomanip>
    #include <iterator>
    #include <algorithm>
    #include <stdlib.h>
    #include <sstream>

    #include <Process.h>
    #include "FCFS.h"

    using namespace std;

    void FCFS::buildMatrix(ifstream& myinfile){
            string line;
            while(getline(myinfile, line)){
                    Process newProc = buildProcess(line);
                    arrivedProc.push_back(newProc);
            }

            sort(arrivedProc.begin(), arrivedProc.end(), Process::compareAT);
            vectorSize = arrivedProc.size();
    }

    vector<string> FCFS::parser (const string& s, const string& w){
            string temp{s};
            for (char& ch : temp){
                    for (char white : w){
                            if (ch == white) ch = ' ';
                    }
            }

            vector <string> substrings;
            stringstream ss;
            ss << temp;
            for (string buffer; ss >> buffer;){
                    substrings.push_back(buffer);
            }

            return substrings;
    }

    Process FCFS::buildProcess(string line){
            vector<string> procEl = parser(line, ",-");
            Process newProc(stoi(procEl[0]), stoi(procEl[1]), 

stoi(procEl[2]), stoi(procEl[3]), stoi(procEl[4]), stoi(procEl[5]));
                return newProc;
        }
    bool FCFS::runSchedule(string myin, string myout){
...just implempentation that I don't think is the problem...
    }

int main(){
        FCFS sched;
        string in = "Sample_Input1.txt";
        string out = "output.txt";

        sched.runSchedule(in, out);
}

my error:

    Ethans-MacBook-Pro:HW4 lozae$ g++ -g -Wall -std=c++11 -I. FCFS.cpp
Undefined symbols for architecture x86_64:
  "Process::setWT(double)", referenced from:
      FCFS::runSchedule(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >) in FCFS-53eb8f.o
  "Process::setTAT(double)", referenced from:
      FCFS::runSchedule(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >) in FCFS-53eb8f.o
  "Process::print(std::__1::basic_ostream<char, std::__1::char_traits<char> >&) const", referenced from:
      FCFS::runSchedule(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >) in FCFS-53eb8f.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Thanks for the help!!

el_loza
  • 47
  • 2
  • 8
  • What is your linker command line? – πάντα ῥεῖ Mar 12 '16 at 13:25
  • 2
    What is `Process`? If it is what you wrote, check for typo. If it is external library, link required things. – MikeCAT Mar 12 '16 at 13:25
  • 2
    "...just implempentation that I don't think is the problem..." And that's exactly where the problem is. Edit your post, and provide a [mcve]. What do you think the "referenced from: FCFS::runSchedule" is talking about? The compiler is telling you where the problem is, and somehow you decided that this is not where the problem is. Strange. – Sam Varshavchik Mar 12 '16 at 13:37

1 Answers1

1

Either there is a problem compiling the code for Process, which you did not include, or you didn't tell g++ where to find the library to link to.

An "Undefined Symbol" type error means that the linker cannot find the compiled code for a symbol. (In this case Process::setWT, Process::setTAT, and Process::print.) You need to tell g++ where to find the libraries to link to.

http://gcc.gnu.org/onlinedocs/gcc/Link-Options.html

A brief overview of how code compiles with g++ is: 1) g++ compiles the code into object files and/or libraries, 2) ld (the linker) links the object files and libraries into an executable.

This is simplified, but it is important to know that the linker needs to know what object files and libraries (compiled code, but not executables) to link together to create the executable file.

Kyle A
  • 928
  • 7
  • 17
  • This post may also prove helpful. [Undefined Symbols for architecture x86_64: Compiling problems](http://stackoverflow.com/questions/18751868/undefined-symbols-for-architecture-x86-64-compiling-problems) – Kyle A Mar 12 '16 at 13:54
  • Thank you guys so much! That link seemed to be very helpful and I was able to solve my localized issue along with my greater undefined issue problem. There was a typo in the line that linked all of the .cpp files together. – el_loza Mar 13 '16 at 00:00