使用混合C和C ++的函数的未定义引用

Zimano

undefined reference to 'mgmt_new_default()'尝试将一些C代码合并到C ++项目中时出现错误。

我试图在其构造函数中设置类的属性。这个课叫做SocketManager

套接字管理器

#ifndef SOCKETMANAGER_H
#define SOCKETMANAGER_H
#include "bluetooth/src/mgmt.h"
class SocketManager
{
public:
SocketManager();

struct mgmt *management;

//MGMT-related methods
void Initialize();
void StartDiscovery();
};

#endif // SOCKETMANAGER_H

socketmanager.c(错误代码导致错误)

#include "socketmanager.h"
#include "bluetooth/src/mgmt.h"

SocketManager::SocketManager()
{
    this->management = mgmt_new_default();
}

该函数mgmt_new_default(void)来自一个名为mgmt.c的文件,下面是标头和正文。

mgmt.h

#ifndef MGMT2_H
#define MGMT2_H

#include <stdbool.h>
#include <stdint.h>

#define MGMT_VERSION(v, r) (((v) << 16) + (r))

typedef void (*mgmt_destroy_func_t)(void *user_data);

struct mgmt;

struct mgmt *mgmt_new(int fd);
struct mgmt *mgmt_new_default(void);

struct mgmt *mgmt_ref(struct mgmt *mgmt);
void mgmt_unref(struct mgmt *mgmt);

typedef void (*mgmt_debug_func_t)(const char *str, void *user_data);

bool mgmt_set_debug(struct mgmt *mgmt, mgmt_debug_func_t callback,
            void *user_data, mgmt_destroy_func_t destroy);

bool mgmt_set_close_on_unref(struct mgmt *mgmt, bool do_close);

typedef void (*mgmt_request_func_t)(uint8_t status, uint16_t length,
                const void *param, void *user_data);

unsigned int mgmt_send(struct mgmt *mgmt, uint16_t opcode, uint16_t index,
                uint16_t length, const void *param,
                mgmt_request_func_t callback,
                void *user_data, mgmt_destroy_func_t destroy);
unsigned int mgmt_send_nowait(struct mgmt *mgmt, uint16_t opcode,     uint16_t index,
                uint16_t length, const void *param,
                mgmt_request_func_t callback,
                void *user_data, mgmt_destroy_func_t destroy);
unsigned int mgmt_reply(struct mgmt *mgmt, uint16_t opcode, uint16_t index,
                uint16_t length, const void *param,
                mgmt_request_func_t callback,
               void *user_data, mgmt_destroy_func_t destroy);
bool mgmt_cancel(struct mgmt *mgmt, unsigned int id);
bool mgmt_cancel_index(struct mgmt *mgmt, uint16_t index);
bool mgmt_cancel_all(struct mgmt *mgmt);

typedef void (*mgmt_notify_func_t)(uint16_t index, uint16_t length,
                const void *param, void *user_data);

unsigned int mgmt_register(struct mgmt *mgmt, uint16_t event, uint16_t index,
            mgmt_notify_func_t callback,
            void *user_data, mgmt_destroy_func_t destroy);
bool mgmt_unregister(struct mgmt *mgmt, unsigned int id);
bool mgmt_unregister_index(struct mgmt *mgmt, uint16_t index);
bool mgmt_unregister_all(struct mgmt *mgmt);

#endif // MGMT2_H

mgmt.c(仅与mgmt_new_default()冲突)

struct mgmt *mgmt_new_default(void)
{
struct mgmt *mgmt;
union {
    struct sockaddr common;
    struct sockaddr_hci hci;
} addr;
int fd;

fd = socket(PF_BLUETOOTH, SOCK_RAW | SOCK_CLOEXEC | SOCK_NONBLOCK,
                            BTPROTO_HCI);
if (fd < 0)
    return NULL;

memset(&addr, 0, sizeof(addr));
addr.hci.hci_family = AF_BLUETOOTH;
addr.hci.hci_dev = HCI_DEV_NONE;
addr.hci.hci_channel = HCI_CHANNEL_CONTROL;

if (bind(fd, &addr.common, sizeof(addr.hci)) < 0) {
    close(fd);
    return NULL;
}

mgmt = mgmt_new(fd);
if (!mgmt) {
    close(fd);
    return NULL;
}

mgmt->close_on_unref = true;

return mgmt;
}

我不知道该错误可能来自何处?我觉得我需要第二只眼睛。我认为我正确地实现了所有必要的标题保护,但这让我很困惑。

请注意,我没有直接使用BlueZ源代码,而是通过挑选一些我想要的文件并将它们“合并”到C ++项目中进行了实验。我合并了mgmt.cmgmt.h文件“按原样”,没有进行任何更改,但标题保护符除外。

谢尔盖

每当由C ++编译器编译时,您的头文件都必须对C文件中定义的函数使用extern“ C”。常见的习惯用法是在.h文件中使用

#ifdef __cplusplus
extern "C" {
#endif
// your functions declarations
#ifdef __cplusplus
}
#endif

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

混合 C 和 C++... 未定义的函数引用

来自分类Dev

未定义对函数的引用?C ++

来自分类Dev

C ++:对构造函数的未定义引用

来自分类Dev

未定义对函数c的引用

来自分类Dev

未定义的函数引用(C ++)

来自分类Dev

C ++:对void函数的未定义引用

来自分类Dev

对C ++中函数的未定义引用

来自分类Dev

对C ++中函数的未定义引用

来自分类Dev

使用未定义的引用运行函数

来自分类Dev

(.text + 0x20):对“ main”的未定义引用和对函数的未定义引用

来自分类Dev

(.text + 0x20):对“ main”的未定义引用和对函数的未定义引用

来自分类Dev

对构造函数和析构函数的未定义引用

来自分类Dev

对构造函数和析构函数的未定义引用

来自分类Dev

在C ++代码中对C函数的未定义引用

来自分类Dev

使用 Makefile 和 GTest 对函数的未定义引用

来自分类Dev

C ++类模板对函数的未定义引用

来自分类Dev

中间目标文件抛出的对函数c ++的未定义引用

来自分类Dev

C ++未定义对具有队列参数的函数的引用

来自分类Dev

c ++-对现有函数的未定义引用

来自分类Dev

在C程序中未定义对函数的引用

来自分类Dev

c ++对默认构造函数的未定义引用

来自分类Dev

在函数中未定义的引用 c++

来自分类Dev

C++ 对库函数的未定义引用

来自分类Dev

GetPhysicallyInstalledMemory 函数 - C 中未定义的引用

来自分类Dev

C ++未定义的引用

来自分类Dev

函数的隐式声明和未定义的引用

来自分类Dev

在rtems中使用powf函数(对powf的未定义引用)

来自分类Dev

无法编译x86_64 .s和.c文件:函数的未定义引用

来自分类Dev

C中的“未定义的函数引用”错误,具体取决于定义的位置

Related 相关文章

热门标签

归档