C++ C++;链接错误

C++ C++;链接错误,c++,linker,C++,Linker,我有多个由hxx头文件和继承连接的类。这都是关于操作系统模拟的。 我分别编译了它们以查看是否有语法错误,但事实并非如此。相反,当我编译主驱动程序osTester.cxx时,我会遇到如下链接错误 #include <iostream> #include <ostream> #include <vector> #include<string> //#include <boost/shared_ptr.hpp> #include "

我有多个由hxx头文件和继承连接的类。这都是关于操作系统模拟的。 我分别编译了它们以查看是否有语法错误,但事实并非如此。相反,当我编译主驱动程序osTester.cxx时,我会遇到如下链接错误

#include <iostream>
#include <ostream>
#include <vector>
#include<string>
//#include <boost/shared_ptr.hpp>
#include "OS.hxx"
#include "Ram.hxx"
#include "Cpu.hxx"
#include "HardDisk.hxx"
#include "JobCreationFactory.hxx"
#include "Mouse.hxx"
#include "Monitor.hxx"
#include "Keyboard.hxx"

using namespace std;
typedef vector<int>LISTOFINT;
typedef LISTOFINT::iterator INT_ITER;

//namespace bo=boost;
//using bo::shared_ptr;
/* This is the main driver of the simulation,it makes all simulated devices available 
 and ready for use before operating system takes over(osTakeOver())
 */

int main(){
    
    int numberOfJobs = 0;
    bool simulate = false;
    string start;
    LISTOFINT schedulingAlgorithms; // 1 for SJF(Shortest Job first) and 2:for RR(Round Robbin)
    LISTOFINT numberOfResourcesAvailable;
    int mouseAvailable;
    int monitorAvailable;
    int keyboardAvailable;
    HardDisk HD;
    OS myOS;
    Cpu myCpu;
    Ram myRam;
    Mouse myMouse;
    Monitor myMonitor;
    Keyboard myKeyboard;
    JobCreationFactory j;
    //j.test();
    j.createFiles(HD);
    j.createJobs(HD);
    
    //set defaut number of jobs to be used in the simulation .. allow user to input number of jobs
    cout << "Enter number of Jobs to be used in the simulation (eg 40000) followed by space then  'S' for Start:";
    while (cin >> numberOfJobs >> start && !simulate) {
        simulate = true;
        HD.setNumberOfJobs(numberOfJobs);
    }   
    cout << "Enter the number of processes that a mouseQ can handle:(say 6)";
    cin >> mouseAvailable;
    cout << "Enter the number of processes that a monitoQ can handle:(say 7)";
    cin >> monitorAvailable;
    cout << "Enter the number of processes that a keyboardQ can handle:(say 8)";
    cin >> keyboardAvailable;
    numberOfResourcesAvailable.push_back(mouseAvailable);
    numberOfResourcesAvailable.push_back(monitorAvailable);
    numberOfResourcesAvailable.push_back(keyboardAvailable);
    //setAvailable Resources
    myOS.setAvailable(numberOfResourcesAvailable);
    
    /*simulate based on SJF then RR using the same number of resources then compare results
     *repeating the same algorithm a few more times would give a better comparison since this simulation
     *is entirely based on random number generation
    */
    schedulingAlgorithms.push_back(1);
    schedulingAlgorithms.push_back(2);
    INT_ITER sBegin = schedulingAlgorithms.begin();
    INT_ITER sEnd = schedulingAlgorithms.end();
    
    //at this point all devices are ready so an assumption can be made that 
    //BIOS has successfully checked devices hence BIOSdone = true
    //once everything is ready BIOS Loads OS
    
    for (; sBegin != sEnd; ++sBegin) {// for both algorithms
        myOS.setSchedulingAlgorithm(*sBegin);
        myOS.osTakeOver(HD,myRam,myCpu,myMouse,myMonitor,myKeyboard);
    }
    //for now the smulation progress will be seen on the console
    
    return 0;
    
}

单独编译是对语法错误的合理测试,但是单独链接没有意义,会遗漏一些东西


因此,请找到要编译而不链接的编译器选项(Microsoft Visual C++:
/C
,g++:
-C
)并使用它。

此错误告诉您,方法JobCreationFactory::createFiles没有定义,因此您可能忘记在任何文件中定义它,或者定义它的文件没有与osTester.cxx.o(调用它的文件)链接在一起.

您在相应的类中定义了所有提到的方法吗?我使用g++和make,通过编写CMakeLists.txt来告诉make在哪里可以找到文件。有人可以查看错误:谢谢。我在cxx中有JobCreationFactory::createFiles,在.hxx中有prototype。因此,您需要将从.cxx文件生成的.o文件添加到链接器命令中线
Linking CXX executable osTester
Undefined symbols:

  "JobCreationFactory::createFiles(HardDisk)", referenced from:
      _main in osTester.cxx.o
  "OS::setAvailable(std::vector<int, std::allocator<int> >)", referenced from:
      _main in osTester.cxx.o
  "OS::osTakeOver(HardDisk, Ram, Cpu, Mouse, Monitor, Keyboard)", referenced from:
      _main in osTester.cxx.o
  "HardDisk::setNumberOfJobs(int)", referenced from:
      _main in osTester.cxx.o
  "OS::setSchedulingAlgorithm(int)", referenced from:
      _main in osTester.cxx.o
  "JobCreationFactory::createJobs(HardDisk)", referenced from:
      _main in osTester.cxx.old: symbol(s) not found

