2007-12-26 12:33:49
The highlight of Beginning Linux Programming (Third Edition)
This is absolutely a good book regarding Linux Programming for beginner. I highlight key-sentences of this book below…
注:用50%的Gray表示原文,100%的Black表示我的问题
Chapter 1. Getting started
Linux applications are represented by two special types of files: executables and scripts.
Q.程序的执行路径问题
Since this is the first program we’ve run, it’s a good time to point something out. The hello program will probably be in your home directory. If PATH doesn’t include a reference to your home directory, the shell won’t be able to find hello. Furthermore, if one of the directories in PATH contains another program called hello, that program will be executed instead. This would also happen if such a directory is mentioned in PATH before your home directory. To get around this potential problem, we can prefix program names with ./ (e.g., ./hello). This specifically instructs the shell to execute the program in the current directory with the given name.
Q.在Windows下我喜欢用Source Insight作为查看代码的工具,在Linux下面我还没找到什么很好的工具,看来作者主要是用grep命令了,太强了
It’s often convenient to use the grep command to search header files for particular definitions and function prototypes.
Q.Library文件的处理,怎么指定库文件(full path or -l),怎么让编译器去指定路径搜索所需的库文件(-L)
The libraries usually exist in both static and shared formats, as a quick ls /usr/lib will show. We can instruct the compiler to search a library either by giving it the full path name or by using the -l flag.
The -lm (no space between the l and the m) is shorthand (shorthand is much valued in UNIX circles) for the library called libm.a in one of the standard library directories (in this case /usr/lib). An additional advantage of the -lm notation is that the compiler will automatically choose the shared library when it exists.
Although libraries are usually found in standard places in the same way as header files, we can add to the search directories by using the -L (uppercase letter) flag to the compiler. For example,
$ gcc -o x11fred -L/usr/openwin/lib x11fred.c -lX11
will compile and link a program called x11fred using the version of the library libX11 found in the /usr/openwin/lib directory.
The –L. option tells the compiler to look in the current directory (.) for libraries. The –lfoo option tells the compiler to use a library called libfoo.a (or a shared library, libfoo.so, if one is present).
Q.怎么产生静态库文件(Static Libraries)
We can create and maintain our own static libraries very easily by using the ar (for archive) program and compiling functions separately with gcc -c.
We do this by invoking the C compiler with the -c option, which prevents the compiler from trying to create a complete program. Trying to create a complete program would fail because we haven’t defined a function called main.
作者给了一个简单的例子(注:这个例子有个bug)
- fred.c
#include
void fred(int arg)
{
printf(“fred: you passed %d\n”, arg);
}
- bill.c
#include
void bill(char *arg)
{
printf(“bill: you passed %s\n”, arg);
}
$ gcc -c bill.c fred.c
$ ls *.o
bill.o fred.o
bill.c and fred.c是库文件的源代码,它们都没有main函数的,但是编译不会出错因为我们用了-c option
- lib.h
/*
This is lib.h.
*/
void bill(char *);
void fred(int);
- program.c
#include “lib.h”
int main()
{
bill(“Hello World”);
exit(0);
}
主程序,调用库文件暴露出来的函数
$ cgcc -c program.c $ cgcc -o program program.o bill.o $ ./program bill: we passed Hello World // YM: a minor bug here. It should output ‘bill: you passed Hello World’
Now let’s create and use a library. We use the ar program to create the archive and add our object files to it. The program is called ar because it creates archives, or collections, of individual files placed together in one large file. Note that we can also use ar to create archives of files of any type. (Like many UNIX utilities, ar is a generic tool.)
$ ar crv libfoo.a bill.o fred.o // generate a static library named libfoo.a
a - bill.o
a - fred.o
The library is created and the two object files added. To use the library successfully, some systems, notably those derived from Berkeley UNIX, require that a table of contents be created for the library. We do this with the ranlib command. In Linux, this step isn’t necessary (but it is harmless) when we’re using the GNU software development tools.
$ ranlib libfoo.a
Q.静态库文件的缺点
One disadvantage of static libraries is that when we run many applications at the same time and they all use functions from the same library, we may end up with many copies of the same functions in memory and indeed many copies in the program files themselves. This can consume a large amount of valuable memory and disk space.
Q.怎么看一个程序和哪些动态库相关
We can see which shared libraries are required by a program by running the utility ldd. For example, if we try running it on our example application, we get the following:
$ ldd program libc.so.6 => /lib/libc.so.6 (0x4002a000)
/lib/ld-linux.so.2 => /lib/ld-linux.so.2 (0x40000000)
Chapter 4 The Linux Environment
Q. 在Linux下编程需要了解的一些基本环境相关的概念:
给程序传参数(Passing arguments to programs)
环境变量(Environment variables)
系统时间(Finding out what the time is)
临时文件(Temporary files)
得到用户和主机的信息(Getting information about the user and the host computer)
Log信息(Causing and configuring log messages)
系统限制(Discovering the limits imposed by the system)
Q. 这章所涉及的Linux API
1. Programs arguments
#include
int getopt(int argc, char *const argv[], const char *optstring);
extern char *optarg;
extern int optind, opterr, optopt;
2. Environment variables
#include
char *getenv(const char *name);
int putenv(const char *string);
extern char **environ;
3. Time and Date
#include
time_t time(time_t *tloc);
double difftime(time_t time1, time_t time2);
struct tm *gmtime(const time_t timeval);
struct tm *localtime(const time_t *timeval);
time_t mktime(struct tm *timeptr);
char *asctime(const struct tm *timeptr);
char *ctime(const time_t *timeval);
size_t strftime(char *s, size_t maxsize, const char *format, struct tm *timeptr);
char *strptime(const char *buf, const char *format, struct tm *timeptr);
4. Temporary files
#include
char *tmpnam(char *s);
FILE *tmpfile(void);
char *mktemp(char *template);
int mkstemp(char *template);
5. User Information
#include
#include
#include
uid_t getuid(void);
char *getlogin(void);
struct passwd *getpwuid(uid_t uid);
struct passwd *getpwnam(const char *name);
void endpwent(void);
struct passwd *getpwent(void);
void setpwent(void);
uid_t geteuid(void);
gid_t getgid(void);
gid_t getegid(void);
int setuid(uid_t uid);
int setgid(gid_t gid);
6. Host Information
#include
#include
int gethostname(char *name, size_t namelen);
int uname(struct utsname *name);
long gethostid(void);
6.Logging
#include
#include
#include
void syslog(int priority, const char *message, arguments...);
void closelog(void);
void openlog(const char *ident, int logopt, int facility);
int setlogmask(int maskpri);
pid_t getpid(void);
pid_t getppid(void);
7. Resources and limits
#include
int getpriority(int which, id_t who);
int setpriority(int which, id_t who, int priority);
int getrlimit(int resource, struct rlimit *r_limit);
int setrlimit(int resource, const struct rlimit *r_limit);
int getrusage(int who, struct rusage *r_usage);
Limit Constant What They’re For
NAME_MAX The maximum number of characters in a filename
CHAR_BIT The number of bits in a char value
CHAR_MAX The maximum char value
INT_MAX The maximum int value
Resource Parameter Description
RLIMIT_CORE The core dump file size limit, in bytes
RLIMIT_CPU The CPU time limit, in seconds
RLIMIT_DATA The data () segment limit, in bytes
RLIMIT_FSIZE The file size limit, in bytes
RLIMIT_NOFILE The limit on the number of open files
RLIMIT_STACK The limit on stack size, in bytes
RLIMIT_AS The limit on address space (stack and data), in bytes
No comments:
Post a Comment