#include headers in C/C++

2019-04-29 08:54发布

After reading several questions regarding problems with compilation (particularly C++) and noticing that in many cases the problem is a missing header #include. I couldn't help to wonder in my ignorance and ask myself (and now to you):

Why are missing headers not automatically checked and added or requested to the programmer?

Such feature is available for Java import statements in Netbeans for example.

9条回答
走好不送
2楼-- · 2019-04-29 09:25

The compiler shouldn't have to think for you. What if there is a function of the same name in two different libraries? How would it know which header to include and which library to link against? Having a compiler or IDE silently fixing your sloppy code is a bad idea in my opinion.

查看更多
Lonely孤独者°
3楼-- · 2019-04-29 09:25

Because in general it's a difficult problem to know which header files you need to include to define something properly. It would be quite a nice feature to have your IDE be able to guess simple cases and offer to help though.

查看更多
\"骚年 ilove
4楼-- · 2019-04-29 09:29

Part of the difference is because of a few fundamental design decisions that were made differently between the two. In particular, Java requires that the name of a class match the name of the file, so the name of the class you use pretty much tells it what you needed to import.

In C or C++, the name you give to a header doesn't necessarily have to match the contents at all. If you wanted to badly enough, you could name your headers 1.h, 2.h, 3.h, and so on -- or even 1.bas, 2.pas, 3.java, 4.ada, and whatever other misleading names you happen to prefer. That's obviously a poor idea, but if you did it anyway, the compiler wouldn't be at all bothered.

As such, it's much more difficult for a C or C++ tool to guess at what header needs to be included to get the definition of a particular type. In theory, it could (for example) build a big database of all the functions, classes, types, etc., in all the headers you've written, and when you use one, tell you which header(s) define what names, but I'm not aware of an IDE that actually does so.

查看更多
家丑人穷心不美
5楼-- · 2019-04-29 09:33

If I reference a function named sqrt, how does the compiler know which file to look in, if I haven't specified it? It could be absolutely any file on my entire harddrive.

Unlike Java, C++ doesn't really consider any files "special". Java has its giant (bloated) class library, which is automatically made accessible to the programmer.

In C++, this concept doesn't exist. You tell the compiler which paths to search in, and whenever you #include a file, it will search for the filename in those paths.

If that happens to find a standard library file, it'll use that. If it happens to find a third-party file, it'll use that.

The compiler doesn't know that sqrt is defined in the header math.h. Or that it is also typically defined in cmath In fact, the functions defined by a header might vary. Perhaps, if I #define the appropriate preprocessor symbol, some functions will be removed from a specific header, and others will be enabled.

But unlike Java, where the functions and classes defined by a library can be determined just by examining the library file's metadata, in C++, the header has to be compiled. And the result of compiling it might vary depending on the context in which it is included.

So the C++ compiler cannot guess at which header should be included in order to define the function you just used.

查看更多
成全新的幸福
6楼-- · 2019-04-29 09:34

Remember the clash in Java between java.util.Date and java.sql.Date? If someone uses Date in their code, you can't tell whether they forgot import java.util.Date or import java.sql.Date.

In both Java and C++, it is not possible to tell with certainty what import/include statement is missing. So neither language tries. Your IDE might make suggestions for undeclared symbols used in your code.

The problem is further complicated in C++, because the standard says that any standard header can include any other standard header(s). It's therefore very easy to use a function or class without directly including the header which defines it, because your compiler happens to include the right header indirectly. The resulting code works in some implementations but not others, according to whether they share that header dependency.

It's not in general possible for a C++ IDE to tell whether a header dependency is "guaranteed", or just an incidental implementation detail that users shouldn't rely on. Obviously for standard libraries it could just know what's defined in what headers, but as soon as you get to third party libraries it gets quite uncertain.

I think most C++ programmers expect to have to look up what headers define what symbols. With Java, the one-public-class-per-file rule simplifies this considerably, and you just import the packages/classes you want. C++ doesn't have packages, and the only way for the IDE to find a class called my_namespace::something::MyClass is to search for it in every header file.

查看更多
Bombasti
7楼-- · 2019-04-29 09:36

Last I remember, Java also throws error if an Import statement is missed. It is the NetBeans GUI that makes your life easier.

Probably you should try finding an intelligent GUI for your C/C++ code.

查看更多
登录 后发表回答