collect2: ld returned 1 exit status
make[2]: *** [src/osTester] Error 1
make[1]: *** [src/CMakeFiles/osTester.dir/all] Error 2
make: *** [all] Error 2
#include<iostream>
#include<sstream>
#include<vector>
#include<algorithm>
#include "random.hxx"
#include "Jobs.hxx"
#include "HardDisk.hxx"
#include "File.hxx"

//using namespace std;
//typedef vector<int> LISTOFINT;
//LISTOFINT::iterator INT_ITER;

class JobCreationFactory{
    int numOfJobs;
    int numOfFiles;
    int time;
    int size;
    LISTOFINT header;
    
    JobCreationFactory()
    :numOfJobs(0),time(0),size(0)
    {
    }
    
     int getNumberOfJobs() 
    {
        return numOfJobs;
    }
    
     void createFiles(HardDisk hd)
    {   string file = "File";
        string intval;
        stringstream ss (stringstream::in | stringstream::out);
        RandomSeq r1(10000, 20000);
        numOfFiles = r1();
        RandomSeq r2(10, 100);
        for(int j = 0; j<=numOfFiles; j++ )
        {
            ss << j+1;
            ss >> intval;
            file.append(intval);
            hd.storeFiles(File(file,r2()));
        }
    }
    
     void createJobs(HardDisk hd)
    {
        string job = "Job";
        string intval;
        stringstream ss (stringstream::in | stringstream::out);
        RandomSeq r1(60000,70000);
        numOfJobs = r1();
        int instructionException = (int)(0.1 * numOfJobs);
        
        for (int i = 1; i < getNumberOfJobs()+ 1; i++) 
        {
            RandomSeq r1(0, 2);
            RandomSeq r2(0, numOfFiles);
            size = assignSize(); 
            time = assignTime(size);
            setHeader();
            ss << i;
            ss >> intval;
            job.append(intval);
            Jobs newJob(job,size, time, header,r1());
            if(i%instructionException == 0)
            {
                newJob.setInstructionException(true);
            }
            for(int k = 0; k<newJob.getNumberOfFiles();k++)
            {
                newJob.associateJobWithFiles(k, r2());
            }
            hd.storeJobs(newJob);
        }
    }
    
     int assignSize()
    {
        RandomSeq r1(500,5000);
        int size = r1();
        return size;
    }
    
     int assignTime(int size)
    {
        int time =0;
        RandomSeq r1(10,20);
        RandomSeq r2(20,30);
        RandomSeq r3(30,40);
        RandomSeq r4(40,50);
        RandomSeq r5(50,60);
        RandomSeq r6(60,70);
        RandomSeq r7(70,80);
        RandomSeq r8(80,90);
        RandomSeq r9(90,100);
        
        if(size>=500&&size < 1000)
        {
            time = r1();
        }
        else if(size>=1000&&size < 1500)
        {
            time = r2();
        }
        else if(size>=1500&&size < 2000)
        {
            time = r3();
        }
        else if(size>=2000&&size < 2500)
        {
            time = r4();
        }
        else if(size>=2500&&size < 3000)
        {
            time = r5();
        }
        else if(size>=3000&&size < 3500)
        {
            time = r6();
        }
        else if(size>=3500&&size < 4000)
        {
            time = r7();
        }
        else if(size>=4000&&size < 4500)
        {
            time = r8();
        }
        else
        {
            time = r9();
        }
        
        return time;
        
    }
    
     string assignIO()
    {
        RandomSeq r1(1,5);
        int num = r1();
        string need="";
        
        if(num == 1)
        {
            need = "keyboard";
        }
        else if(num == 2)
        {
            need = "mouse";
        }
        else if(num == 3)
        {
            need = "Both"; // keyboard and mouse
        }
        else if(num == 4)
        {
            need ="file";
        }
        else
        {
            need = "nothing";
        }
        return need;
    }
    
    
     void setHeader()
    {
        RandomSeq r1(0,4);
        RandomSeq r2(0,3);
        RandomSeq r3(0,2);
        INT_ITER hIter = header.begin();
        header.insert(hIter,r1());
        header.insert(hIter+1,r2());
        header.insert(hIter+2,r3());
    }
    
     int getNumberOfFiles() {
        return numOfFiles ;
    }
    void test(){
        cout << "Testing JobCreationFactory\n";
    }
        
};
int main(){
    return 0;
}
#ifndef JobCreationFactory_hxx__
#define JobCreationFactory_hxx__
#include "HardDisk.hxx"
class JobCreationFactory {
public:
    //JobCreationFactory(){
    //}
    int getNumberOfJobs();
    void createFiles(HardDisk hd);
    void createJobs(HardDisk hd);
    int assignSize();
    int assignTime(int size);
    string assignIO();
    void setHeader();
    int getNumberOfFiles();
    void test();
};

#endif // JobCreationFactory_hxx_