OpenHarmony开发者论坛

标题: C、C++混合编译错误 [打印本页]

作者: wxll    时间: 2024-4-18 13:24
标题: C、C++混合编译错误
[md]我尝试将GPIO的相关操作封装成GPIO类,之后创建一个LED类继承该GPIO类,具体代码如下:

```
class io{
  public:
   io() {};
   virtual void io_init();
   virtual void io_set_val(IotGpioValue val);
};

class led : public io
{
  public:
    led() {};
    void io_init(){
      IoTGpioInit(LED_TEST_GPIO);
      hi_io_set_func(HI_IO_NAME_GPIO_9, HI_IO_FUNC_GPIO_9_GPIO);
      IoTGpioSetDir(LED_TEST_GPIO, IOT_GPIO_DIR_OUT);
    }

    void io_set_val(IotGpioValue val){
      IoTGpioSetOutputVal(LED_TEST_GPIO, val);
    }
  
};
```

业务代码为控制LED灯闪烁,具体如下:

```
static void *LedTask(const char *arg)
{
    (void)arg;
    led a;
    a.io_init();
    int flag = 0;
    IotGpioValue val = IOT_GPIO_VALUE0;
    while (1) {
        if(flag == 0){
          val = IOT_GPIO_VALUE0;
          flag = 1;
        }
        else{
          val = IOT_GPIO_VALUE1;
          flag = 0;
        }
        a.io_set_val(val);
        osDelay(500);
    }

    return NULL;
}
```

我已经将引用的头文件如"iot\_gpio.h"使用extern "C"声明:

```
#ifdef __cplusplus
extern "C"{
#endif
#include "cmsis_os2.h"
#include "ohos_init.h"
#include "ohos_types.h"
#include "iot_gpio.h"
#include "hi_io.h"
#ifdef __cplusplus
}
#endif
```

BUILD.gn文件内容如下:

```
static_library("myapp") {
    sources = [
        "hello_world.cpp"
    ]
    include_dirs = [
        "//utils/native/lite/include",
        "//kernel/liteos_m/kal/cmsis",
        "//base/iot_hardware/peripheral/interfaces/kits",
        "//device/hisilicon/hispark_pegasus/sdk_liteos/include",
    ]
}
```

当我进行编译时出现了如下报错:

```
[OHOS ERROR] riscv32-unknown-elf-gcc -Iplatform/os/Huawei_LiteOS/kernel/include -Iconfig -DCONFIG_TEE_HUKS_SUPPORT -DCONFIG_CHIP_PKT_32K -DHI_BOARD_ASIC -DHI_ON_FLASH -DFLASH_FIRM_START=4248512 -E build/link/link.ld.S -o build/build_tmp/scripts/link.lds -P
[OHOS ERROR] riscv32-unknown-elf-ld -nostartfiles -nostdlib -static --gc-sections -L/home/ws/Desktop/OpenHarmony_v3.0_LTS/device/hisilicon/hispark_pegasus/sdk_liteos/build/scripts/../../../../../huawei/hms/hilink/ohos/l0/hi3861/lib/wifi -L/home/ws/Downloads/gcc_riscv32/bin/../lib/gcc/riscv32-unknown-elf/7.3.0 -Lbuild/build_tmp/libs/app/wifiiot_app -Lbuild/build_tmp/libs/components/at -Lbuild/build_tmp/libs/platform/drivers -Lbuild/build_tmp/libs/platform/system -Lbuild/build_tmp/scripts -Lbuild/libs -Lbuild/libs/hi3861/release/no_mesh -Lbuild/scripts -Lohos/libs -Tbuild/build_tmp/scripts/link.lds -Map=output/bin/Hi3861_wifiiot_app.map -o output/bin/Hi3861_wifiiot_app.out --start-group --no-whole-archive --whole-archive -ladc -lat -lauthmanager -lblackbox -lbootstrap -lbroadcast -lc_flash -lcfg -lcjson_static -lcmsis -lcpup -ldeviceauth -ldeviceauth_hal_liteos -ldiag -ldiscovery -ldrv -ldump_static -lflash -lgcc -lgpio -lhal_file_static -lhal_iothardware -lhal_sysparam -lhal_token_static -lhal_update_static -lhal_wifiaware -lhichainsdk -lhidumper_mini -lhievent_lite -lhilog_lite -lhiview_lite -lhota -lhuks_3.0_sdk -li2c -lio -liperf -llitekernel_flash -lltoswpa -llwip -lm_flash -lmbedtls -lmyapp -lnative_file -lparttab -lposix -lpwm -lres_cfg -lsamgr -lsamgr_adapter -lsamgr_source -lsdio -lsec_flash -lspiffs -lsysparam -lsystem -ltoken_static -ltrans_service -ltsensor -luart -lupg -lutils_kv_store -lwifi -lwifi_flash -lwifiaware -lwifiiot_app -lwifiservice -lwpa --end-group
[OHOS ERROR] riscv32-unknown-elf-ld: ohos/libs/libmyapp.a(libmyapp.hello_world.o).rodata._ZTI3led[_ZTI3led]+0x0): undefined reference to `vtable for __cxxabiv1::__si_class_type_info'
[OHOS ERROR] riscv32-unknown-elf-ld: ohos/libs/libmyapp.a(libmyapp.hello_world.o).rodata._ZTI3led[_ZTI3led]+0x8): undefined reference to `typeinfo for io'
```

OpenHarmony源码版本为 v3.0 LTS,请问该错误是否是由于链接命令中"-nostdlib"选项导致没有链接c++标准库而造成的,如果是,我该如何取消该选项?如果不是,请问该如何修复这个错误。
[/md]
作者: 论坛小助手    时间: 2024-5-15 09:24
使用-nostdlib选项会导致编译器不连接标准库,可以在gn中配置lgflags  -= [ "-nostdlib" ]来删除
作者: wxll    时间: 2024-5-26 22:39
回复 论坛小助手: 请问我该在哪里添加配置?直接在.gn文件中添加编译会报错




欢迎光临 OpenHarmony开发者论坛 (https://forums.openharmony.cn/) Powered by Discuz! X3.5