已弃坑 待更
gcc
Gcc can compile C,C++(maybe g++),and objective-C. It is both a compiler and linker.
一步到位:gcc main.c module1.c module2.c -o program
分开:gcc -c main.c
gcc -c module1.c
gcc -c module2.c
gcc main.o module1.o module2.o -o program
command-line options
-c files : Direct gcc to compile the source files into an object files without going through the linking stage. Makefiles (below) use this option to compile files one at a time.
-o file : Specifies that gcc’s output should be named file. If this option is not specified, then the default name used depends on the context…(a) if compiling a source .c file, the output object file will be named with the same name but with a .o extension. Alternately, (b) if linking to create an executable, the output file will be named a.out. Most often, the -o option is used to specify the output filename when linking an executable, while for compiling, people just let the default .c/.o naming take over.It’s a memorable error if your -o option gets switched around in the command line so it accidentally comes before a source file like “…-o foo.c program” – this can overwrite your source file – bye bye source file!
-g : Directs the compiler to include extra debugging information in its output. We recommend that you always compile your source with this option set, since we encourage you to gain proficiency using the debugger such as gdb (below).
-Wall : Give warnings about possible errors in the source code.
-Idir : Adds the directory dir to the list of directories searched for #include files.
-lmylib : (lower case ‘L’) Search the library named mylib for unresolved symbols (functions, global variables) when linking. The actual name of the file will be libmylib.a, and must be found in either the default locations for libraries or in a directory added with the -L flag (below).
-Ldir
make
The “make” utility automates the process of compiling and linking.
run make with no arguments in the project directory.and it will search for a file called Makefile
or makefile
for its build instructions.
Makefiles
variable: for example CC=gcc
standard variable names:
CC: The name of the C compiler, this will default to cc or gcc in most versions of make.
CFLAGS: A list of options to pass on to the C compiler for all of your source files. This is commonly used to set the include path to include non- standard directories (-I) or build debugging versions (-g).
LDFLAGS: A list of options to pass on to the linker. This is most commonly used to include application specific library files (-l) and set the library search path (-L).
To refer to the value of a variable, put a dollar sign ($) followed by the name in parenthesis or curly braces…
1 | CFLAGS = -g -I/usr/class/cs107/include |
The second major component of a makefile is the dependency/build rule.
A rule generally consists of two lines: a dependency line followed by a command line.
1 | binky.o : binky.c binky.h akbar.h |
Unix Shell
Directory Commands
cd directory: change directory
pwd : print working(current) directory
rm file: delete a file
mv old new : Rename a file(also works for moving things between directories).If exists, overwritten.
mkdir name : Create a directory
rmdir name : Delete a directory(must be empty)
Shorthand Notations & Wildcards
. : Current Directory
.. : Parent Directory
~ : Your home directory
~/cs107 : The cs107 directory in your home directory
~user : Home directory of user
通配符 : Any number of characters(not ‘.’)
? : Any single character(not ‘.’)
Miscellaneous Commands
cat file : Print the contents of file to standard output
more file : Same as cat,but only a page at a time.
less file : Same as more,but with navigability.
w : Find out who is on the system and what they are doing.
ps : List all your processed(use the process id’s in kill below)
jobs : Show jobs that have been suspended(use with fg)
program& : Runs program in the background.
ctrl-z : Suspend the current program.
% : Continue last job suspended, or use fg(foreground)
%number : Continue a particular job(the number comes from the jobs list)
kill process-id : Kill a process
kill -9 process : Kill a process with extreme prejudice
grep exp files : Search for an expression in a set of files.
wc files : Count words,lines and characters in a file
script : Start saving everything that happens in a file. Type exit when done
lpr file : Print file to the default printer
lpr -Pinky file : Print file to the printer named inky
diff file1 file2 Show the differences between two files.
telnet hostname : Log on to another machine
source file : Execute the lines in the given file as if they were typed to the shell
Getting Help
man subject : Read the manual entry on a particular subject
man -k keyword : Show all the manual pages for a particular keyword
History
history : Show the most recent commands executed
!! : Re-execute the last command (or type up-arrow with modern shells)
!number : Re-execute a particular command by number
!string : Re-execute the last command beginning with string
^wrong^right^ : Re-execute the last command, substituting right for wrong
ctrl-P : Scroll backwards through previous commands
Pipes
a > b : Redirect a’s standard output to overwrite file b
a >> b : Redirect a’s standard output to append to the file b
a >& b : Redirect a’s error output to overwrite file b
a < b : Redirect a’s standard input to read from the file b
a | b : Redirect a’s standard output to b’s standard input