我有多个类通过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;
}
我尝试过使用CMake但仍然出现以下错误
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
请帮助!!!
我在cxx中有JobCreationFactory :: createFiles,在.hxx中有原型。
我真的不明白主要问题。请分别参考以下Jobcreationfactory.cxx和.hxx以及osTester.cxx(包括在之前的帖子中)。我尝试从不同的类调用一个方法说操作系统,我得到相同的链接错误。
#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;
}
//。hxx如下:
#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_
答案 0 :(得分:1)
单独编译是对语法错误的合理测试,但单独链接没有意义,事情将会丢失。
因此找到编译器选项进行编译而不进行链接(Microsoft Visual C ++:/c
,g ++:-c
)并使用它。
答案 1 :(得分:1)
此错误告诉您没有JobCreationFactory :: createFiles方法的定义 - 所以要么忘了在任何文件中定义它,要么定义它的文件没有与osTester.cxx.o链接(这是调用它的文件。)