Anyone know how to rename a header file in vscode so that all includes of the header file will be updated accordingly?
For example, if I rename Header0.h to Header1.h, all includes will change from
#include "Header0.h"
to
#include "Header1.h"
Use of extensions is also welcome
Related
ALWAYS when I start a new class (EX: main.cpp) I need to
#include <iostream>
#include <string>
#include <math.h>
There is a way to make it automatically ? I mean every time when I create a new class they will already be included ?
A cpp file is not a class, it's a source file. A cpp file may include a class, or multiple classes, or no classes. Similar, a header filer is not a librarie, it's just a header file.
Add your includes to a header (.h file), and then your cpp file only has to include that single header to include all those common includes. Visual Studio even has something called a precompiled header, which is exactly meant as such a header with common includes, except that it's precompiled (which means, using that will compile faster than using a regular header). Afaik, you'll still have to include that single header yourself, tho, so you won't get around writing at least one #include ...
My solution is more a workaround than a "real" solution but : You need these lines at least once in your program.
So I'd create a headers_container.hpp file containing stuff my whole program needs, like these #include.
For example :
headers_container.hpp :
#include <iostream>
#include <string>
#include <math.h>
// Some stuff my whole program needs...
in your *.cpp files :
#include "headers_container.cpp"
// Your compiler knows iostream, std::strings and math now
Make sure the path to headers_container.hpp is correct (if the .hpp is not in the same folder as your .cpp
Using this method, you can add one #include in headers_container.hpp and it will update all the .cpp files.
Moreover you can write a small script to generate files (I did the script you can find here : https://gitlab.com/-/snippets/2033889 )
Enjoy your way in programming ! :)
I have not used C++ in visual studio in a lonnng lonnng time so probably this is a basic question but I would like some help.
I am opening someone else C++ code and the first thing I notice is that it is not recognizing its includes.
For example I have a main.cpp that has
#include "stdafx.h"
#include "myheader.h"
this cpp file is inside a "source" folder and those header files are under a "headers" folder.
I got an error "cannot open source file "stdafx.h" and "cannot open source file "myheader.h"
It is been ages I haven't touched C++ in visual studio. What configuration I should do to fix this?
if source and headers folders are in same level in folder structure you can use
#include "..\headers\myheader.h"
Similarly locate your stdafx.h and add the relative path to the .cpp file.
My app is working correctly, but I'm trying to clear up something. When I include the precompile header in my source files, I get the following happen.
Basically the first line is underlined as if there is a problem with it, so I have to include it again underneath, with the correct path... but I need both or it wont compile.
Any ideas?
The Visual Studio uses precompiled header only if the name matches (the name is not case-sensitive). Let say, a precompiled header name StdAfx.h is set in your project, so only when a code contains #include "stdafx.h" then is precompiled header used.
The #include "..\stdafx.h" is not recognized as the precompiled header even though it is the real location.
You need to add an include path to the location of stdafx.h into your project settings and then the header #include "stdafx.h" will be work correctly.
Another common issue with a precompiled header is that the include directive is in a header. It should be always in the source file (.ccp).
I was always thinking that I must include #include "Stdafx.h" line at very top of each header file in project like it is said in description. I have created simple MFC dialog based application with help of wizzard and found that header files not contains #include "Stdafx.h" lines, but cpp ones has them. So, when I must use #include "Stdafx.h" line in cpp and when in h one?
Short answer: in a .h: never; in a .cpp if, and only if, it is configured with precompiled headers (they are by default).
The rationale is that #include "stdafx.h" is used to include the precompiled headers (built when compiling stdafx.cpp). In order for them to be effective they must be the very first line of the compilation unit (not counting blanks or comments). And that very first line cannot be in a header file because the #include that includes it comes first!
I would recommend using #include "stdafx.h" in cpp files only, since you might want to compile one cpp 'with' precompiled headers, and another cpp 'without' precompiled headers.
Also, when you create projects using DevStudio's wizard, these stdafx.h includes will be located in the cpp files.
I have a header file called simpio.h, which is in the same folder as the file which is including it in its header. However, I keep on getting the error "Cannot open include file: 'simpio.h': No such file or directory." I am using Visual C++ 2008 Express Edition. Help would be appreciated.
Thanks
You need to use double quotes:
#include "simpio.h"
You have to know that you should use <> when you are trying to include a standard library header or when you want to include a file for which the path is contained in the Additional include directories option.
You have to use "" when you whant to include a file who doesn't satified the previous explanation, let's say that it is almost always file specific to your project.
Some example :
#include <iostream> // library file
#include <boost/thread.hpp> // there is "C:\SDKS\boost in my Additional include directories
#include "MyHeader.h" // Local file, at the same place of my .vcproj
#include "Header/AnotherHeader.h" // Local file in a folder named "Header"
In your case, we can think that you are in the second case. You just have to do :
#include "simpio.h"
Or if your file is in another folder :
#include "SomeFolders/simpio.h"