2023-08-30 buildroot编译 multiple definition of `xxxx‘; bluealsa.o:(.bss+0x0): first defined here

一、在编译buildroot的时候遇到下面的error

multiple definition of `xxxx'; bluealsa.o:(.bss+0x0): first defined here

二、C++ "multiple definition of .. first defined here" 在C++中,有时候需要在不同文件中定义同一个变量。对于这类变量如果处理不当,虽然已经加了const,也是很容易出现“multiple definition of... first defined here”的错误。这个跟gcc的编译器版本有关,有的版本可以编译的过,有的版本编译不通过。

三、测试代码,三个文件testa.c  testb.c  test.h

        3.1 test.h

#ifndef _TEST_H_
#define _TEST_H_

struct bluez_a2dp_codec {
       int id;
};

const struct bluez_a2dp_codec **bluez_a2dp_codecs;

#endif

        3.2 testa.c

#include <stdlib.h>
#include <stdio.h>
#include "test.h"

static const struct bluez_a2dp_codec a2dp_codec_source_ldac = {
        .id = 120,
};


static const struct bluez_a2dp_codec *a2dp_codecs[] = {
        &a2dp_codec_source_ldac,
};

const struct bluez_a2dp_codec **bluez_a2dp_codecs = a2dp_codecs;
extern void test();
int main(void)
{

     printf("hello world! id = %d\n",bluez_a2dp_codecs[0]->id);
     test();
    return 0;
}

        3.3 testb.c

#include <stdlib.h>
#include <stdio.h>
#include "test.h"

void test()
{
        printf("test! id = %d \n",bluez_a2dp_codecs[0]->id);

}

        3.4 在ubuntu上用gcc编译是可以通过

3.4 使用arm交叉编译gcc version 6.5.0 (Buildroot 2018.02-rc3-g3ae2d93f6)是可以编译通过

3.5 使用arm 交叉编译gcc version 10.3.0 (Buildroot 2018.02-rc3)编译报错。

四、解决方案,一个是换编译器,另外一个方法是用extern的形式定义变量。


$ cat fix_bluez-alsa.patch
diff --git a/external/bluez-alsa/src/bluealsa.c b/external/bluez-alsa/src/bluealsa.c
old mode 100644
new mode 100755
index 8335904ed..157d73b47
--- a/external/bluez-alsa/src/bluealsa.c
+++ b/external/bluez-alsa/src/bluealsa.c
@@ -19,7 +19,7 @@
 #include "hfp.h"
 #include "transport.h"

-
+extern struct bluez_a2dp_codec **bluez_a2dp_codecs;
 /* Initialize global configuration variable. */
 struct ba_config config = {

diff --git a/external/bluez-alsa/src/bluez-a2dp.c b/external/bluez-alsa/src/bluez-a2dp.c
old mode 100644
new mode 100755
index 5f81e6e51..697dee683
--- a/external/bluez-alsa/src/bluez-a2dp.c
+++ b/external/bluez-alsa/src/bluez-a2dp.c
@@ -339,4 +339,4 @@ static const struct bluez_a2dp_codec *a2dp_codecs[] = {
        NULL,
 };

-const struct bluez_a2dp_codec **bluez_a2dp_codecs = a2dp_codecs;
+struct bluez_a2dp_codec **bluez_a2dp_codecs = a2dp_codecs;
diff --git a/external/bluez-alsa/src/bluez-a2dp.h b/external/bluez-alsa/src/bluez-a2dp.h
old mode 100644
new mode 100755
index 67a7c5d51..e73e2a354
--- a/external/bluez-alsa/src/bluez-a2dp.h
+++ b/external/bluez-alsa/src/bluez-a2dp.h
@@ -55,6 +55,6 @@ struct bluez_a2dp_codec {
 };

 /* NULL-terminated list of available A2DP codecs */
-const struct bluez_a2dp_codec **bluez_a2dp_codecs;
+//const struct bluez_a2dp_codec **bluez_a2dp_codecs;

 #endif
diff --git a/external/bluez-alsa/src/bluez-iface.c b/external/bluez-alsa/src/bluez-iface.c
old mode 100644
new mode 100755
index c0a22f69f..3867d013d
--- a/external/bluez-alsa/src/bluez-iface.c
+++ b/external/bluez-alsa/src/bluez-iface.c
@@ -132,7 +132,7 @@ static const GDBusMethodInfo *bluez_iface_profile_methods[] = {
        NULL,
 };

-const GDBusInterfaceInfo bluez_iface_endpoint = {
+GDBusInterfaceInfo bluez_iface_endpoint = {
        -1, "org.bluez.MediaEndpoint1",
        (GDBusMethodInfo **)bluez_iface_endpoint_methods,
        NULL,
@@ -140,7 +140,7 @@ const GDBusInterfaceInfo bluez_iface_endpoint = {
        NULL,
 };

-const GDBusInterfaceInfo bluez_iface_profile = {
+GDBusInterfaceInfo bluez_iface_profile = {
        -1, "org.bluez.Profile1",
        (GDBusMethodInfo **)bluez_iface_profile_methods,
        NULL,
diff --git a/external/bluez-alsa/src/bluez-iface.h b/external/bluez-alsa/src/bluez-iface.h
old mode 100644
new mode 100755
index 7d6163840..0c199c630
--- a/external/bluez-alsa/src/bluez-iface.h
+++ b/external/bluez-alsa/src/bluez-iface.h
@@ -13,7 +13,7 @@

 #include <gio/gio.h>

-const GDBusInterfaceInfo bluez_iface_endpoint;
-const GDBusInterfaceInfo bluez_iface_profile;
+//const GDBusInterfaceInfo bluez_iface_endpoint;
+//const GDBusInterfaceInfo bluez_iface_profile;

 #endif
diff --git a/external/bluez-alsa/src/bluez.c b/external/bluez-alsa/src/bluez.c
old mode 100644
new mode 100755
index f69cbedeb..1d90aeb43
--- a/external/bluez-alsa/src/bluez.c
+++ b/external/bluez-alsa/src/bluez.c
@@ -26,7 +26,8 @@
 #include "utils.h"
 #include "shared/log.h"

-
+extern GDBusInterfaceInfo bluez_iface_endpoint;
+extern GDBusInterfaceInfo bluez_iface_profile;
 /**
  * Get D-Bus object reference count for given profile. */
 static int bluez_get_dbus_object_count(

五、参考文章

【C++】解决C++ “multiple definition of .. first defined here“问题_玛丽莲茼蒿的博客-CSDN博客

Ubuntu 22.04编译Android10源码报错,multiple definition of `yylloc‘; scripts/dtc/dtc-lexer.lex.o:(.bss+0x0):_JIANGFUGUI_的博客-CSDN博客

解决方法:Ubuntu 22.04编译Linux内核提示错误multiple definition of `yylloc' - 简书

usr/bin/ld: scripts/dtc/dtc-parser.tab.o:(.bss+0x10): multiple definition of ‘yylloc‘; scripts/dtc/d_/usr/bin/ld: scripts/dtc/dtc-parser.tab.o:(.bss+0x_北斗七星的柄的博客-CSDN博客

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值