简体   繁体   中英

How to use available libraries from within mbed-os?

I have pulled down a fresh copy of mbed-os by using the mbed-cli tool.

$ 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"

Ultimately I am working to enable uVisor on my NXP FRDM-K64F device, but for now I am only using the QuickStart tutorial to get a simple example working without enabling the uVisor.

So, as suggested in the link above, I make a source directory in the newly created clone of mbed-os:

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

I copy in the basic main.cpp and compile. It works. However, when I try and create a problem using some of the library routines -- specifically EthernetInterface .

Replacing my simple main.cpp from the uVisor example with the more complicated one (using EthernetInterface) from the above link:

#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) {}
}

Compiling with:

mbed compile -m K64F -t GCC_ARM

I am met with compilation errors stating that the EthernetInterface class does not have members that I am trying to invoke.

../../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);
     ^

When, of course, the EthernetInterface class does have such members. I think the problem is related to the mbed utilities not compiling against the correct source code because it seems to find the header. If I add a --source= option to the mbed compilation I am met with other errors regarding things that EthernetInterface.cpp includes.

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

The files are certainly contained with mbed-os, I am just not sure how to actually use them.

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

tl;dr -- How can we link against the library code given at libraries/ ? I can directly include the header files by directly including the file, but the corresponding source appears to be that located in the features/ directory rather than the one in libraries/ .

I'm wondering what you're doing that I'm missing, because this works for me:

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

Open ethernet-test/main.cpp and put in:

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

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

Then:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM