如何在链接时修复Makefile错误“无法识别的引用”?

时间:2013-03-08 00:56:56

标签: c++ g++ makefile

我在链接时收到以下内容:

BServer.cpp:(.text+0x58e5): undefined reference to `dbusServer::dbusServer(char*, int)'
BServer.cpp:(.text+0x58f4): undefined reference to `dbusServer::~dbusServer()'

运行make时的g ++输出是:

g++ dbusServer.o PCounter.o BServer.o -o PCounter -L./BAPI/lib -L./usr/local/lib/libdbus -lBAPIx64 -m64 -lstdc++ -lpthread -lboost_system -lboost_program_options -L./home/ben/Downloads/libdbus-3.0.3/src/ -ldbus -L/home/ben/workspace/IntegratedPCounter/src/PCounter

我在/home/ben/workspace/IntegratedPCounter/src/PCounter

中将'#include "dbusServer.h'作为BServer.cpp加入

然后我尝试在dbusServer中创建BServer的实例,如下所示:

dbusServer modserver("192.168.0.9",5432);

这是dbus的头文件:

// Define the number of devices to be managed
#include <dbus.h>
#define ADDRESS_START    0
#define ADDRESS_END      7

#ifndef dbusSERVER_H_
#define dbusSERVER_H_

class dbusServer {
dbus_t *ctx;
int nb;
typedef struct regHL
{
    uint16_t hibits;
    uint16_t lowbits;
};
private:
    void SplitCount(int); //To-do determine of the value needs to be split
    void addDevice(int);

public:
    dbusServer(char *, int);

    ~dbusServer();

    int WriteDeviceCounts(int, int);
    int AddDevice(int);
    int resetDeviceCounts(int);
 };
 #endif /* dbusSERVER_H_ *

这是dbus代码:

#include <dbus.h>;
#include <errno.h>
#include "dbusServer.h"
//Using boost program options to read command line and config file data
#include <boost/program_options.hpp>
#include <syslog.h>
using namespace std;

namespace po = boost::program_options;
//alias program option namespace


    int nb = 0;
    dbus_t *ctx;
    int rc;
    int dbusServer::dbusServer(char *IPAddress, int port) {
        nb = 0;
        // TODO Auto-generated constructor stub
        int socket;
        ctx = dbus_new_tcp(IPAddress, port);
        dbus_mapping_t *mb_mapping;
    mb_mapping = dbus_mapping_new(500, 500, 500, 500);
    if (mb_mapping == NULL) {
        fprintf(stderr, "Failed to allocate the mapping: %s\n",
                dbus_strerror(errno));
        dbus_free(ctx);
        return (-1);
    }

    socket = dbus_tcp_listen(ctx, 1);
    dbus_tcp_accept(ctx, &socket);

    return (0);
}

dbusServer::~dbusServer() {
    // TODO Auto-generated destructor stub
    dbus_mapping_free (mb_mapping);
    dbus_close(ctx);
    dbus_free(ctx);
}

int writeDeviceCounts(uint32_t counts, int device) {
    //split the 32 bit value from the counting device
    uint16_t highBits = (counts & 0xFFFFFFFF00000000) >> 16; // get the high bits
    uint16_t lowBits = (counts & 0xFFFFFFFF); //get the low bits

    return (-1);
}


int addDevice(int device_id) {
    //increment the device counter
    return(-1);

}

void removeDevice(int device_id) {

}



void resetDeviceCounts(int device_id) {

}

在第58页的“安全C ++”一书(Kushnir,V.,O'Reilly Press,2012)中,有一个坏类创建的例子,特别是在编写一个descructor方面。但是,纠正后的实现看起来会解决我的一些问题。

我想知道的一件事是如何避免在c ++中实现类的问题,所以这些事情都没有出现。必须有一份常用白话中的做和不做的清单,可以引导人们远离问题。

2 个答案:

答案 0 :(得分:2)

undefined reference to dbusServer::dbusServer(char*, int)
undefined reference to dbusServer::~dbusServer()'

这些错误意味着你告诉编译器这些函数存在......

class dbusServer {
    dbusServer(char *, int);
    ~dbusServer();
};

...编译器决定使用它们。

但你从未写过它们。它们未定义。

修改

在您的某个源文件中,您需要:

dbusServer::dbusServer(char *, int)
{
    // Your code
}

dbusServer::~dbusServer()
{
    // Your code
}

答案 1 :(得分:1)

您的dbusServer.cpp存在问题。删除class dbusServer {之类的行,它会使您的dbusServer(...)被引用为dbusServer::dbusServer::dbusServer(...)

从.cpp文件中的类定义中提取成员函数代码。

总之,一个在* .h中声明为

的类
class dbusServer{
    dbsusServer(char*, int);
    ...
};

它在* .cpp中的实现应该是

dbusServer::dbusServer(char*, int){
...
}

而不是:

class dbusServer{
     dbusServer::dbusServer(char*, int){
      ...
     }
};

另外,构造函数不对。

int dbusServer::dbusServer(char *IPAddress, int port) {

它应该是:

dbusServer::dbusServer(char *IPAddress, int port) {