Page 1 of 1

Cpp code with errors, same c code without errors

Posted: Fri Mar 22, 2024 12:14 pm
by Stipa88
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.

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

Posted: Fri Mar 22, 2024 2:47 pm
by DrMickeyLauer
This is completely normal and expected, as C++ has much stricter rules than C -- in particular with regards to struct initialization and type conversion.

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

Posted: Fri Mar 22, 2024 7:59 pm
by chegewara
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.