2012-12-12 27 views
0

我有一個file.cpp,我從windows移植到linux。我已經更改了所有特定的頭文件和函數,但是我仍然遇到了這個錯誤。這是我的代碼。錯誤未定義的引用`BP :: Device :: Create(std :: string const&)'

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

這是file.cpp,其中包括以上頭文件。這個類在這裏定義。

#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 

這是從編譯嘗試一個outpu:

[email protected]:~/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 
+0

你在哪一點得到這個錯誤?鏈接時或編譯時? –

+0

我沒有看到'BP :: Device :: Create'的定義,並且你也試圖調用像'dev-> GetDescription(str);'這樣標記爲純虛函數的函數。 –

+0

@TimoGeusch:錯誤表示「未定義的引用」,這始終是鏈接器錯誤。 –

回答

1

你的class BP::Device定義包含一個名爲Create()方法的聲明,但它沒有代碼,所以它有一些被定義源文件的某處。您的Windows版本是否包含具有其定義的源文件?因爲你的鏈接顯然不包含該文件,如果它存在。如果您使用的是Visual Studio,則必須將該文件添加到您的項目中,或者添加一個包含它的庫。

+0

我有點迷路,因爲這是爲窗戶,我從來沒有與視覺工作室合作過。我也有一個BioPlux.lib。我想在編譯時使用它嗎? 'g ++ -o bttest_simple bttest_simple.cpp -BioPlux.h' – SamuelNLP

+0

@SamuelNLP - 好吧,對不起,我誤解了;認爲你正在移植到Windows。它有助於實際閱讀您帖子的第一行。關於你的命令:如果你創建了一個庫,你可以在你的g ++命令行中加入一長串.cpp和庫文件,通常在linux上有一個後綴'.a'。如果這不僅僅是一個簡單的程序,你需要一個makefile;你和那些人一起工作過嗎? – phonetagger

+0

如果你不熟悉makefile,你應該找到一個很好的教程。最終你會想要閱讀http://www.gnu.org/software/make/manual/make.html,但更簡單的教程可能會讓你開始。嘗試http://www.cs.colby.edu/maxwell/courses/tutorials/maketutor/ – phonetagger

相關問題