本文介绍Linux环境变量, C编译器, 头文件以及库的使用
Linux程序可以分为两类: executables 和Scripts. Executables程序可以直接运行,类似MS Windows的.exe文件,Scripts即脚本(Shell Script),包含执行其他程序的指令,类似MS Windows的.bat, .cmd
1. PATH环境变量Linux存放程序的几个标准目录/bin: Binaries, programs used in booting the system/usr/bin:User Binaries, standard programs available to users/usr/local/bin: Local binaries, programs specific to aninstallation Root用户的PATH可能还会指向两个存放系统管理程序的目录/sbin/usr/sbin 其他可选的OS组件或者第三方程序安装在/opt目录下/opt
2. C编译器cc, c89, gcc
2.1 Hello World in C <!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->1 #include <stdio.h>
2 int main()
3 {
4 printf(“Hello World
”);
5 exit(0);
6 }
2.2 compile and run$ gcc -o hello hello.c
$ ./hello
Hello World
$
3. 开发环境/usr/bin 系统程序
/usr/local/bin or /opt 自己安装的程序
GCC compiler 通常在 /usr/bin or /usr/local/bin,
3.1头文件/usr/include及其子目录
/usr/include/X11
/usr/include/g++ 如果头文件不在标准目录中,需要在编译时指明其目录
$ gcc -I/usr/openwin/include fred.c Tips:如何查找你要的头文件
$grep EXIT_ *.h
...
stdlib.h:#define EXIT_FAILURE 1 /* Failing exit status. */
stdlib.h:#define EXIT_SUCCESS 0 /* Successful exit status. */
3.2 库文件/lib
/usr/lib 库文件命名规则:
- lib开头
- x 库类型(比如c表示C库, m表示数学库)
- 文件类型(.a, .so) 静态库,共享库
完整路径名查找库
$ gcc -o fred fred.c /usr/lib/libm.a 只用库名查找库, 用-l选项指定库类型
$gcc -o fred fred.c –lm 指定非标准库路径,用-L选项指定路径(大写),
$ gcc -o x11fred -L/usr/openwin/lib x11fred.c -lX11