Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

identifier "DDRB" is undefined - VS code / Visual studio

I am getting the following error when using the identifier DDRB:

identifier "DDRB" is undefined

But, when I click “go to definition”, the IDE does shows that it can find them. The code also compiles without any problem. I was using VScode first and setting intellisense to "tag parser" did work, but it also got rid of the error checking. So, I switched over to Visual Studio, but the issue remains. In both cases I included the AVR library.

I have googled quite a bit and found some solutions, but most were outdated or did not work. What can I do to resolve this issue?

"minimal reproducible example:"

#include <avr\io.h>

int main() {

    DDRB |= (1 << DD3);

}
like image 831
Luuk Wuijster Avatar asked May 15 '26 02:05

Luuk Wuijster


2 Answers

I can reproduce same issue in VS2017, and this one can be resolved by adding the #define __AVR_ATmega32U4__ above the #include <avr\io.h> like this:

#define __AVR_ATmega32U4__ 
#include <iostream>
#include <avr/io.h>
int main()
{
    DDRB |= (1 << DD3);
}

After adding the macro definition, VS Intellisense option can recognize them well and the issue goes away. More details refer to Kissiel's reply. Thanks to him!

like image 86
LoLance Avatar answered May 16 '26 15:05

LoLance


If you don't want to paste this definition into almost every file:

  1. press f1
  2. find C/C++; Edit configurations (UI)
  3. paste your mcu name in Defines section e.g __AVR_ATmega32U4__

It worked for me in vs code.

like image 32
gicher Avatar answered May 16 '26 15:05

gicher