The C language header file and source file are not in the same directory

Source: Internet
Author: User

Http://www.cnblogs.com/ShaneZhang/archive/2013/05/20/3088688.html

Engineering, the file name of the header should be the same as the corresponding source file name for maintenance, if the header file contains multiple source files in the definition or declaration, you should group the code in the layout header file by source file, and note that each group is located in the source file. When a project file more should be the source files and header files separate directory, the general header files are stored in the Include or Inc directory, and the source files are stored in the source or src directory, according to experience, a project of more than 30 files should be stored separately from the source file and the header file, When files are small, drop them directly into the same directory.

In fact, the C + + standard does not determine the order in which these two methods search for file filepath, but is determined by the compiler implementation, the difference is that if the compiler in the order of the second form of the definition of search for files filepath failure or not support this way, replace it with the first order and then search.

In fact, in general the first way is to search the compiler's system directory first, and the second way is to compile the header file in the same directory as the current directory for the search, if the search failed and then in the system header file search. The two approaches are essentially no different, but when our own program files have the same name as the system header file, the latter will first search our header file instead of the system. However, with the system header file name is not a good habit, accidentally can bring unnecessary trouble, when we write our own library, it is best to put it into a directory, do not add this directory directly to the compiler's header file search path (such as the GCC-I, Visual C + + I option, etc. , in fact, the compiler in the Unix/linux platform is generally the-I option, but rather to add to the previous level of the directory, and in our source file refers to the header file contains the directory name, which is not prone to conflict.

For example, we created a library called Mylib, where a header file is strutil.hpp, we can create a/home/user/project/src/mylib directory, then put STRUTIL.HPP in, and then put/home/user /project/src added to the compilation options:

Gcc-i/home/user/project/src

In this way, the strutil.hpp file can be referenced in our source program:

#include "mylib/strutil.hpp"

Referencing header files by the displayed directory name is not prone to conflict, and it is not easy to confuse our own header files with the system header files.

Of course, from the code logic we have another solution to the conflict, that is, the namespace, see section [?] Section.

Http://blog.sina.com.cn/s/blog_6e0693f70100so42.html

The C language header file and source file are not in the same directory

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.