Using the MDF with C++

leenowell
Posts: 92
Joined: Tue Jan 15, 2019 1:50 pm

Re: Using the MDF with C++

Postby leenowell » Wed Feb 06, 2019 7:04 am

Hi

The get_atarted file I left as a .c file. All I did was rename the app-main function so I could move it to Lee.cpp to prove that the main could exist in a .CPP file. The class doesn't wrap the get_started funxtions I included it to prove that you can define classes there and therefore use c++.

In theory, if you can get this to compile, you can now do whatever c++ you want in the lee.cpp files even calling the functions from get-started e.g. call app-main1 from app-main1 in lee.cpp to run the example.

I built it on Ubuntu with compiler version 5.2.0 and crosstool 1.22.0-73 - it displays both when you run make from the command line.

Elijahg
Posts: 12
Joined: Sat Dec 08, 2018 3:30 am

Re: Using the MDF with C++

Postby Elijahg » Wed Feb 06, 2019 7:43 pm

Thanks again Lee for your help, I've renamed get_started.cpp back to get_started.c, and - this is getting ridiculous now - it compiles, but the linker can't find app_main... I guess the linker is ignoring the lee.o file.

Code: Select all

$ make
[snip]
Generating libwpa_supplicant.a.sections_info
Generating libxtensa-debug-module.a.sections_info
Generating esp32.common.ld
LD build/get-started.elf
/Users/elijah/esp/get-started/build/esp32/libesp32.a(cpu_start.o):(.literal.main_task+0x10): undefined reference to `app_main'
/Users/elijah/esp/get-started/build/esp32/libesp32.a(cpu_start.o): In function `main_task':
/Users/elijah/esp/esp-mdf/esp-idf/components/esp32/cpu_start.c:503: undefined reference to `app_main'
collect2: error: ld returned 1 exit status
Both lee.cpp and get_started.c are compiled.

Code: Select all

CC build/main/get_started.o
CXX build/main/lee.o
/Users/elijah/esp/get-started/main/lee.cpp: In constructor 'LeeTest::LeeTest()':
/Users/elijah/esp/get-started/main/lee.cpp:6:5: warning: unused variable 'i' [-Wunused-variable]
 int i =0;
     ^
AR build/main/libmain.a
I have no idea what's going on, I've written plenty of projects with Arduino libraries and have never had so much trouble!

I'm using crosstool 1.22.0-80, and compiler 5.2.0.

Code: Select all

Toolchain path: /Users/elijah/esp/xtensa-esp32-elf/bin/xtensa-esp32-elf-gcc
Toolchain version: crosstool-ng-1.22.0-80-g6c4433a
Compiler version: 5.2.0

leenowell
Posts: 92
Joined: Tue Jan 15, 2019 1:50 pm

Re: Using the MDF with C++

Postby leenowell » Wed Feb 06, 2019 7:57 pm

Do you have app_main1() defined in get_started.h and in get_started.c? You should then have app_main() defined in lee.cpp only

leenowell
Posts: 92
Joined: Tue Jan 15, 2019 1:50 pm

Re: Using the MDF with C++

Postby leenowell » Wed Feb 06, 2019 8:53 pm

A couple of other thoughts....
1. you did put the lee files and the get_started.h in the main directory with the get_started.c file?
2. you could try a "make clean" and then do a "make"

Elijahg
Posts: 12
Joined: Sat Dec 08, 2018 3:30 am

Re: Using the MDF with C++

Postby Elijahg » Wed Feb 06, 2019 10:36 pm

leenowell wrote:
Wed Feb 06, 2019 7:57 pm
Do you have app_main1() defined in get_started.h and in get_started.c? You should then have app_main() defined in lee.cpp only
Yup.
leenowell wrote:
Wed Feb 06, 2019 8:53 pm
A couple of other thoughts....
1. you did put the lee files and the get_started.h in the main directory with the get_started.c file?
2. you could try a "make clean" and then do a "make"
Yes, all in the same folder, and I've tried make clean too... I've attached the entire get-started folder, perhaps you could try compiling it your end?

Thanks again!
Attachments
get-started.zip
(65.68 KiB) Downloaded 710 times

Elijahg
Posts: 12
Joined: Sat Dec 08, 2018 3:30 am

Re: Using the MDF with C++

Postby Elijahg » Wed Feb 06, 2019 11:33 pm

I've posted a comment on the Github issue relating to this, as I still feel the examples should compile as C++ without having to include the functions through a class.

Elijahg
Posts: 12
Joined: Sat Dec 08, 2018 3:30 am

Re: Using the MDF with C++

Postby Elijahg » Thu Feb 07, 2019 3:51 am

I've managed to fix the example code so that it'll compile with C++!

The erroring lines (81-85 and 376-381) are actually structs of type sockaddr_in and mwifi_config_t. C supports designated initializers whereas C++ does not, and apparently even when compiled with 'extern "C"' around them, they're still not supported by the C++ compiler. Designated initializers are a C99 thing, presumably the C++ compiler doesn't support C99. The error resulting from this is:

Code: Select all

Sorry, unimplemented: non-trivial designated initializers not supported
Not sure how these are anything but trivial, but still.


Each of these is fixed by simply changing 81-85 from this...

Code: Select all

    
    struct sockaddr_in server_addr = {
        .sin_family = AF_INET,
        .sin_port = htons(port),
        .sin_addr.s_addr = inet_addr(ip),
    };
to this:

Code: Select all

struct sockaddr_in server_addr;

server_addr.sin_family = AF_INET,
server_addr.sin_port = htons(port),
server_addr.sin_addr.s_addr = inet_addr(ip),
and 376-381 from this...

Code: Select all

 
 mwifi_config_t config     = {
        .router_ssid     = CONFIG_ROUTER_SSID,
        .router_password = CONFIG_ROUTER_PASSWORD,
        .mesh_id         = CONFIG_MESH_ID,
        .mesh_password   = CONFIG_MESH_PASSWORD,
    };
to this:

Code: Select all

mwifi_config_t config;

strncpy(config.router_ssid, CONFIG_ROUTER_SSID, 32);
strncpy(config.router_password, CONFIG_ROUTER_PASSWORD, 64);
memcpy(config.mesh_id, CONFIG_MESH_ID, 8);
strncpy(config.mesh_password, CONFIG_MESH_PASSWORD, 64);
Also app_main needs to be wrapped within extern "C"...

Code: Select all

extern "C" {
	void app_main();
}
...and of course rename the [nnn]_example.c to [nnn]_example.cpp.


Hope that helps someone at some point! I'll update the Github issue with a pull request too.

Thanks again Lee for your help with this! :D

leenowell
Posts: 92
Joined: Tue Jan 15, 2019 1:50 pm

Re: Using the MDF with C++

Postby leenowell » Thu Feb 07, 2019 7:50 am

Seems odd thought that mine compiles fine without the changes. In terms of what I did, I didn't wrap app-main in a class just merely implemented it in the CPP file rather than the .c file in the example. I will take a look at the folder you sent over the weekend.

leenowell
Posts: 92
Joined: Tue Jan 15, 2019 1:50 pm

Re: Using the MDF with C++

Postby leenowell » Sun Feb 10, 2019 6:53 pm

Hi,

I have looked at this again and seems like I must have missed the error as I have the same as you now. So... have fixed it in the attached files. The issue was that the CPP compiler was compiling app_main and mangling the name. Thus when it came to linking it was looking for app_main but got the mangled version. Putting extern "C" wrappers around it fixes that. The other trouble was that app_main1 was referenced from a .c file and a .cpp file. When from a .cpp file it will mangle the name but from a .c file it will not. Hence.... the extern "C" wrappers and the #defines to change behaviour when needed.

You may need to do a "make clean" or delete the build directories.

Hope this works for you...

thanks

Lee.
Attachments
main.zip
(6.79 KiB) Downloaded 644 times

danylook
Posts: 7
Joined: Thu Aug 30, 2018 3:34 pm

Re: Using the MDF with C++

Postby danylook » Wed Jul 10, 2019 4:50 pm

hi
this issue still continue.
i use the mqtt_example in C++ and have many errors in mdf_mem.h

mostly a bad convertion form void to char or to int
this not compile for C++

mdebug_cmd.c:317:16: warning: assignment from incompatible pointer type [-Wincompatible-pointer-types]
packet = MDF_CALLOC(1, sizeof(mdebug_coredump_packet_t));
^
In file included from /home/cowscan/esp/esp-mdf/components/mcommon/include/mdf_common.h:59:0,
from /home/cowscan/esp/esp-mdf/MPU-driver/cowscan/main/cowscan.cpp:43:
/home/cowscan/esp/esp-mdf/MPU-driver/cowscan/main/cowscan.cpp: In function 'void node_read_task(void*)':
/home/cowscan/esp/esp-mdf/components/mcommon/include/mdf_mem.h:89:12: error: invalid conversion from 'void*' to 'char*' [-fpermissive]
ptr; \
^
/home/cowscan/esp/esp-mdf/MPU-driver/cowscan/main/cowscan.cpp:185:21: note: in expansion of macro 'MDF_MALLOC'
char *data = MDF_MALLOC(MWIFI_PAYLOAD_LEN);
^
CC build/mcommon/mdf_mem.o
/home/cowscan/esp/esp-mdf/MPU-driver/cowscan/main/cowscan.cpp: In function 'mdf_err_t event_loop_cb(mdf_event_loop_t, void*)':
/home/cowscan/esp/esp-mdf/components/mcommon/include/mdf_mem.h:89:12: error: invalid conversion from 'void*' to 'uint8_t* {aka unsigned char*}' [-fpermissive]
ptr; \
^
/home/cowscan/esp/esp-mdf/MPU-driver/cowscan/main/cowscan.cpp:349:41: note: in expansion of macro 'MDF_MALLOC'
node_list.change_list = MDF_MALLOC(node_list.change_num * sizeof(mesh_addr_t));



thanks in any update to use MDF in c++

Who is online

Users browsing this forum: No registered users and 38 guests