Can someone tell me how linking works?

Basically what I'm asking is, for Mac, linux, and Windows, how are program and library files stored?

For example, I know that library files, when compiled on windows should look like lib.a. If I to link to it with GCC from anywhere the file should it be stored in C:\Windows\system32?

and, with GCC at least if I want to link to it (and its in that global place) I just do g++ myprogram.cpp -o test -llib whereas anywhere else its g++ myprogram.cpp -o test -l/path/to/my/lib using lib.a assuming I have the proper header file included.

What is the difference between a shared and unshared library? When would I want one or the other?

I believe on both mac and linux header files have to be stored in /usr/bin/include so you can do #include <my_header.h> . And that this tells the compiler to look in a default folder ("global"?) Is there a similar place on Windows?

Also, I'm confused on how in both systems where to store a program if I want to run it anywhere from the command line. Sometimes if I write code in a linux environment (usually javascript or php) and there is a problem with a line number, I would like to do myprogram -e 65 debug.php (echo line 65 of debug.php) without having to open the file. I have no idea where I would want to store this executable. On any OS.
I think Shared Libraries == Dynamic Link Libraries (DLLs) and Unshared Libraries ==Statically Linked Libraries (.lib)
You use statically linked when you want the library code to be included with your program executable, and you use shared libraries when you want/need it to be an external file. You would have to use the external fil option if you are only given the external file and the header with no source code or a static library to link to, or f the license requires it, or if it needs to be updated independently of the program, etc. or you can also use external files as a sort of plugin system.

For access on the command line, it must be in any folder in your system's PATH environment variable. (For Windows, I don't know for other OSes)

For including in C++, #include <file> says "look in the global-to-all-programs folder", and #include "file" says "look in the working directory". The "global-to-all-programs" directory depends entirely on your compiler, not your OS.
Last edited on
Thanks for the info. That cleared up a lot.
#include <file> actually says look for the file in the folders on the include path (which will usually include the global folder) and #include "file" means look in the working directory first and, if it's not there, then look in the folders on the include path.

While it's bad form, #include <string> will find the std::string header in its usual place, unless you've provided your own, private version.

The include path on my Windows PC is:

INCLUDE=c:\Program Files\Microsoft Visual Studio 9.0\VC\ATLMFC\INCLUDE;c:\Progra
m Files\Microsoft Visual Studio 9.0\VC\INCLUDE;C:\Program Files\Microsoft SDKs\W

There is a similar search path for libraries. For VC++ it's LIBPATH

I thing GCC uses C_INCLUDE_PATH and CPP_INCLUDE_PATH for headers, and LD_LIBRARY_PATH or libraries?

An example Linux include path, nicked from the Internet, is:



Last edited on
Topic archived. No new replies allowed.