简体   繁体   中英

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);

}

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!

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

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

It worked for me in vs code.

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