Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
My question is "when we come across a new function, how can we figure out what is the minimum header file/library to include?
In other words, is there a systematic way to figure out the required header/library for a certain function?
To clarify:
I googled and found ofstream to be handy for output I/O. I needed to include <fstream> to be able to use ofstream. how can I determine these two libraries and how to figure out the minimum required one? (well in this case, I googled again! or obviously I could search the filesystem for any file .h or .soor .cpp or ... that define this function )
Reading the documentation is the preferred way.
Searching the filesystems for the function name way too often leads astray - there are many headers that rely on code in a file that includes it in turn.
The cppreference site is a pretty good resource on the standard.
For platform specifics:
If you're on Windows, MSDN tells you exactly which header and library to include.
Linux and Unixes have their man pages.
OS X has the XCode documentation and man pages.
Well you have answered your own question. Either google it or check the API documentation, they should mention what is to be included. For example "man strcpy" tells me that I need to include #include <string.h>.
Additionally, you can also try to understand the relationship among the APIs. For example, fstream provides ofstream and ifstream, so including fstream will help in that case.
When it comes to standard C or C++, I usually use cppreference to find which header file is related with what I want to use.
If it is Unix-related, then the man pages are my friend.
Related
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
A similar issue has already been discussed (C/C++ include header file order), but this thread makes no mention of forward declarations. If I summarize what I've read online so far:
Everyone agrees that the corresponding header for the .cpp file should go first. This ensures that the header files has everything it needs.
Beyond that, there seems to be no consensus. The Google guidelines (https://google.github.io/styleguide/cppguide.html) suggest including headers from system -> other libs -> project. Many people on SO suggest the exact opposite. It seems to be a matter of personal preferences.
Regarding forward declarations, is there any reason to add them before/after the include headers? I don't see why it would matter (Is a class declaration allowed after a class definition?), but maybe I am missing something.
I think this is one of those questions that has no one right answer.
The way I think of it (and its by no means the absolute answer) is from an encapsulation point of view and I tend to do the opposite of the google guidelines for this reason:
If you include your project headers first you are less likley to hit issues where your header relies on some system include that went before it because you will just get a compile error... so therefore I find you see issues faster by putting the system includes last.
Same thing for the forward declarations - if you need them and they are not provided by a header - then add them after the includes because the headers should not need them (otherwise it would be in the header) and so you only declare it as/when you need it...
That is more-or-less my reasoning - but as I say, if your headers are designed correctly then you could do it either way...
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I consider myself a C++ beginner and have come to a question when dreading the fact of using many include directives. So instead of figuring it out myself the long way, I have come to get some help before I can learn and practice more.
From the book Absolute C++ 5th edition by Walter Savitch, says that "The operator sizeof is part of the core C++ Language and requires no include directive or using directive." 12.4 Random Access to Files, page 585
I want to know where can I find other operators or codes that does not use the include directive or using directive.
This could help me build programs before I can start learning more of the C++ Libraries.
Thank you!
What you are asking about can all be answered by going on cppreference. Basically there are many default libraries that can be included by writing #include <lib>. So for example you want to use vectors (well optimized dynamic array), you write #include <vector>, if you want to handle input and output you write #include <iostream>, etc.
All of this can be found documented on page I've linked above. If you don't know with what to start some basic and most libraries would be iostream, string, vector, map.
Then there is an memory library, which provides for example an unique pointer which is a really strong tool in C++.
Then there is an algorithm library, containing useful functions working with containers over iterators.
These are just examples that came to my mind first and I've used them the most. If you are not sure about anything you can always search on cppreference for full documentation on it.
If you have any other or more specific question feel free to ask.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
The question has been asked many times here: "How do I iterate through files in a directory in c++?"
I've seen the responses, typically suggesting to use an external library (usually boost filesystem) to handle this and the OS differences in implementation.
My question is: why? Why is this so difficult?
I'm new to the language and can't help but feel that I'm trying to overstep the bounds of idiomatic c++.
Is it more appropriate to implement a single file solution and use another language to implement the file iteration?
Relative to other languages, C++ has a tiny standard library. This has its advantages (porting C++ to a new platform is much easier), but it also means that, to get a lot of things done, you're going to rely on external libraries.
Filesystem work has not, until C++17, been something that has been a part of the C++ standard. And even now, some people are resistant to the C++17 filesystem library because it doesn't work quite so well with certain types of underlying filesystems.
Note that many languages that have standard filesystem support don't support these platforms at all.
I'm new to the language and can't help but feel that I'm trying to overstep the bounds of idiomatic c++.
If you're going to use C++, then you need to accept that you're going to have to go out and use other libraries for a lot of the stuff that many other languages give you for free. Using a library is how you get things done in C++; it's not "overstepping the bounds" of anything.
You can't iterate the files in a directory in C++ without using platform-specific APIs. The core C++ language and the standard library do not provide any mechanism to obtain a directory listing and iterate through it. So in order to do this, you must use an external library or platform specific API.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
Hello I'm new to coding in C++, thankfully I'm quickly learning how to use it. So far I've heard of libraries and how they can be used in an application.
My questions is are:
What do I need to do after I download ANY library?
How do I #include "library.h" or #include <library> and use it
in a project without intellisense or the compiler going nuts?
What if the library is header only?
What should I look for?
Are there any apps I need?
What if there's no build folder?
Usually the instructions are unclear to me, maybe it's because I'm still green.
Please try to explain this in an easy to follow manner like if you were teaching it to someone that knows NOTHING: I've tried to follow other guides but with no luck.
Beforehand, thank you very much!
If it's header-only, including the header is enough. Otherwise, a library can be any piece of code in any form (source in various languages, binary, shared, static, ...). It's impossible to cover all the cases, each library is supposed to come with its own documentation.
Each and every library has a specific license (eg MIT, GPL, LGPL etc) for it's use. There can be differences between uses (eg personal, academic and commercial etc) but often not. It's quite easy to learn about the major licenses and how they might apply to your use case on the internet. But if you are doing this for commercial purposes or have any doubt whatsoever consult a lawyer.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
This was asked on a past exam. Given that the header file is custom I am assuming not because they are just variations of the main(){ header correct?
I'm not sure if I understand the question, but I would say yes, they are optional. You could write all of your (custom) functions, classes, etc. in one file if you wanted.
I'm new here so I can't comment, but the question is a bit confusing to me. But here is what I think, I hope it helps:
Header files contain functions, variables, classes etc. in C and C++. Header files that come pre-built with your compiler must be included before you can use any function or anything thing inside that file.
Now referring to a custom header files, you may choose to create a file containing specific information to use in your programs, often to make your code look more organize or to create reusable libraries. Those are OPTIONAL simply because you can manage to create all your functions, variables and classes in the same file containing your main(){}. It might look messy, impossible to read but possible to achieve.
BTW I'm not sure about what you mean by header files being variations of the main(), but agreeing with Trevor Hickey, they shouldn't have a main() function since they are not compilable, they don't execute the functions they just hold the information.