Unable to use Precompiled headers in visual studio - c++

I've seen several questions discussing this topic but none of their solutions seems to apply here. I have several libraries that I don't wont to be compiled every time I build the project so I've created "b5pch.h" and b5pch.cpp" files.
//b5pch.h
#pragma once
#include <iostream>
#include <memory>
#include <utility>
#include <algorithm>
#include <functional>
#include <sstream>
#include <string>
#include <vector>
#ifdef B5_PLATFORM_WINDOWS
#include <Windows.h>
#endif
//b5pch.cpp
#include "b5pch.h"
In properties I've set precompiled header for every cpp file to be Use(/Yu) like so:
And for b5pch.cpp it's set to Create(/Yc)
after that I've added #include "b5pch.h at the start of each cpp file(I only have two not including b5pch.cpp) but when I try to build the project I get two errors saying exactly the same thing
Error C1010 unexpected end of file while looking for precompiled header. Did you forget to add '#include "b5pch.h"' to your source?

Okay I've fixed the problem. when I was including b5pch.h in my cpp files I was doing it like this:
#include ../b5pch.h since they were in different directories.
When I moved pch files in same directory and I just wrote #include b5pch.h there were no more errors. I didn't wanted them to be in same folder so I've moved them back out but in Project Properties->Additional Include Directories I've added "src" so I could just use #include b5pch.h in my cpp files even tho they were not in the same folder.

Related

Unreal Engine and Clion (can't resolve some sybmols)

UE 4.23
CLion 2019.2.1 (clangd server off)
After testing this newbie tutorial i have many unresolved symbols in clion like on picture:
Only when I add this-> to variable or method - the red letters are disappear.
How avoid this?
In your FloatingActor.cpp file, add the following includes after #include "FloatingActor.h":
#include "Components/StaticMeshComponent.h"
#include "ConstructorHelpers.h"
will solve the problem.
Please note that you should only put these includes inside the .cpp file, not the .h file.
Here are my includes in .cpp file and .h file.
FloatingActor.h
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "FloatingActor.generated.h"
FloatingActor.cpp
#include "FloatingActor.h"
#include "Components/StaticMeshComponent.h"
#include "ConstructorHelpers.h"
More discussions here.

Why do these headers only work outside of the pre-compiled header?

In stdafx.h:
#include <fstream>
#include <sstream>
In example.cpp:
#include <stdafx.h>
std::ifstream in_stream;
std::stringstream st_stream;
If I don't place the fstream and sstream includes in the .cpp file I get a ton of errors, such as:
Error C2079 'in_stream' uses undefined class
'std::basic_ifstream<char,std::char_traits<char>>'
Error C2228 left of '.exceptions' must have class/struct/union
Why do the errors disappear if I place the appropriate includes directly in the .cpp file? Shouldn't the functionality be identical?
This should be written as "stdafx.h" not <stdafx.h>, because "stdafx.h" is not a standard header file (that's just C++ ethics, not rules).
Visual Studio automatically creates this files and adds a bunch of header files to it.
If you have a large project with many source files, and <fstream> is used in many source files, then include <fstream> in "stdafx.h". Otherwise avoid editing this file.
std::ifstream requires <fstream> header files. The required header file is mentioned in relevant help pages. See for example std::ifstream help
Add the relevant header files directly in your "myfile.cpp" file:
//myfile.cpp:
#include "stdafx.h"
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
int main(){...}
If you have a small project you can tell Visual Studio to stop using precompiled headers via "Project Settings" -> "C/C++" -> "Precompiled Headers". This way you can remove "stdafx.h" and your source file will be more compatible with different compilers.

How to include all source files from folder? (C++, MS VS 2013)

I have simple project where I use tiny ttmath library for C++ (big nums).
This library consists of 13 *.h files.
I have included all these files in a stupid way:
#include "ttmath\ttmath.h"
#include "ttmath\ttmathbig.h"
#include "ttmath\ttmathdec.h"
#include "ttmath\ttmathint.h"
#include "ttmath\ttmathmisc.h"
#include "ttmath\ttmathobjects.h"
#include "ttmath\ttmathparser.h"
#include "ttmath\ttmaththreads.h"
#include "ttmath\ttmathtypes.h"
#include "ttmath\ttmathuint.h"
#include "ttmath\ttmathuint_noasm.h"
#include "ttmath\ttmathuint_x86.h"
#include "ttmath\ttmathuint_x86_64.h"
What is the right way? I expect smth like this:
#include "ttmath\*.h"
but can not find...
What is the right way? I expect smth like this:
#include "ttmath\*.h"
but can not find...
That won't work because the preprocessor is not going to expand characters to match things in the way you expect wildcards to work.
My recommendation would be to create a single custom header file of your own, and place all the #include entries in there. For example, in your .c file, you can add your own header:
#include "my_header.h"
And the contents of my_header.h would be:
#include "ttmath\ttmath.h"
#include "ttmath\ttmathbig.h"
#include "ttmath\ttmathdec.h"
#include "ttmath\ttmathint.h"
#include "ttmath\ttmathmisc.h"
#include "ttmath\ttmathobjects.h"
#include "ttmath\ttmathparser.h"
#include "ttmath\ttmaththreads.h"
#include "ttmath\ttmathtypes.h"
#include "ttmath\ttmathuint.h"
#include "ttmath\ttmathuint_noasm.h"
#include "ttmath\ttmathuint_x86.h"
#include "ttmath\ttmathuint_x86_64.h"
Basically, you put everything in a single header, and include that one instead.
The preprocessor doesn't have an "include all" built into it. Neither does it accept wildcards in filenames. You'll have to manually include all of them.
A common solution is to place all the includes in a new .h file and include that one every time you need all of them.

fatal error: vector: No such file or directory

I have an Android project comprising of lots of native code in C++. However, I am unable to build my library as it is not able to find out vector.h header file. What could be the issue ?
A sample of my inclusions in almost all the pages.
#include <jni.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <vector>
The compiler is able to find out all other header files except vector.h in every file. Any suggestions about where am I going wrong ?
NOTE : Filenames end with .cpp and I have already tried #include <vector.h> , #include "vector.h"
Thanks !
The issue was finally resolved by creating Application.mk in JNI folder of project and adding the following to it :-
APP_STL := stlport_static
For more details, refer to this question on SO

What's the benefit of including common files in one single header file?

I was looking at the doom3 code on github and I notice something unusual. Several files have only one include for a file called idlib/precompiled.h and this file have includes for several other headers like
...
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <assert.h>
#include <time.h>
#include <ctype.h>
#include <typeinfo>
#include <errno.h>
#include <math.h>
...
and to program headers
#include "../framework/BuildVersion.h"
#include "../framework/BuildDefines.h"
#include "../framework/Licensee.h"
#include "../framework/CmdSystem.h"
#include "../framework/CVarSystem.h"
I wonder if there's any good reason for that, because that's the first time I see such thing
This is called precompiled headers. The main benefit is dramatically increased compilation speed.
All the headers in precompiled.h are compiled only once per project. Without precompiled headers, each header content would be compiled multiple times: for each .cpp file it is included in.
Dependencies are best served with an include-once in the using source (pragma, guarding defines). That involves a very small overhead as you get a tree of repeated includes: including a header while including a header.
However for the standard libraries are/were sometimes not so well organized and to provide a standard base of headers this was easiest. It also reflects a kind of "module" idea, bundled headers for the basic layer.
As to local includes: it is likely to be non-object-oriented lazyiness, not expressing dependencies.