Quote:
Originally Posted by
yifangt
Thanks!
Your replies clarified lots of my confusion to use third party library.
I think I understand the -IXYZ is for the compiling step.
Yes, -I is for compilation step. It controls where
#include <something.h> statements look. By default gcc/g++ look in /usr/include/something.h. If you have a header file installed into /usr/local/include you must use, you can do -I /usr/local/include
-I has no effect on
#include "local.h" statements, which look in the source file's directory.
Quote:
#include <header.h> for the compiler, and -lsomelibraryname for the linker, are really all that's required in the vast majority of cases.
Do you mean only the header files (*.h) is needed and be included in my code?
That's
always true, really. Mark it down as a rule:
The compiler needs only the header files.
And the reason that's true cuts to the heart of what compilation and linking are.
- Compilation: This turns your code and only your code into assembly language. Any functions (even your own), any global variables, any libraries you use, are all left as generic stubs to fill in later.
- Linking: This loads all those little assembly programs at the same time, builds a table of their names, and converts all those generic stubs into locations in that table. With everything hardwired into actual memory addresses, it can create an executable that will run.
"All those little assembly programs" include not just your code, but also the generic code necessary to initialize any C program, default libraries like libc, and anything you specify with -l.
Quote:
In other words, is the header (*.h) file along with the .so Yes
.
P.S. .a files are archive files - they literally contain the big pile of .o files needed to make a .so file. Linking to zlib.a is like dumping all of zlib's .c files in your source code. If you link to the .a file you don't need the .so. This will make your executable a lot bigger though.