Cpp code with errors, same c code without errors

Stipa88
Posts: 20
Joined: Mon May 09, 2022 12:26 pm

Cpp code with errors, same c code without errors

Postby Stipa88 » Fri Mar 22, 2024 12:14 pm

Hey.

The code inside the *.c file compiles without errors, the same code inside the *.cpp file has many errors, warnings..

The code is an official LEDC example:

#include <stdio.h>
#include "esp_system.h"
#include "esp_log.h"
#include "esp_err.h"
#include "driver/ledc.h"


#define LEDC_TIMER LEDC_TIMER_0
#define LEDC_MODE LEDC_LOW_SPEED_MODE
#define LEDC_OUTPUT_IO (5) // Define the output GPIO
#define LEDC_CHANNEL LEDC_CHANNEL_0
#define LEDC_DUTY_RES LEDC_TIMER_13_BIT // Set duty resolution to 13 bits
#define LEDC_DUTY (4096) // Set duty to 50%. (2 ** 13) * 50% = 4096
#define LEDC_FREQUENCY (4000) // Frequency in Hertz. Set frequency at 4 kHz


void LEDC_Init()
{
// Prepare and then apply the LEDC PWM timer configuration
ledc_timer_config_t ledc_timer = {
.speed_mode = LEDC_MODE,
.duty_resolution = LEDC_DUTY_RES,
.timer_num = LEDC_TIMER,
.freq_hz = LEDC_FREQUENCY, // Set output frequency at 4 kHz
.clk_cfg = LEDC_AUTO_CLK
};
ESP_ERROR_CHECK(ledc_timer_config(&ledc_timer));

// Prepare and then apply the LEDC PWM channel configuration
ledc_channel_config_t ledc_channel = {
.speed_mode = LEDC_MODE,
.channel = LEDC_CHANNEL,
.timer_sel = LEDC_TIMER,
.intr_type = LEDC_INTR_DISABLE,
.gpio_num = LEDC_OUTPUT_IO,
.duty = 0, // Set duty to 0%
.hpoint = 0
};

ESP_ERROR_CHECK(ledc_channel_config(&ledc_channel));
}

Pls help.

DrMickeyLauer
Posts: 168
Joined: Sun May 22, 2022 2:42 pm

Re: Cpp code with errors, same c code without errors

Postby DrMickeyLauer » Fri Mar 22, 2024 2:47 pm

This is completely normal and expected, as C++ has much stricter rules than C -- in particular with regards to struct initialization and type conversion.

chegewara
Posts: 2378
Joined: Wed Jun 14, 2017 9:00 pm

Re: Cpp code with errors, same c code without errors

Postby chegewara » Fri Mar 22, 2024 7:59 pm

Sadly espressif is very often ignoring this fact which is making hard to use esp-idf in C++ code.
Very often they are ignoring variables order in structures.

Next time please try to post also error logs, which can help to help you.

Who is online

Users browsing this forum: Baidu [Spider], Majestic-12 [Bot], Sang_Huynh and 125 guests