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

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:
press f1
find C/C++; Edit configurations (UI)
paste your mcu name in Defines section e.g __AVR_ATmega32U4__
It worked for me in vs code.

Related

Including <Windows.h> causes (unknown attribute"no_init_all") error

Title says it all, started a new project in VS2017, included <iostream>, then when I went to include <Windows.h> (This is my first attempt at working with this header by the way), I got the error saying: unknown attribute"no_init_all"
Any idea what might be causing this?
OK, putting those comments into an answer ...
This bug is fixed in VS 2019, but as per this answer, in VS 2017 you can use:
#define no_init_all deprecated
or even just:
#define no_init_all

cin and cout operator error C++

I am doing an assignment for school and at school we use codeblocks for our IDE, but I wanted to use visual studio at home. The problem is when I run my program on visual studio I continue to get an operator error on my cin and cout but only before a string. I tried #include and while the error lines went away when I build I get the same error before. I am new to C++ and rather confused any help is appreciated.
picture of code below
The #include <string> you tried would be correct.
Your main problem is the #include "stdafx.h". It's a Precompiled header. Visual studio puts it in by default although it isn't required. It has to be the first include if you use it:
Visual C++ will not compile anything before the #include "stdafx.h" in the source file, unless the compile option /Yu'stdafx.h' is unchecked (by default); it assumes all code in the source up to and including that line is already compiled.
Just put it as the first #include (or get rid of it completely) to fix your issue. Then the rest should start working normally.

HYPRE blas build error with VS 2015

I installed VS 2015 Professional. I installed the latest HYPRE, from the Lawrence Livermore website. I then configured it using CMake and proceeded to build, and I started getting BLAS (dnrm2.c) build errors:
2> dnrm2.c
2> 1>
C:\Program Files (x86)\Windows Kits\10\Include\10.0.10240.0\ucrt\math.h(454): error C2059: syntax error: '('
The line of code triggering the error in dnrm2.c is:
#include "math.h"
which points to the file:
c:\Program Files (x86)\Windows Kits\10\Include\10.0.10240.0\ucrt\math.h
I looked up this error and found some suggestions such as this to change the include to:
#include <cmath>
and to edit the HYPRE project settings in: Configuration > C/C++ > Advanced > Compile As to Compile As C++ (/TP)
which I did, but I still see same error, since apparently the same header path to math.h is included from cmath as well:
#else /* _STD_USING */
#include <math.h>
#endif /* _STD_USING */
I've even tried re-installing VS 2015 without any luck (same errors). Appreciate any ideas on what's going on here, and how to resolve this. I guess I could try a minimalist example in VS 2015 that includes the math.h and report back, if that helps.
EDIT
My minimalist example:
#include "math.h"
int main() {
double d1 = sqrt(4.0);
float d2 = abs(4.0);
return 0;
}
appears to be building OK. I tried to set the project the same way to Compile as C (or C++, didn't matter). This doesn't really help me though.
OK, the problem here is with HYPRE source it looks like. They have this in a file f2c.h included before including the math.h:
//#undef abs
//#define abs(x) ((x) >= 0 ? (x) : -(x))
//#endif
When I commented it out (since this is already defined in the standard), then it gets past that build error. Of course I run into other build errors. I'm trying to tackle those separately.
EDIT: It's not as simple as that because they (HYPRE) actually rely on their own definition of abs. So I had undo the above and change the order of some includes so that the undef actually made sense. Either way, this is a HYPRE source code problem.
If you succeed in compiling the HYPRE on VS2015, Could you send your VS2015 program to me!
My major is Geophysics modeling and inversion.
MY email is schoolhui#hotmail.com
Thank you very much!
I've just commented
_Check_return_ int __cdecl abs(_In_ int _X);
in
c:\Program Files (x86)\Windows Kits\10\Include\10.0.10240.0\ucrt\math.h
and then HYPRE was successfully compiled!
Then, I've uncommented "abs".

Visual Studio 2015 + libtcod - Unexpected exit

I am having an odd issue getting libtcod to work with Visual Studio 2015.
I have followed all the steps found on this blog post to get everything linked.
The problem is that during debugging or running it inside Visual Studio the application will always close at initRoot with exit code 1, no other information or errors.
Running the produced EXE outside of Visual Studio has no issues whatsoever - starts up and works as expected!
The only information I could find related to this was a forum post that turned ugly in 2012
I did make a modification to the blog post's code to include a setCustomFont as well, which did not change anything.
Does anyone have any ideas on what might be going on?
Code:
#include <cstdio>
#include "libtcod.hpp"
// Hide the console window
#pragma comment(linker, "/SUBSYSTEM:windows /ENTRY:mainCRTStartup")
int main()
{
TCODConsole::setCustomFont("terminal12x12_gs_ro.png", TCOD_FONT_LAYOUT_ASCII_INROW | TCOD_FONT_TYPE_GRAYSCALE);
TCODConsole::initRoot(80, 50, "C++ libtcod tutorial");
TCODConsole::root->printEx(40, 25, TCOD_BKGND_NONE, TCOD_CENTER, "Hello world");
TCODConsole::flush();
TCODConsole::waitForKeypress(true);
return 0;
}
For anyone that stumbles upon this later, the above blog post is correct for setting up the vast majority of the application - if you run into the same issue I did (crashing / exiting in initRoot) make sure you have the terminal png in the source directory, this is what fixed it for me.

Why might the “fatal error C1001” error occur intermittently when using openmp?

My code works well without #openmp
but I got this error when I added #openmp compiler
1>c:\users\hdd amd ali\documents\v studio 10 projects\visual studio 2010\projects\escaledesvols2 - copy\escaledesvols2\djikstra.cpp(116): fatal error C1001: An internal error occurred in the compiler.
1> (compiler file 'f:\dd\vctools\compiler\utc\src\p2\wvm\mdmiscw.c', ligne 1098)
note:
i use many different libraries (like #boost)
#include <string>
#include <iostream>
#include <stdio.h>
#include <ctime>
#include <iomanip>
#include <sstream>
#include <msclr\marshal_cppstd.h> // for unmanaged piece of code
#include <vcclr.h>
I had this issue recently; I was compiling with visual studio 2015. I tried it with visual studio 2017 and I still got the internal compiler error. Then I tried it with visual studio 2013 and it told me that I can't have a "return" statement inside an openMP section. when I removed the return from VS 2013 and VS 2105 the compiler was able to successfully compile. So, it make sense to try it with VS 2013 and it will give you a better error description. You could also be having return statements inside openMP sections and that could be the reason for c1001 error.
In my case it was a return function from the OpenMP loop. Removing a "return" line solved the problem.
You should simply report it.
In terms of workarounds, it is likely related to memory/resource consumption. Usual tricks to lower consumption are
disable debug information
split up compilation units to smaller size (this might be key here: "I'm using many libraries" should not be an issue unless you're including all the headers in a single translation unit
try to reduce template instantiations
Alternatively
reduce system load (close other programs, such as your Stackoverflow browser that might tatke valuable resources :))