![]() |
3 years ago | |
---|---|---|
.. | ||
Makefile | 4 years ago | |
README.md | 3 years ago | |
erf.c | 5 years ago |
README.md
Compilation
Things you should know
- There is an implicit -lc passed to the compiler for the linker to link the standard libc.
- Adding the -g flag adds symbols for debugging. Without it gdb can't give you variable or function names.
- There is some overhead in size of the execuable when using this command but it is worth the ability debug
- Add the standard
-std=gnu11
you are targeting to the compile command. -Wall
adds compiler warnings.Werror
will actually treat warnings as errors preventing compilation. Trust the compiler. If an error or warning occurs fix it.
Paths
The linker will normally just look for the header and object files in the file system hierarchy's usual places.
In the erf.c
program we can explicitly pass the math and the libc libraries to the linker like so:
gcc erf.c -o erf lm -g -Wall -O3
If we have a library in a non-standard location you have to tell the compiler where the header files are and you have to tell linker where it is located.
gcc -I/usr/local/include use_useful.c -o use_useful -L/usr/local/lib -luseful
-I
Adds the given path to the include search path that the compiler searches for header files.-L
Adds the library (object code) to the Linker's search path.-l
Says to link theuseful
library
Note: I guess mac doesn't have ldd
it looks like otool -L <binary>
can show the same thing.
Order matters in dependencies
If you have specific.o
depend on lbroad
which depends on lgeneral
then you need:
gcc specific.o -lbroad -lgeneral
Any other order will probably fail.
Hunting for libraries
pkg-config
addresses many problems by creating a repository of flags and locations that packages self-report
as being necessary for compilation.
pkg-config --libs gsl libxml-2.0
pkg-config --cflags gsl libxml-2.0
> -lgsl -lgslcblas -lm -lxml2
> -I/usr/include/libxml2
If these libraries are installed it will produce exactly the flags needed to compile using GSL and LibXML2. This
example command also demonstrates how GSL is dependent on the GNU Basic Linear Algebra Subprograms which in turn
is dependent on the math.c library (-lm
).
Since we don't see a -L
we know that all the libraries are located in the typical lib
locations, but the -I
tells use that the header files for libxml2
are in a different location.
When you want to use pkg-config
to evaluate the at the shell we can use backticks or $()
(command substituation).
The $(...)
is the more common (modern) syntax, but both are valid.
gcc `pkg-config --cflags --libs gsl libxml2` -o specific specific.c
This first evaluates the backticks which is then used as input to the command producing:
gcc -I/usr/include/libxml2 -lgsl -lgslcblas -lm -lxml2 -o specific specific.c
As you can probably tell the more libraries your program begins using, the more locations we need to include/link to.
Path Environment Variables
You can also set the path for the C compiler and Linker with environmental variables. They tend to be different on each OS,
so you need to read the man
for your compiler.
man gcc
- Search for pattern:
/ENVIRONEMT