Last active
March 15, 2021 09:52
-
-
Save elowy01/ad324aaf4a15c1095b5b371988e85293 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Include path: | |
The list of directories for header files is often referred to as the include path, a | |
# Library path | |
List of directories for libraries as the library search path or link path. | |
# Modifying these search paths when compiling: | |
# Where 'gdbm' library is within /opt/gdbm-1.8.3/lib | |
$ gcc -Wall -I/opt/gdbm-1.8.3/include -L/opt/gdbm-1.8.3/lib dbmain.c -lgdbm | |
#Environment variables to control the Include: | |
C_INCLUDE_PATH (for c) and CPLUS_INCLUDE_PATH (for cpp) | |
and the and Library path: | |
LIBRARY_PATH | |
and in order to control the search path for Shared library: | |
LD_LIBRARY_PATH | |
# Read excellent tutorial at: https://www3.ntu.edu.sg/home/ehchua/programming/cpp/gcc_make.html | |
#Useful stuff extracted from this tutorial: | |
#Knowing the default include-paths in your system used by the "GNU C Preprocessor": | |
$ cpp -v | |
#Run the compilation in verbose mode (-v) to study the library-paths (-L) and libraries (-l) used in your system: | |
gcc -v -o hello.o hello.c | |
# 'nm' command to List Symbol Table of Object Files. | |
A 'T' in the second column indicates a function that is defined, while a 'U' | |
indicates a function which is undefined and should be resolved by the linker. | |
# 'ldd' utility examines an executable and displays a list of the shared libraries that it needs. | |
$ ldd hello.o | |
#Another more thorough tutorial at: | |
https://www.linuxtopia.org/online_books/an_introduction_to_gcc/index.html | |
# | |
gcc -I /path/to/header_files1/ -I /path/to/header_files2/ main.c // here we add | |
\several folders with headers | |
###### Example of .c hello_world code : | |
#include <stdio.h> | |
int | |
main (void) | |
{ | |
printf ("Hello, world!\n"); | |
return 0; | |
} | |
Now we can compile (advisable activate all warnings:) | |
$ gcc -Wall hello.c -o hello | |
#### | |
# Compiling multiple source files including the difference between using | |
# #include <hello.h> or using #include "hello.h" | |
# Read document at: https://www.linuxtopia.org/online_books/an_introduction_to_gcc/gccintro_11.html | |
#### | |
# Linking a static library (in this case the Math library): | |
#include <math.h> | |
#include <stdio.h> | |
int | |
main (void) | |
{ | |
double x = sqrt (2.0); | |
printf ("The square root of 2.0 is %f\n", x); | |
return 0; | |
} | |
# Now, we can link by using: | |
gcc -Wall -lm calc.c -o calc # where -lm is used to specify the name of the library | |
###############] | |
# How can we list the contents of an *.a file (statis library) | |
ar t libhts.a | |
############### | |
# Debugging: | |
# If you want to compile a program with possibility of debugging it: | |
gcc -g main.c -o main | |
# Now, we can run the gdb debugger by doing: | |
gdb ./main | |
# We can create a Breakpoint by doing: | |
break main // where main is the name of your function | |
# And now if we enter: | |
run // it will go to the first line in function 'main' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment