2011-03-30 44 views
0

當我嘗試編譯它們時,我有多個C++類文件和頭文件我得到以下錯誤。我甚至嘗試使用cmake使我的生活更輕鬆,但我仍然得到它:什麼是最簡單的方法來解決C++鏈接錯誤

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 

我該如何解決這個錯誤?

代碼如下:

#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; 

} 
+1

@user請格式化您的問題中的代碼。 – 2011-03-31 00:01:03

+0

[可能的重複](http://stackoverflow.com/q/5481409/283302)。 – 2011-03-31 00:44:15

回答

0

嘗試使用IDE如Visual Studio或代碼::塊,等那工具可以幫助你與這些編譯和鏈接問題。

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聲明的所有方法。如果這個概念讓你感到困惑,你可能需要關於C++的get a good book

+0

謝謝,我一直以爲我會這樣做,只要我在課堂外定義方法。 – user599319 2011-03-31 01:06:34

0

鏈接錯誤通常是由於:

  • 忘記定義符號/功能。有些人只是忘記在.cpp文件上執行該操作。

  • 定義錯誤的符號。它應該是一件事,但它被定義爲另一個,非常相似。

  • 不與正確的庫鏈接。如果這個函數沒有被你實現,這意味着符號被定義在其他地方。

相關問題