visual studio code showing wrong autocomplete suggestions in c++ - c++

I am trying to learn SDL in C++ So auto-complete is very essential for me.VS code was working fine with SFML but due to android problems I started learning SDL and now auto-completions for SDL is not working correctly . It always suggest some wrong members ,even If I didn't include any library . I don't have enough experience with VS . I came to VS from neovim only for auto completions which not working properly :(
I had tried restring ,updating and deleting .vscode dir but nothing happened

Hover your cursor on the header file mentioned in the source code
You'll notice a red/green swiggly underline
A light bulb icon appears, click on it
select the option called "edit includepath setting"
It takes you to a new "c_cpp_properties.json"
In this json file, under your platform, add all the paths to the headerfiles
After doing this, most of the unwanted intellisense results disappear. Only releavent ones show up the top.
VS code is a very good tool which comes with source control features. Add C++ extension and a debugger and you are good to go.

Related

VSCode does not show errors before building

I use VSCode(code editor, not IDE) for C++ with only Microsoft's C++ extension and today I have encountered a problem here. When I write the wrong syntax, the "PROBLEMS" panel does not show errors. It only shows errors on building the code. Earlier that wasn't the case. How do I fix this ?
Before building:
After building:
I got that error by Disable error squiggles. If you don't reach that error in my case, don't read anymore.
After ignoring, I got your problem:
To fix this, open settings.json file:
Scroll to the end, then set "C_Cpp.errorSquiggles": from Disabled to Enabled.
After reading the question, and the comments, my understanding is that you want Visual Studio Code to tell you when you make a mistake on the sintax, without having to compile the project.
What could be wrong is your C++ IntelliSense, which is odd because it comes in bundle with the C/C++ extension from Microsoft. The extension itself might be disabled, corrupted during an aborted update, or uninstalled.
Have you tried the good old and helpful uninstall and reinstall the extension? That normally fixes my problems with extensions in Visual Studio and VS Code
Note: You have to install the extension first: C/C++ IntelliSense, debugging, and code browsing extension
This is because the C/C++ IntelliSense, debugging, and code browsing extension does not know about the current project.
Navigate to View | Command Palette, enter and select C/C++ Build and debug active file: Select
Project, and then select the correct project that you want to work with.
This will help you see the problems as you create your code without requiring you to run it.
The extension has to know that it is allowed to continuously check your code.
I hope this helps you in the future with other extensions too...
Try and close your current folder and open a different folder. Then come back to your original folder. This worked for me.

C++ Setup in Visual Studio Code fails to run

I just downloaded VSC in order to start learning C++. Previously I had just used Notepad++ for web design and Python IDE for Python, but I decided to move onto Visual Studio Code for a more sophisticated code editor.
However, when trying to run a basic program, I don't have the option to run in the first place. This picture is what's supposed to come up when you right-click on the text area (from multiple YouTube tutorials):
However, this is what comes up on my screen.
Additionally, there is no "run" button on the top-right of the screen like the tutorials show.
My folders regarding VSC in File Explorer are quite simple. I have a folder for my .cpp code, and MinGW as a compiler. My build is Windows 10.
Obviously, this depends from person to person, but is there any key way this situation can be resolved? Thank you in advance.

Red squiggles do not appear in VS2008

I am using VS2008 pro and while autocomplete works properly I get that problem. My project is in visual c++/MFC and advanced settings are there for C#, but not for C/C++ as you can see below in order to enable them.
I figured out that it may be Intellisense that does not work properly, so have tried to clean my solution file and deleted .ncb and rebuild my project as others suggest, but without success. I have tried to create other projects, and the problem insists.
Finally, as an alternative I have found that I can use Virtual Assist, but this is the last choice for me.
So, how could I make red squiggles work again?

After using QFileDialog program crashes after few seconds

I am making app to increase productivity in our company. It reads the whole .csv file and puts its contents to a table in .pdf format. I am using QT as GUI library and PoDoFo as .pdf library.
The thing is, after I let user select .csv file to get data from and path to save generated .pdf, the program crashes in two minutes or so. It varies from case to case. I was able to "track" the issue down to the QFileDialog.
It doesn't matter if I created it on the stack, or dynamically still the same thing happens. From the moment you press "Choose" in the GUI, which opens up file dialog, program will crash in something around a two minutes giving this in Visual Studio Community 2017
I was able to create minimalistic code, to reproduce the error. I am using Windows 7 Pro, Microsoft Visual Studio Community 2017, compiling 32-bit debug
.h,.cpp,.ui files: https://www.dropbox.com/sh/cf057p4lnumesol/AAD39zDJnrxGChXAuE7y27Kra?dl=0
Any help would be appreciated, I've spent 5 days of my free time trying to figure this out. Thanks
EDIT: I understand that the "Frame not in module" means that visual studio doesn't have debugging symbols for the part of code where crash occurred, since I did not built qt myself, that makes sense.
EDIT2: I am using QT Visual Studio Tools addon version 2.3.2
Qt version is 5.12.2
EDIT3: Crashes only occurs when compiling for 32-bit arch.
EDIT4: Crash is happening only if using native file dialog. If option QFileDialog::DontUseNativeDialog is set, then no crash occurrs.
Not sure if this is related, but I had an issue where if I gave a path to the FileDialog it would grind away for a few seconds because it was somehow interpreting it as a network path. To fix this I gave it a path that had a prefix of "file://". To accomplish this I did this:
QUrl().fromLocalFile(cell_info->progDir()).toString()
The original path came from cell_info->progDir(). Somehow that path was a regular windows specific path. Once I wrapped it in the from LocalFile call it produced what I needed for all platforms I was using it with.
Again, I have no idea if this is related to your issue, but it sounds fishy like it is grinding away in the background on something. If this is not related feel free to ignore.

Visual Studio Questions/C++

I'm a hobbyist developer and have a background with Java (IDE of choice was Eclipse). I'm using Visual Studio Express 2010 and wanting to learn C++.
Few questions:
I create a "HelloWorld" in C++ and compiles/runs in VS/Windows. When I try to compile it under Linux/GCC, it obviously throws tons of errors. Default windows console project includes windows specific files; but if just create an "Empty Project" it throws tons of linker/build errors. What's the best practices here to keep my code portable?
Why is it creating 47 files for 8 lines of code?
How do you format code? You can do Edit->Format Selection, but the hotkeys don't work?
How do I output to VS's 'Output' Window? ( like eclipse does when you run a console java app )
It keeps reverting my "Project Location" to my "home directory" every time I restart. How do you change it? Is it a bug? Because it's Express edition?
Is there a way to keep it from switching to Debug view when it runs?
I create a "HelloWorld" in C++ and
compiles/runs in VS/Windows. When I
try to compile it under Linux/GCC, it
obviously throws tons of errors.
Default windows console project
includes windows specific files; but
if just create an "Empty Project" it
throws tons of linker/build errors.
What's the best practices here to keep
my code portable?
For portable code, avoid VS wizards entirely. Use Make/NMake if you're starting with rocks and sticks, or the portable build system of your preference (Ant, CMake, etc.) Some of these will spit out a VS solution/project file for you to use.
Why is it creating 47 files for 8
lines of code?
Wizards are magical like that.
How do you format code? You can do
Edit->Format Selection, but the
hotkeys don't work?
Ctrl-K Ctrl-F (under Edit, Advanced)
How do I output to VS's 'Output'
Window? ( like eclipse does when you
run a console java app )
Lookup OutputDebugString() for the debug window. Output window should get all cout/cerr output.
It keeps reverting my "Project
Location" to my "home directory" every
time I restart. How do you change it?
Is it a bug? Because it's Express
edition?
Probably hidden in options somewhere - don't know that one, sorry.
Is there a way to keep it from
switching to Debug view when it runs?
Launch using Ctrl+F5 to run without the debugger attached.
Have fun!
I create a "HelloWorld" in C++ and compiles/runs in VS/Windows. When I try to compile it under Linux/GCC, it obviously throws tons of errors. Default windows console project includes windows specific files; but if just create an "Empty Project" it throws tons of linker/build errors. What's the best practices here to keep my code portable?
It's fairly difficult to keep your code truly portable if you're writing Windows applications. Standard C++ will obviously work on either platform, but Linux can't run Windows applications, and vice versa. Remember that console applications are also considered Windows applications. They're not any more "pure" just because they are text-based, rather than graphical. Windows applications have their own entry point, different from the standard main function found in ANSI C++ (technically, main is still there, but it's hidden and called internally by the Windows libraries).
The best thing to do is not to link to any of the Windows headers. Unfortunately, you won't be very satisfied with the results. About all that you'll be able to generate is library code. You can't get a UI on the screen unless you use the Windows functions to do it.
An "Empty Project" is just what it says—empty. I assume the build errors are because you're trying to call functions that aren't defined anywhere. You'll find that you need to include windows.h to get off the ground, which instantly makes your code non-portable.
Why is it creating 47 files for 8 lines of code?
This is obviously an exaggeration; none of the wizards produce anywhere near that many code files. Especially not the "Empty Project", which doesn't create any at all.
A Win32 console application includes the following 5 files:
stdafx.h and stdafx.cpp — these files are used to enable "precompiled headers", meaning that Visual Studio will compile all of your headers once, and only recompile them when they change, rather than recompiling them each time you build the project. This used to provide enormous speed boosts, and still does on large projects. You probably don't need or care about this for small projects, but it's not a bad idea to get familiar with their usage if you're going to be developing in Visual Studio.
A targetver.h file, whose only purpose is to specify the earliest version of Windows that you want your application to run on. This is necessary because later versions of Windows add additional functionality that wasn't available in previous versions. Your app won't run if you link to functions or libraries that don't exist. Set this up once and then forget about it.
A <projectname>.cpp file, which is the implementation code for your application. This is pretty standard stuff—it includes the _tmain function, which is the entry point for a console app.
A ReadMe.txt file, which you can immediately delete. It contains some introductory information and describes the files that have been added to your project. (Yes, reading this yourself could have answered this question.)
A Win32 application would have a few more files, but most of the same ones as well. In particular, you'll see a resource file (with the extension .rc) that contains the icons, dialogs, bitmaps, cursors, etc. used in your program.
If you don't like this structure, you can either forgo the use of a wizard, or modify it yourself. There's nothing set in stone about it.
How do you format code? You can do Edit->Format Selection, but the hotkeys don't work?
Formatting code works fine. I'm not sure why people are telling you that Visual Studio doesn't support this, or that you'll need a third-party plug-in. There's no "Format Document" command as there is in C#, but the "Format Selection" command works just fine. The only difference is, you have to select something in order for it to be enabled.
The default keyboard extension for that command is CtrlK, Ctrl+F. It also works fine, right out of the box. My typical workflow is to hit Ctrl+A first to select all.
How do I output to VS's 'Output' Window? ( like eclipse does when you run a console java app )
I don't know what Eclipse does, nor do I know anything about Java. What do you want this to do? When and what things do you want to get written to the "Output" window? A console application will run in a console window, not in the "Output" window. That's not what it's for.
It's intended for debugging purposes. The OutputDebugString function is one way of utilizing it. The output of the standard cerr keyword should be automatically redirected to the "Output" window.
It keeps reverting my "Project Location" to my "home directory" every time I restart. How do you change it? Is it a bug? Because it's Express edition?
This isn't a bug, it's a feature. Visual Studio is designed for working with projects and solutions, not one-off code files. So by default, it prompts you to specify a project folder, a location to store your files. And what better place for the default location than your home folder?
If you don't like that location, you can change it. Under the "Tools" menu, select "Options". Expand the "Projects and Solutions" category, and click the "General" item. Then, change the path of the "Projects location" (the top textbox). Couldn't get much simpler than that.
Is there a way to keep it from switching to Debug view when it runs?
I frankly don't understand how this question makes any sense at all. When you run an application with the debugger attached, Visual Studio switches to a different window layout specifically optimized for debugging. I just answered a similar question. The upshot is that there's no way of telling Visual Studio to use the same window layout for both design and debug view, but I also can't imagine why you'd want to, either. Different things are useful, depending on what you're currently doing.
The two window layouts are customizable, and your changes are remembered. I've customized mine heavily from the defaults; it's very likely that your tastes vary as well. There are lots of great features, like the "Locals" window, which shows a listing of all the values of the local variables in scope at the point where you break into your program's execution.
Also remember that the default "Debug" and "Release" build configurations have nothing to do with whether or not Visual Studio automatically attaches the debugger to your application's process. If you want to start your app without the debugger attached, select "Start without Debugging" from the Debug menu, or press Ctrl+F5. There are lots of side effects to this though, and it's probably not what you wanted. Without the debugger attached, you lose most of what Visual Studio provides to you as an IDE. You might as well just run the app from Windows Explorer without even launching VS.
Finally, if you prefer Eclipse (or at least are already accustomed to its nuances and prefer not to learn Visual Studio's), you can still use it for C++ development. Download it here.
If you're just wanting to learn C++ and you don't necessarily care about the platform, I would probably avoid using Visual Studio to start with. Visual Studio provides some functionality for managing projects and builds, but honestly, I think you're better off learning how to manage code files and use the compiler on the command line first, then working up from there.
If you're on Windows, I'd recommend installing Cygwin and getting the GNU compiler tools through the Cygwin setup utility (gcc or g++).
This is a bit of an opinionated answer, but my experience with C++ on Windows leads me to believe that you'd be better served trying to learn C++ from more of a unix-like angle. Windows C++ adds a whole layer of crap that will just confuse you when you're getting started.