C++ 错误:未定义对'BP::Device::Create(std::string const&;)和#x27;
我有一个文件.cpp,我正在从windows移植到linux。我已经更改了所有特定的头和函数,但仍然出现此错误。这是我的密码C++ 错误:未定义对'BP::Device::Create(std::string const&;)和#x27;,c++,linux,windows,C++,Linux,Windows,我有一个文件.cpp,我正在从windows移植到linux。我已经更改了所有特定的头和函数,但仍然出现此错误。这是我的密码 #define _CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES 1 #define _CRT_SECURE_NO_DEPRECATE 1 #include <stdio.h> #include <curses.h> #include <termios.h> #include <term.h>
#define _CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES 1
#define _CRT_SECURE_NO_DEPRECATE 1
#include <stdio.h>
#include <curses.h>
#include <termios.h>
#include <term.h>
#include <unistd.h>
#include "BioPlux.h"
#define BUF_SIZ 1000
static struct termios initial_settings, new_settings;
static int peek_character = -1;
int kbhit();
int kbhit()
{
char ch;
int nread;
if(peek_character != -1)
return 1;
new_settings.c_cc[VMIN]=0;
tcsetattr(0, TCSANOW, &new_settings);
nread = read(0,&ch,1);
new_settings.c_cc[VMIN]=1;
tcsetattr(0, TCSANOW, &new_settings);
if(nread == 1) {
peek_character = ch;
return 1;
}
return 0;
}
int main()
{
BP::Device::Frame frames[BUF_SIZ];
FILE *f = fopen("teste.txt", "w");
BP::Device *dev = NULL;
try
{
// decomment next block to search for bioPlux devices
/*
std::vector<std::string> devs;
BP::Device::FindDevices(devs);
fprintf(f, "FindDevices: %d\n", devs.size());
for(int i=0; i < devs.size(); i++)
fprintf(f, "%s\n", devs[i].c_str());
*/
dev = BP::Device::Create("00:07:80:40:DD:D8"); // put here the MAC address of your device
std::string str;
dev->GetDescription(str);
printf("Description: %s\n", str.c_str());
fprintf(f, "# Description: %s\n", str.c_str());
dev->SetDOut(true); // put the digital output at HIGH - useless
dev->BeginAcq(); // 1000 Hz, 12 bits, 6/8 channels
//dev->BeginAcq(1000, 0x01, 12); // 1000 Hz, 12 bits, channel 1 only
//for(int k = 0; k < 10; k++) // total 10 seconds
while(!kbhit())
{
dev->GetFrames(BUF_SIZ, frames); // block for 1 sec
putchar('*');
for(int i=0; i < BUF_SIZ; i++) // while the data to file
{
fprintf(f, "%i", frames[i].seq);
for(int j = 0; j < 8; j++)
fprintf(f, "\t%i", frames[i].an_in[j]);
fputc('\n', f);
}
}
dev->EndAcq();
} // end try
catch (BP::Err err)
{
char const *typ;
switch(err.GetType())
{
case BP::Err::TYP_ERROR:
typ = "Error";
break;
case BP::Err::TYP_NOTIFICATION:
typ = "Notification";
break;
}
printf("Error: %d Type: %s - %s\n", err.code, typ, err.GetDescription());
}
if (dev) delete dev;
fclose(f);
return 0;
}
您对
类BP::Device
的定义包含一个名为Create()
的方法的声明,但没有该方法的代码,因此必须在某个源文件中定义它。您的Windows版本是否包含源文件及其定义?因为你的链接显然不包括那个文件,如果它存在的话。如果您使用的是Visual Studio,则必须将该文件添加到项目中,或者添加包含该文件的库。在什么情况下会出现此错误?当链接或编译时?我看不到BP::Device::Create
的定义,您还试图调用标记为纯虚拟的函数,如dev->GetDescription(str)
@TimoGeusch:错误显示未定义引用
,这始终是链接器错误。我有点迷路,因为这是针对windows的,我从未使用过visual studio。我还有一个BioPlux.lib。这样编译时,我应该使用它吗g++-o bttest\u simple bttest\u simple.cpp-BioPlux.h
是的,正如我的回答所示,这可能是在该库中定义的。你需要将它添加到你的链接中。我有点迷路了,因为这是针对windows的,我从未使用过visual studio。我还有一个BioPlux.lib。这样编译时,我应该使用它吗g++-o bttest\u simple bttest\u simple.cpp-BioPlux.h
@SamuelNLP-好的,对不起,我误解了;我以为你要移植到Windows。阅读文章的第一行会有帮助。关于您的命令:如果您构建了一个库,您可以将它添加到g++命令行的一长串.cpp和库文件中,在linux上通常带有后缀.a
。如果这不是一个简单的程序,那么您需要一个makefile;你使用过这些工具吗?如果你不熟悉makefiles,你应该找到一个很好的教程。最终你会想要阅读,但一个简单的教程可能会让你开始。试着回答您的问题“我想在这样编译时使用它吗?g++-o bttest\u simple bttest\u simple.cpp-BioPlux.h
”不,您通常不会在命令行上列出头文件(BioPlux.h)。即使您这样做了,您使用的语法(-BioPlux.h
前面有一个破折号)表明BioPlux.h不是一个文件,而是一个您希望GCC理解的参数;按照惯例,这种语法表示它是程序的“开关”或“选项”,而不是文件名参数。但是.lib后缀不是linux的特征;你要把它移植到linux吗?
#include <vector>
#include <string>
#include <inttypes.h>
typedef unsigned char BYTE;
typedef unsigned short WORD;
#ifndef _BIOPLUXHEADER_
#define _BIOPLUXHEADER_
namespace BP
{
class Device
{
public:
struct Frame
{
BYTE seq;
bool dig_in;
WORD an_in[8];
};
static void FindDevices(std::vector<std::string> &devs);
static Device* Create(const std::string &port);
virtual void GetDescription(std::string &str)=0;
virtual void BeginAcq(void)=0;
virtual void BeginAcq(int freq, BYTE chmask, BYTE nbits)=0;
virtual void GetFrames(int nframes, Frame *frames)=0;
virtual void SetDOut(bool dout)=0;
virtual void EndAcq(void)=0;
virtual ~Device() {}
};
class Err
{
public:
enum Code {
// Notifications
//BT_AUTHENTICATION,
BT_ADDRESS = 1,
BT_ADAPTER_NOT_FOUND,
BT_DEVICE_NOT_FOUND,
CONTACTING_DEVICE,
PORT_COULD_NOT_BE_OPENED,
// Errors
PORT_INITIALIZATION,
//FIRMWARE_NOT_SUPPORTED,
DEVICE_NOT_IDLE,
DEVICE_NOT_IN_ACQUISITION_MODE,
PORT_COULD_NOT_BE_CLOSED,
BT_DEVICE_NOT_PAIRED,
INVALID_PARAMETER,
FUNCTION_NOT_SUPPORTED
} code;
enum Type {
TYP_ERROR,
TYP_NOTIFICATION
};
Err(Code c) : code(c) {}
Type GetType(void);
const char* GetDescription(void);
};
}
#endif
samuelpedro@ubuntu:~/Desktop/bioPlux API/C++$ g++ -o bttest_simple bttest_simple.cpp -lbluetooth BioPlux.lib
/tmp/cccJUI8I.o: In function `main':
bttest_simple.cpp:(.text+0xf8): undefined reference to `BP::Device::Create(std::string const&)'
bttest_simple.cpp:(.text+0x426): undefined reference to `BP::Err::GetType()'
bttest_simple.cpp:(.text+0x459): undefined reference to `BP::Err::GetDescription()'
collect2: error: ld returned 1 exit status