c++ - 如何使用 mbed-os 中的可用库?

标签 c++ gcc compilation arm mbed

我使用 mbed-cli 工具下载了 mbed-os 的新拷贝。

$ mbed new mbed-os-test
[mbed] Creating new program "mbed-os-test" (git)
[mbed] Adding library "mbed-os" from "https://github.com/ARMmbed/mbed-os" at latest revision in the current branch
[mbed] Updating reference "mbed-os" -> "https://github.com/ARMmbed/mbed-os/#dda7f7d77abd4330b05e686ce3bbe58230eb7876"

最终我正在努力在我的 NXP FRDM-K64F 设备上启用 uVisor,但现在我只使用 QuickStart教程,以在不启用 uVisor 的情况下获得一个简单的示例。

因此,按照上面链接中的建议,我在新创建的 mbed-os 克隆中创建了一个 source 目录:

$ mkdir mbed-os-test/mbed-os/source

我复制基本的 main.cpp 并编译。有用。但是,当我尝试使用某些库例程创建问题时——特别是 EthernetInterface .

将 uVisor 示例中的简单 main.cpp 替换为来自上述链接的更复杂的示例(使用 EthernetInterface):

#include "mbed.h"
#include "EthernetInterface.h"

int main() {
    EthernetInterface eth;
    eth.init(); //Use DHCP
    eth.connect();
    printf("IP Address is %s\n", eth.getIPAddress());

    TCPSocketConnection sock;
    sock.connect("mbed.org", 80);

    char http_cmd[] = "GET /media/uploads/mbed_official/hello.txt HTTP/1.0\n\n";
    sock.send_all(http_cmd, sizeof(http_cmd)-1);

    char buffer[300];
    int ret;
    while (true) {
        ret = sock.receive(buffer, sizeof(buffer)-1);
        if (ret <= 0)
            break;
        buffer[ret] = '\0';
        printf("Received %d chars from server:\n%s\n", ret, buffer);
    }

    sock.close();

    eth.disconnect();

    while(1) {}
}

编译:

mbed compile -m K64F -t GCC_ARM

我遇到编译错误,指出 EthernetInterface 类没有我尝试调用的成员。

../../mbed-os/source/main.cpp: In function 'int main()':
../../mbed-os/source/main.cpp:34:9: error: 'class EthernetInterface' has no member named 'init'
     eth.init(); //Use DHCP
         ^
../../mbed-os/source/main.cpp:36:38: error: 'class EthernetInterface' has no member named 'getIPAddress'
     printf("IP Address is %s\n", eth.getIPAddress());
                                      ^
../../mbed-os/source/main.cpp:38:5: error: 'TCPSocketConnection' was not declared in this scope
     TCPSocketConnection sock;
     ^
../../mbed-os/source/main.cpp:39:5: error: 'sock' was not declared in this scope
     sock.connect("mbed.org", 80);
     ^

当然,当 EthernetInterface 类确实有这样的成员时。我认为问题与 mbed 实用程序没有针对正确的源代码进行编译有关,因为它似乎找到了 header 。如果我向 mbed 编译添加 --source= 选项,我会遇到有关 EthernetInterface.cpp 包含的内容的其他错误。

mbed compile -m K64F -t GCC_ARM --source=../libraries/net/eth/EthernetInterface/

[ERROR] In file included from ../libraries/net/eth/EthernetInterface/EthernetInterface.cpp:19:0:
../libraries/net/eth/EthernetInterface/EthernetInterface.h:27:18: fatal error: rtos.h: No such file or directory

这些文件肯定包含在 mbed-os 中,我只是不确定如何实际使用它们。

$ find . -name EthernetInterface.cpp
./libraries/net/eth/EthernetInterface/EthernetInterface.cpp
./features/net/FEATURE_IPV4/lwip-interface/EthernetInterface.cpp

tl;dr -- 我们如何链接到 libraries/ 中给出的库代码?我可以通过直接包含文件来直接包含头文件,但是相应的源似乎位于 features/ 目录中,而不是 libraries/ 中的源。

最佳答案

我想知道你在做什么我错过了,因为这对我有用:

$ mbed new ethernet-test
$ cd ethernet-test
$ mbed target K64F
$ mbed toolchain GCC_ARM

打开ethernet-test/main.cpp并输入:

#include "mbed.h"
#include "EthernetInterface.h"

int main(int, char**) {
  EthernetInterface eth;
  eth.connect();
}

然后:

$ mbed compile
...
Total Flash memory (text + data + misc): 108092 bytes
Image: ./.build/K64F/GCC_ARM/ethernet-test.bin

关于c++ - 如何使用 mbed-os 中的可用库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39083899/

相关文章:

c++ - ioctlsocket 或 recv 在 windows 套接字编程中需要更多时间来执行?

gcc - 如何使用 GCC 获得更好的矢量化?

c++ - 如何隐藏来自外部库的编译警告

c# - 定义自己的关键字和含义

linux - 验证 bash 语法

c++ - 试图理解调车场算法

c++ - 使用现有的 2D 纹理和 glTextureView 初始化立方体贴图

c++ - C++ 中的迭代器循环

gcc - 使用 GCC 4.8 快照配置 GDC 的构建

c - 从静态编译函数返回时子例程线程 JIT x86 机器代码段错误