Is there a way to programmatically traverse header files? - c++

I have a situation where I am using a library (collection of C++ header files only) that is 80MB in size in my project.
Now, I am only including 3 header files from that library in one of my source codes, but unfortunately each one of those header files include other header files and so on.
I would like to find a way to somehow traverse the header files starting from the 3 header files that I initially include to list all the header files that are being used.
I am trying to do so, so I can only include the necessary header files from that huge library in hope of reducing its size.

GCC has the -M flag (and similar) to generate lists of dependencies. I imagine other compilers have something similar.

Related

Does std:: library of c++ have .cpp files?

This may be a naive question since I'm a c++ beginner.
In a typical cpp project, we have cpp files and their header files. So based on my understanding, usually cpp files have definitions, while respective header files have declarations. During compiling, we have to include very cpp files in the project since header files do not specify the location of the definition of the declared object. Finally, linker links declaration and definition. This allows the communication between different translation units.
However, when it comes to std:: library, we do include header files but never compile its cpp files. For example, we write #include in main.cpp, but when compiling, the command does not have anything like g++ vector.cpp main.cpp ...
I even tried to search cpp files in the std library folder in my system, but all I can find is various header files.
I do know the definition and declaration can be included in a single header file, I don't think this is how std:: is wrote. Otherwise, it will result in multiple copies of definition when included in different cpp files.
Please help me to understand how this works? Can I do something like this with my own project?
When it comes to templated libraries, the source code must be found in the header files (.h or .hpp), so it is not exceptional to find header-only libraries.
If part of the code is not templated and its implementation is made in source files (.cpp), usually the code is precompiled and supplied as .lib (.a/.so) files to be linked to.
As regards open source APIs, if there are .cpp files they are delivered as well and you have to compile/precompile them by yourself.
Yes, there is compiled code in the standard library. You don't have to compile it because the library comes already compiled with your compiler. And you don't explicitly link to it, because the compiler knows about it. So you don't see it mentioned anywhere when you do normal builds. If you set your compiler to some form of verbose mode you can see that the name of the standard library gets passed to the linker, which pulls the code it needs from the library.

How do I add user downloaded header files to the list of header files that is present by default in the system?

I have some header files (.h and .cpp files) which I downloaded from the internet. Want to add them to the list of header files that is already there in the system by default.
I know that I can add them to the directory in which my code is present, which uses these header files. I've been doing that for most of my projects. But it is taking up a lot of unnecessary space.
I want to know if there's any way in which I can make these header files, which I downloaded from the internet as standard header files like stdlib.h, iostream etc.
EDIT: What I want to exactly do is this: I have downloaded the stanford cpp library, which has some very useful functions. I want to use them. But I don't want to copy and paste the include files in my project directory every time. Is there a way to make them as standard header files like stdlib.h or iostream? I use a Macbook. For code-editing, I use Atom. For running and compiling the program, I use the command-line -- g++ is my compiler. I am familiar with Makefiles (GNU Make).
Makefiles wrap other compilers; Xcode uses clang; so I'm going to assume you're using that for now.
You want to locate your rule for generating .o objects; and add (from https://clang.llvm.org/docs/CommandGuide/clang.html) the argument
-I<dir> as many times as required to add all your paths.
gcc is basically the same args https://linux.die.net/man/1/g++ ...-Idir to impact <> and "" lookups -iquotedir to impact "" lookups only; and then -Ldir to impact link paths

How to give lots of header files to MATLAB loadlibrary?

I have a program in C++ and compiled the program into a static library .a file. Now I want to use this library in MATLAB. Seemingly all remaining steps to use it in MATLAB is to give to MATLAB this .a library and the header files using loadlibrary(libname,hfile) command.
The problem is that my program consists of about 200 .h files located in different folders under the main source folder. How can I give all of these header files to MATLAB loadlibrary? I just started to integrating all of the headers into one header file; but is there a better, easier way to this?
This will require some work on your part.
First option is to add additional header files in the loadlibrary command
loadlibrary('mylib','mylib.h','addheader','header2')
You'll need to have a list of all your additional header files.
Alternatively, you can explore the prototype file option instead of ehader file.
See this link for more information.

Combining Header files for a library

I was wondering, when making your own static library, is it a good idea to combine all the header files into one wrapper header file? For example, let say I have a bunch of *.h and *.cpp files, which I compiled into *.lib files. Is it good practice to make a wrapper header file, so when I write include, I only have to include one header, which will include everything else, which I may or may not need. Does this bloat up the *.exe file, which would have been linked to the library file with the stuff that wasn't being used?
If it is a static library, it is going to become part of exe anyway whether you make it one header or several headers. However, from a design perspective, it is better to keep headers separate so that the user of that lib includes only the headers that he specifically needs.

What to put in precompiled header? (MSVC)

What are the best candidates for a precompiled header file? Can I put STL and Boost headers there, even though they have templates? And will that reduce compile times?
Also, what are the best IDE settings to reduce compile times?
The quick answer: the STL and Boost headers do indeed belong in the precompiled header file, even though these header files define template classes.
When generating a precompiled header file, a compiler parses the header text (a significant task!), and converts it into a binary format that is optimised for the compiler's benefit.
Even though the template classes will be instantiated when other .cpp files are compiled, they will be instantiated from information in the precompiled header, which is significantly faster for the compiler to read.
(later addition)
One thing that you should not include in a precompiled header are files that are part of your project and are changed frequently, even if every single .CPP file includes these files.
The reason is this - the generation of the precompiled header can take a long time, because the boost, stl and windows libraries are very large.
You might have a simple file (eg "StringDefs.h") that everything uses. If StringDefs.h is included in stdafx.h, and one developer touches StringDefs.h, then every developer has to wait until the entire precompiled header recompiles. It would be much faster if StringDefs.h was left out of the precompiled header, and parsed along with each .CPP file.
One addition to Andrew Shepherd's answer. Use the precompiled header for header files that are external to your project, for files that change infrequently. If you're changing the header files in the current project all the time, it's probably not worth precompiling them.
I've written an article on techniques that reduce the compilation time. Among these techniques a post on precompiled header and its application can be found here. It also has a section on best practices that you may find interesting. CMake scripts that handle it transparently are included.
Put anything in the precompiled header that most of the .cpp files in that project would include anyway. This goes for any header file, really. This allows the compiler to parse these files once, and then reuse that information in all .cpp files in the same project.