Is it possible to include the C++ standard library in a single statement, or must you do it header by header?
No, there is no shortcut to include all of the standard headers. You must generally include each one that you require separately.
It's possible to create a single header file that includes all standard library headers—something like all.h. However, you should consider whether doing that is a smart move, as it will extend compilation times significantly.
Also, make sure you understand the C++ compilation model before you make such decisions. If you don't already know, find out what's a translation unit, an object file, what the linker does, what's the difference between including a library header and linking a library.
Under Visual Studio you could put all such header files inside your precompiled header file. Since this file is included by all your files all stl hearders would be available everywhere. If all the includes in precompiled header file are from external libraries, then precompilation should speed up compilation times.
No, there is no shortcut to include all the standard headers.
Maybe you can include all the headers in your own all.h, then #include<all.h>,but it is not recommended.
GCC has a header called <bits/stdc++.h>, but this should never be relied on in a typical context.
As noted by the OP, this is the kind of thing you might only want to use in a competitive programming contest, where only running time matters.
Related
When attempting to compile an externally provided C++ codebase, I've encountered a confusing problem:
Several of the header files are missing #include <MyLibrary.h>, where MyLibrary is an obvious dependency that is simply missing, and preventing compilation.
My question is:
When compiling C++ code, is there a way to automatically include a dependency header file, without needing to #include it in each of the .h/.cpp files where it is required? In other words, a way to provide the C++ preprocessor a list of header files to automatically include when compiling all of the source code?
I understand that this is probably a very bad idea, but I'm trying to determine if the code I've been provided is simply broken, or if there's some way it could be compiled without manually fixing each file with missing dependencies.
The C++ language standard does not support "global headers", but individual compilers do.
It's called a forced include.
With Visual C++ it's the option /FI, and with g++ it's option -include.
With Visual C++, however, the usual way to include common headers is to place those includes in a file called stdafx.h, and include that file in every translation unit, first of all. That's part of Visual C++'s precompiled header support. It's problematic because when it's turned on (and it's on by default in a Visual Studio project) it changes the preprocessing rules so that some standard code may not compile, but it can speed up larger builds considerably.
Yes, at least with gcc and compatibles.: Use the -include option. See https://stackoverflow.com/a/3387518/3150802.
In C/C++ project, mostly the file can be of either types .h or .c/.cpp. Apart from their naming difference such as header and implementation files; is there any functionality difference ?
In other words: if in a working C/C++ project what difference it makes if we change all files with .c or .cpp extension ?
[Note: We can also have #include guards for .c/.cpp files. We can skip their compilation if they are observed as headers.]
Edit:
Debate is not intended for this as I don't have any solid use case. Rather I wanted to know, that allowing to give .h,.hxx,.i extensions are just a facility or a rule. e.g. One functionality difference I see is that .cxx files can have their likable object files.
What difference does it make? The compiler is perfectly happy about it. To it, it's just files.
But to you? You makes a lot of difference:
you're no longer able to immediately figure out which one is the header and which one is the implementation;
you can no longer give header and implementation the same name;
If you are using gcc, and you try and compile a bunch of C++ files labled with a .c extension, it's going to try and compile your file as-if it were a C-language file, which is going to create a ton of errors.
There's also project sanity as well ... this is why many times you'll see projects actually label C++ headers as .hpp rather than just .h so that it's easier to create a distinction between C-language source-code and headers, and C++ source-code and headers.
Header files generally must not be compiled directly but instead #included in a file that is directly compiled. By giving these two groups of files their own extension it makes it a lot easier to determine which to compile.
Make and IDE's and other tools normally expect the conventions of .c/.cpp for source and h/hpp for header. Compiler normally goes a step further and defaults to C compilation for .c and c++ compilation for .cpp
Hence, a bad idea to give your headers the same extension as the the source files.
I know precompiled headers are used for speeding up compilations, but are there any do's and don'ts to what files I should include in them? For example, I have a project that uses a lot of boost libs so I just include the boost header files in stdafx.h (I'm using VS2008). Should I include every standard header file in them, too? Will this increase the size of my executeable even if I, for example, include <vector> but never use std::vector? Is it a bad idea to include my own project's header files in stdafx.h?
Generally speaking, every header file that you use across the application and that doesn't change often should go into the precompiled header file. This will speed up compilation because the precompiled header file gets compiled only once.
If you add a header file which changes often, you'll miss the point of the precompiled header file, because this often-changing header file will cause your whole project to recompile, possibly unnecessarily.
Specifically, defines a template class, so if you won't use std::vector, the overhead will not be big. However, I would advise against adding header files - however standard and generic - if you don't really need them. There IS some overhead to the compilation time, the binary size, and it could cause conflicts later in the project, so why add something if you don't really need it?
Pre-compiled headers don't affect the size of your executable, only the compilation speed. Since they are pre-compiled, they don't have to be re-compiled all the time. Windows.h is the primary beneficiary of this feature.
It's a good idea to include the c++ standard header-files and the boost library headers and any other headers from third party libraries that you frequently use. This will not affect the size of your executable.
However, you should not include headers from your own project, since the whole project needs to be rebuild whenever you make changes in these headers.
Why do we need to add both includes and libs to the compilation?
Why doesn't the libs wrap everything in it?
Header files define interfaces; libraries provide implementations.
The header for a library is going to tell your compiler the names and signatures of functions provided by the library, the names of variables provided by the library, and the layout of classes provided by the library.
The library itself is compiled code which is executed at run time. Using the header during compilation allows your compiler to generate compiled code which knows how to invoke and communicate with the existing library code.
A header file (usually) only contains declarations for classes and functions. The actual implementations are built from CPP files. You can then link against those implementations with only the header declarations available.
I'm guessing this is your way of handling the question you asked at How to make #include <mysql.h> work?
Unfortunately, I think the better solution is to either learn more about C++, or learn more about Google, before posting absolutely everything to this site.
I have a project that uses Boost.Asio and the Media-Decoding-Samples that come with the Intel IPP-Library. The problem is the following. If I compile the project without defining WIN32_LEAN_AND_MEAN, Asio complains with the infamous "winsock.h already included" error. If I define the macro, a header in the other library has undefined types, because it relies on windows.h including winsock.h. I tried adding winsock.h to that header manually, but this makes Boost.Asio cry out again. The only solution I can think of would be to go through every compilation unit and make sure that Asio is always included first. Due to the size and complexity of the project (and the uglyness of that solution) I would like to find a better way.
Thanks in advance !
you can get around this if you split up your source and make sure not to use some kind of common header in your application in which you incude both Asio and IPP. So the source files needing Asio include Boost headers, the source files needing IPP include the IPP headers, but the header files for your classes that need to refer to both should not include any external headers, and use forward declarations if needed.
" a header in the other library has undefined types"
How many? How complicated? Perhaps you could define those types?