解决c ++链接错误的最简单方法是什么?

时间:2011-03-30 23:56:34

标签: c++ linker

当我尝试编译它时,我有多个c ++类文件和头文件我得到以下错误。我甚至尝试使用cmake让我的生活更轻松但我仍然得到它:

make: Warning: File `Makefile' has modification time 11 s in the future
make[1]: Warning: File `CMakeFiles/Makefile2' has modification time 11 s in the future
make[2]: Warning: File `src/CMakeFiles/simulation.dir/flags.make' has modification time 11 s in the future
Scanning dependencies of target simulation
make[2]: warning:  Clock skew detected.  Your build may be incomplete.
make[2]: Warning: File `src/CMakeFiles/simulation.dir/flags.make' has modification time 11 s in the future
[  4%] Building CXX object src/CMakeFiles/simulation.dir/OS.cxx.o
[  9%] Building CXX object src/CMakeFiles/simulation.dir/Ram.cxx.o
[ 14%] Building CXX object src/CMakeFiles/simulation.dir/RamQueues.cxx.o
[ 19%] Building CXX object src/CMakeFiles/simulation.dir/HardDisk.cxx.o
[ 23%] Building CXX object src/CMakeFiles/simulation.dir/CpuRegisters.cxx.o
[ 28%] Building CXX object src/CMakeFiles/simulation.dir/BankersAlgorithm.cxx.o
[ 33%] Building CXX object src/CMakeFiles/simulation.dir/Clock.cxx.o
[ 38%] Building CXX object src/CMakeFiles/simulation.dir/random.cxx.o
[ 42%] Building CXX object src/CMakeFiles/simulation.dir/PCB.cxx.o
[ 47%] Building CXX object src/CMakeFiles/simulation.dir/File.cxx.o
[ 52%] Building CXX object src/CMakeFiles/simulation.dir/Jobs.cxx.o
[ 57%] Building CXX object src/CMakeFiles/simulation.dir/Cpu.cxx.o
[ 61%] Building CXX object src/CMakeFiles/simulation.dir/JobCreationFactory.cxx.o
[ 66%] Building CXX object src/CMakeFiles/simulation.dir/StateQueue.cxx.o
[ 71%] Building CXX object src/CMakeFiles/simulation.dir/Keyboard.cxx.o
[ 76%] Building CXX object src/CMakeFiles/simulation.dir/Mouse.cxx.o
[ 80%] Building CXX object src/CMakeFiles/simulation.dir/Monitor.cxx.o
[ 85%] Building CXX object src/CMakeFiles/simulation.dir/ReadyQueue.cxx.o
[ 90%] Building CXX object src/CMakeFiles/simulation.dir/TerminatingQueue.cxx.o
[ 95%] Building CXX object src/CMakeFiles/simulation.dir/WaitingQueue.cxx.o
[100%] Building CXX object src/CMakeFiles/simulation.dir/osTester.cxx.o
Linking CXX executable simulation
CMakeFiles/simulation.dir/osTester.cxx.o: In function `main':
osTester.cxx:(.text+0xa3): undefined reference to `JobCreationFactory::createFiles(HardDisk)'
osTester.cxx:(.text+0xb9): undefined reference to `JobCreationFactory::createJobs(HardDisk)'
osTester.cxx:(.text+0xee): undefined reference to `HardDisk::setNumberOfJobs(int)'
osTester.cxx:(.text+0x2fe): undefined reference to `OS::setSchedulingAlgorithm(int)'
osTester.cxx:(.text+0x32c): undefined reference to `OS::osTakeOver(HardDisk, Ram, Cpu, Mouse, Monitor, Keyboard)'
collect2: ld returned 1 exit status
make[2]: *** [src/simulation] Error 1
make[1]: *** [src/CMakeFiles/simulation.dir/all] Error 2
make: *** [all] Error 2

我该如何修复此错误?

CODE如下:

#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.createFiles(HD);
    j.createJobs(HD);

    //set defaut number of jobs to be used in the simulation .. allow user to input number of jobs
    cout << "***For simulation we use multiple instances of Mouse Monitor and Keyboard, this is actually the number of process a DeviceQ can handle at a g***\n";
    cout << "Enter number of Jobs to be used in the simulation (eg 40000) follwed by the number of available resources say ( 6 8 9) followed by 's' then hit Carriage return key>>>\n";
    while (cin >> numberOfJobs >> mouseAvailable >> monitorAvailable >> keyboardAvailable && !simulate) {
        simulate = true;
        HD.setNumberOfJobs(numberOfJobs);
    }   
    numberOfResourcesAvailable.push_back(mouseAvailable);
    numberOfResourcesAvailable.push_back(monitorAvailable);
    numberOfResourcesAvailable.push_back(keyboardAvailable);
    //setAvailable Resources
    //myOS.setAvailable(numberOfResourcesAvailable);

    INT_ITER aBegin = numberOfResourcesAvailable.begin();
    INT_ITER aEnd = numberOfResourcesAvailable.end();
    cout << "Initial number of resources Available (Mouse Monitor Keyboard):";
    for (; aBegin != aEnd; ++aBegin) {
        cout << *aBegin << " ";
    }
    cout << endl;
    /*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
    cout << "SJF and RR are represented by:";
    for (; sBegin != sEnd; ++sBegin) {// for both algorithms
        cout << *sBegin <<" ";
        myOS.setSchedulingAlgorithm(*sBegin);
        myOS.osTakeOver(HD,myRam,myCpu,myMouse,myMonitor,myKeyboard);
    }
    cout << "respectively\n";
    cout << endl;
    //for now the smulation progress will be seen on the console

    return 0;

}

3 个答案:

答案 0 :(得分:3)

最简单的方法是查看您是否定义了此符号

  

未定义的引用   `JobCreationFactory :: createFiles(硬盘)'

JobCreationFactory.cxx.o翻译单元中。您很可能已在JobCreationFactory.hxx中声明了它,但未在相应的实现中对其进行定义。


基于你的other question,这个是近似完全重复的,我想我知道发生了什么。在JobCreationFactory.cxx中,您没有定义类方法,而是重新声明它们

class JobCreationFactory{
    int numOfJobs;
    int numOfFiles;
    int time;
    int size;
    LISTOFINT header;

    JobCreationFactory()
    :numOfJobs(0),time(0),size(0)
    {
    }

不要那样做,而是

JobCreationFactory::JobCreationFactory() : numOfJobs(0),time(0),size(0)
{

}

等等,适用于JobCreationFactory.h中声明的所有方法。如果这个概念让您感到困惑,您可能需要get a good book关于C ++。

答案 1 :(得分:1)

链接错误通常是由于:

  • 忘记定义符号/功能。有些人根本忘记在.cpp文件上执行。

  • 定义错误的符号。它应该是一回事,但它被定义为另一种,非常相似。

  • 没有链接到正确的库。如果您没有实现该功能,则意味着该符号在其他地方定义。

答案 2 :(得分:0)

尝试使用像Visual Studio或Code :: Block等IDE。这些工具可以帮助您解决这些编译和链接问题。