我正在尝试将数据值从一个.csv文件复制到另一个文件,但是在编译时,我一直收到 undefined reference 错误。我很困惑,因为我确保包括头文件。谁能提供任何输入?
client.cpp(主要):
#include "common.h"
#include "FIFOreqchannel.h" // this is where I included the header file
#include <iostream>
#include <string>
#include <stdio.h>
#include <sys/wait.h>
using namespace std;
int main(int argc, char *argv[]){
cout<<"test"<<endl;
int n = 100; // default number of requests per "patient"
int p = 15; // number of patients
srand(time_t(NULL));
struct timeval tv; // this is the starting struct
struct timeval ts;// this is the ending struct
gettimeofday(&tv, NULL); // starts the clock
FIFORequestChannel chan ("control", FIFORequestChannel::CLIENT_SIDE); // this is the creation of the server
//datamsg first = datamsg(1,59.996, 2); // This is the creation of the first datamsg. It gets the information for the first person, up to 59.996 seconds, and gets info from the second ecg.
//The next step is to create the file
string file = "x1.csv";
//gettimeofdayI(&start, null);
ofstream inputfile;
inputfile.open(file); // opens the data for the first person
double seconds = 0.0;
while (seconds<59.996){
datamsg ecg1 = datamsg(1,seconds,1); // gets the data from the first ECG
datamsg ecg2 = datamsg(1,seconds,2); // gets the data from the second ECG
//cout<<test<<endl;
char*size= new char [sizeof(ecg1)];
*(datamsg*)size=ecg1;
chan.cwrite(size,sizeof(ecg1));
char * read=chan.cread();
double ecgfirst=*(double*)read;
inputfile<<ecgfirst<<","<<"\n";
char*size2= new char [sizeof(ecg2)];
*(datamsg*)size2=ecg2;
chan.cwrite(size2,sizeof(ecg2));
char * read2=chan.cread();
double ecgsecond=*(double*)read2;
inputfile<<ecgsecond<<","<<"\n";
seconds = seconds +.004; // this is the incrementation of the seconds
}
inputfile.close();
gettimeofday(&ts, NULL);
double totaltime; // this will count the total time of the process
totaltime = ts.tv_sec-tv.tv_sec;
cout<<" The total time to get the info for person is "<<totaltime<<"."<<endl;
// sending a non-sense message, you need to change this
char x = 55;
chan.cwrite (&x, sizeof (x));
char* buf = chan.cread ();
// closing the channel
MESSAGE_TYPE m = QUIT_MSG;
chan.cwrite (&m, sizeof (MESSAGE_TYPE));
}
FIFOreqchannel.h:
#ifndef _FIFOreqchannel_H_
#define _FIFOreqchannel_H_
#include "common.h"
class FIFORequestChannel
{
public:
enum Side {SERVER_SIDE, CLIENT_SIDE};
enum Mode {READ_MODE, WRITE_MODE};
private:
/* The current implementation uses named pipes. */
string my_name;
Side my_side;
int wfd;
int rfd;
string pipe1, pipe2;
int open_pipe(string _pipe_name, int mode);
public:
FIFORequestChannel(const string _name, const Side _side);
/* Creates a "local copy" of the channel specified by the given name.
If the channel does not exist, the associated IPC mechanisms are
created. If the channel exists already, this object is associated with the channel.
The channel has two ends, which are conveniently called "SERVER_SIDE" and "CLIENT_SIDE".
If two processes connect through a channel, one has to connect on the server side
and the other on the client side. Otherwise the results are unpredictable.
NOTE: If the creation of the request channel fails (typically happens when too many
request channels are being created) and error message is displayed, and the program
unceremoniously exits.
NOTE: It is easy to open too many request channels in parallel. Most systems
limit the number of open files per process.
*/
~FIFORequestChannel();
/* Destructor of the local copy of the bus. By default, the Server Side deletes any IPC
mechanisms associated with the channel. */
char* cread(int *len=NULL);
/* Blocking read of data from the channel. Returns a string of characters
read from the channel. Returns NULL if read failed. */
int cwrite(void *msg, int msglen);
/* Write the data to the channel. The function returns the number of characters written
to the channel. */
string name();
};
#endif
FIFOreqchannel.cpp:
#include "common.h"
#include "FIFOreqchannel.h"
using namespace std;
/*--------------------------------------------------------------------------
*/
/* CONSTRUCTOR/DESTRUCTOR FOR CLASS R e q u e s t C h a n n e l */
/*--------------------------------------------------------------------------
*/
FIFORequestChannel::FIFORequestChannel(const string _name, const Side _side)
: my_name( _name), my_side(_side){
pipe1 = "fifo_" + my_name + "1";
pipe2 = "fifo_" + my_name + "2";
if (_side == SERVER_SIDE){
wfd = open_pipe(pipe1, O_WRONLY);
rfd = open_pipe(pipe2, O_RDONLY);
}
else{
rfd = open_pipe(pipe1, O_RDONLY);
wfd = open_pipe(pipe2, O_WRONLY);
}
}
FIFORequestChannel::~FIFORequestChannel(){
close(wfd);
close(rfd);
remove(pipe1.c_str());
remove(pipe2.c_str());
}
int FIFORequestChannel::open_pipe(string _pipe_name, int mode){
mkfifo (_pipe_name.c_str (), 0600);
int fd = open(_pipe_name.c_str(), mode);
if (fd < 0){
EXITONERROR(_pipe_name);
}
return fd;
}
char* FIFORequestChannel::cread(int *len){
char * buf = new char [MAX_MESSAGE];
int length = read(rfd, buf, MAX_MESSAGE);
if (length < 0){
EXITONERROR ("Connection Error");
}
if (len) // the caller wants to know the length
*len = length;
return buf;
}
int FIFORequestChannel::cwrite(void* msg, int len){
if (len > MAX_MESSAGE){
cerr << "message length exceeds buffer size" << endl;
exit (-1);
}
if (write(wfd, msg, len) < 0){
EXITONERROR("cwrite");
}
return len;
}
我得到的错误是未定义的引用错误。因此,我认为链接存在问题,但是我很困惑,因为我包含了头文件。
这是我遇到的错误:
client.cpp:(.text+0xc8): undefined reference to
`FIFORequestChannel::FIFORequestChannel(std::__cxx11::basic_string<char, std::char_traits<char>,
std::allocator<char> >, FIFORequestChannel::Side)'
client.cpp:(.text+0x22e): undefined reference to `FIFORequestChannel::cwrite(void*, int)'
client.cpp:(.text+0x242): undefined reference to `FIFORequestChannel::cread(int*)'
client.cpp:(.text+0x2f3): undefined reference to `FIFORequestChannel::cwrite(void*, int)'
client.cpp:(.text+0x307): undefined reference to `FIFORequestChannel::cread(int*)'
client.cpp:(.text+0x446): undefined reference to `FIFORequestChannel::cwrite(void*, int)'
client.cpp:(.text+0x45a): undefined reference to `FIFORequestChannel::cread(int*)'
client.cpp:(.text+0x489): undefined reference to `FIFORequestChannel::cwrite(void*, int)'
client.cpp:(.text+0x4b6): undefined reference to `FIFORequestChannel::~FIFORequestChannel()'
client.cpp:(.text+0x54d): undefined reference to `FIFORequestChannel::~FIFORequestChannel()'
collect2: error: ld returned 1 exit status
我在Amazon Webservices上使用Cloud 9 IDE,并且所有文件都在同一文件夹中。
最佳答案
使用#include "FIFOreqchannel.h"
是必需的,但不足以链接您的程序。您还必须提供FIFOreqchannel.o
到链接器。
您正在执行以下操作:
g++ client.cpp
你应该做:
g++ client.cpp FIFOreqchannel.cpp ...other_object_files... ...other_libraries...
关于c++ - 未定义对FIFORequestChannel::FIFORequestChannel的引用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58252189/