开发者

How does C++ handle multiple source files?

I'm studying C++ right now, coming from a background in Python, and I'm having some trouble understanding how C++ handles multiple source files. In Python, the import statement first checks the current working directory for the module you're trying to import and then it checks the directories in sys.path. In C++, where would I place a custom made .h file? Where would the compiler even look?

For example, I've got a program, foo.exe compiled from a single source file, foo.cpp, both in the same dir开发者_Go百科ectory. I decide that I want to organize things a little better, so I create a new .h file, bar.h and dump stuff in there. Would I just need to #include to get to the stuff I put there? What if I want to use bar.h with another program (in a completely different directory)?


There are two include variants:

#include  "path-spec" 
#include  <path-spec> 

Quote notation:

This form instructs the preprocessor to look for include files in the same directory of the file that contains the #include statement, and then in the directories of any files that include (#include) that file.

The bracket notation looks for header files in certain defined locations.

With gcc you can get some information about these pathes via:

$ echo | gcc -v -x c++ -E -

Compilers accept

-I or /I

options to add additional pathes.


It (generally) looks in the include path if you use #include <foo>, else it uses relative paths if you use #include "../../foo/bar.h".

You set the include path with -I or /I on most compilers. Consult its manual for details.

Don't define any objects in headers though -- you will have multiple definition errors at link time if you do (and include the header in multiple source files).


It works in a similar way. The #include only is used by the compiler. In execution time the file bar.h doesn't gets used. But in compile time it is.
In compile time, the file could be in two places:
1.- The current directory (as in python)
2.- The directories configured in your include path. Where to configure that directories depends of the compiler you are using. Most of them let you define the include directories in the compile command line. And most IDEs let you configure it in some Options menu.

Hope it helps.


If your header files are in the same dir, you can include them like:

#include "bar.h"

If you want to include this header from another dir:

#include "../foobar/bar.h"

Basically quotations mean to search from current directory and brackets like in #include <abc.h> mean to search in standard header file directories. You can add custom directories to the standard search path by adding a -I /path/to/your/custom/headers in the compile command.


#include with angle brackets looks in the system include directories. Like this:
#include <iostream>

With double quotes it looks in the current directory and other directories given to the compiler to search.
#include "foo.h"
g++ -I../include foo.cpp


the thing you are missing in the python model is the linker.

in python you import code and its interpreted right there and then. in c/c++ you compile each source file into an object file. You then tell the linker to collect a bunch of object files into an executable

Typically the includes in c/c++ source files only contain descriptions of whast in the other C files (the names of functions, etc) not the function contents. This is enough for the compiler to compile a given file. Then the linker will combine your object files with libraries of 'well known' functions and make an executable

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