赞
踩
% ls -s / 其参数列表包含三项: 第一项是程序名称ls,第二项和第三项分别是两个命令行参数,-s和/ |
#include int main (int argc, char* argv[]) { printf (“The name of this program is ‘%s’./n”, argv[0]); printf (“This program was invoked with %d arguments./n”, argc - 1); /* Were any command-line arguments specified? */ if (argc > 1) { /* Yes, print them. */ int i; printf (“The arguments are:/n”); for (i = 1; i < argc; ++i) printf ("%s/n", argv[i]); } return 0; } |
短格式 | 长格式 | 目的 |
-h | --help | 显示用法概要 |
-o filename | --output filename | 指定输出文件名 |
-v | --verbose | 打印详细信息 |
const struct option long_options[] = { { “help”, 0, NULL, ‘h’ }, { “output”, 1, NULL, ‘o’ }, { “verbose”, 0, NULL, ‘v’ }, { NULL, 0, NULL, 0 } }; |
#include #include #include /* The name of this program. */ const char* program_name; /* Prints usage information for this program to STREAM (typically stdout or stderr), and exit the program with EXIT_CODE. Does not return. */ void print_usage (FILE* stream, int exit_code) { fprintf (stream, “Usage: %s options [ inputfile ... ]/n”, program_name); fprintf (stream, “ -h --help Display this usage information./n” “ -o --output filename Write output to file./n” “ -v --verbose Print verbose messages./n”); exit (exit_code); } /* Main program entry point. ARGC contains number of argument list elements; ARGV is an array of pointers to them. */ int main (int argc, char* argv[]) { int next_option; /* A string listing valid short options letters. */ const char* const short_options = “ho:v”; /* An array describing valid long options. */ const struct option long_options[] = { { “help”, 0, NULL, ‘h’ }, { “output”, 1, NULL, ‘o’ }, { “verbose”, 0, NULL, ‘v’ }, { NULL, 0, NULL, 0 } /* Required at end of array. */ }; /* The name of the file to receive program output, or NULL for standard output. */ const char* output_filename = NULL; /* Whether to display verbose messages. */ int verbose = 0; /* Remember the name of the program, to incorporate in messages. The name is stored in argv[0]. */ program_name = argv[0]; do { next_option = getopt_long (argc, argv, short_options, long_options, NULL); switch (next_option) { case ‘h’: /* -h or --help */ /* User has requested usage information. Print it to standard output, and exit with exit code zero (normal termination). */ print_usage (stdout, 0); case ‘o’: /* -o or --output */ /* This option takes an argument, the name of the output file. */ output_filename = optarg; break; case ‘v’: /* -v or --verbose */ verbose = 1; break; case ‘?’: /* The user specified an invalid option. */ /* Print usage information to standard error, and exit with exit code one (indicating abnormal termination). */ print_usage (stderr, 1); case -1: /* Done with options. */ break; default: /* Something else: unexpected. */ abort (); } } while (next_option != -1); /* Done with options. OPTIND points to first nonoption argument. For demonstration purposes, print them if the verbose option was specified. */ if (verbose) { int i; for (i = optind; i < argc; ++i) printf (“Argument: %s/n”, argv[i]); } /* The main program goes here. */ return 0; } |
% program > output_file.txt 2>&1 % program 2>&1 | filter 2<&1表示文件描述符2也即stderr将合并到文件描述符1也即stdout中。 欲重定向stdout和stderr到一个文件,2>&1必须跟在一个文件后面。 欲重定向stdout和stderr到一个管道,2>&1必须在一个管道前面。 |
% ls / bin coda etc lib misc nfs proc sbin usr boot dev home lost+found mnt opt root tmp var % echo $? 0 % ls bogusfile ls: bogusfile: No such file or directory % echo $? 1 |
#include /* The ENVIRON variable contains the environment. */ extern char** environ; int main () { char** var; for (var = environ; *var != NULL; ++var) printf (“%s/n”, *var); return 0; } |
#include #include /* A handle for a temporary file created with write_temp_file. In this implementation, it’s just a file descriptor. */ typedef int temp_file_handle; /* Writes LENGTH bytes from BUFFER into a temporary file. The temporary file is immediately unlinked. Returns a handle to the temporary file. */ temp_file_handle write_temp_file (char* buffer, size_t length) { /* Create the filename and file. The XXXXXX will be replaced with characters that make the filename unique. */ char temp_filename[] = “/tmp/temp_file.XXXXXX”; int fd = mkstemp (temp_filename); /* Unlink the file immediately, so that it will be removed when the file descriptor is closed. */ unlink (temp_filename); /* Write the number of bytes to the file first. */ write (fd, &length, sizeof (length)); /* Now write the data itself. */ write (fd, buffer, length); /* Use the file descriptor as the handle for the temporary file. */ return fd; } /* Reads the contents of a temporary file TEMP_FILE created with write_temp_file. The return value is a newly allocated buffer of those contents, which the caller must deallocate with free. LENGTH is set to the size of the contents, in bytes. The temporary file is removed. */ char* read_temp_file (temp_file_handle temp_file, size_t* length) { char* buffer; /* The TEMP_FILE handle is a file descriptor to the temporary file. */ int fd = temp_file; /* Rewind to the beginning of the file. */ lseek (fd, 0, SEEK_SET); /* Read the size of the data in the temporary file. */ read (fd, length, sizeof (*length)); /* Allocate a buffer and read the data. */ buffer = (char*) malloc (*length); read (fd, buffer, *length); /* Close the file descriptor, which will cause the temporary file to go away. */ close (fd); return buffer; } |
% ar cr libtest.a test1.o test2.o |
% gcc -c -fPIC test1.c |
% gcc -shared -fPIC -o libtest.so test1.o test2.o |
% gcc -o app app.o -L. –ltest |
% gcc -static -o app app.o -L. –ltest |
Create test1.cvoid f1() { printf("Test 1 fucntion 1"); } gcc -c -fPIC test1.c -rw-rw-r-- 1 liuchao liuchao 1124 Jul 31 19:51 test1.o Create test2.cvoid f2() { printf("Test 2 function 2"); } gcc -c -fPIC test2.c -rw-rw-r-- 1 liuchao liuchao 1124 Jul 31 19:52 test2.o Create libstatictest.aar cr libstatictest.a test1.o test2.o -rw-rw-r-- 1 liuchao liuchao 2508 Jul 31 19:53 libstatictest.a Create libdynamictest.sogcc -shared -fPIC -o libdynamictest.so test1.o test2.o -rwxrwxr-x 1 liuchao liuchao 4573 Jul 31 19:54 libdynamictest.so Create app.cint main(int argc, char* argv[]) { f2(); } gcc -c app.c -rw-rw-r-- 1 liuchao liuchao 772 Jul 31 19:55 app.o Create staticappgcc -o staticapp app.o -L. –lstatictest -rwxrwxr-x 1 liuchao liuchao 4861 Jul 31 19:56 staticapp Create dynamicappgcc -o dynamicapp app.o -L. -ldynamictest Run staticapp./staticapp Test 2 function 2 Run dynamicapp./dynamicapp ./dynamicapp: error while loading shared libraries: libdynamictest.so: cannot open shared object file: No such file or directory Ldd staticappldd staticapp linux-gate.so.1 => (0x0060d000) libc.so.6 => /lib/libc.so.6 (0x007a3000) /lib/ld-linux.so.2 (0x00786000) Ldd dynamicappldd dynamicapp linux-gate.so.1 => (0x00861000) libdynamictest.so => not found libc.so.6 => /lib/libc.so.6 (0x00111000) /lib/ld-linux.so.2 (0x00786000) export LD_LIBRARY_PATH=.export LD_LIBRARY_PATH=. echo $LD_LIBRARY_PATH . ./dynamicapp Test 2 function 2 |
#include #include int main (int argc, char** argv) { TIFF* tiff; tiff = TIFFOpen (argv[1], “r”); TIFFClose (tiff); return 0; } |
% gcc -o tifftest tifftest.c –ltiff |
% ldd tifftest libtiff.so.3 => /usr/lib/libtiff.so.3 (0x4001d000) libc.so.6 => /lib/libc.so.6 (0x40060000) libjpeg.so.62 => /usr/lib/libjpeg.so.62 (0x40155000) libz.so.1 => /usr/lib/libz.so.1 (0x40174000) /lib/ld-linux.so.2 => /lib/ld-linux.so.2 (0x40000000) |
% gcc -static -o tifftest tifftest.c -ltiff /usr/bin/../lib/libtiff.a(tif_jpeg.o): In function ‘TIFFjpeg_error_exit’: tif_jpeg.o(.text+0x2a): undefined reference to ‘jpeg_abort’ /usr/bin/../lib/libtiff.a(tif_jpeg.o): In function ‘TIFFjpeg_create_compress’: tif_jpeg.o(.text+0x8d): undefined reference to ‘jpeg_std_error’ tif_jpeg.o(.text+0xcf): undefined reference to ‘jpeg_CreateCompress’ ... |
% gcc -static -o tifftest tifftest.c -ltiff -ljpeg –lz |
% gcc -static -o app app.o -lfoo -lbar –lfoo 例如程序app中的函数void a()在libfoo.a的A.o中实现,A.o中的函数void b()在libbar.a的B.o中实现,B.o中的函数void c()在libfoo.a的C.o中实现。则链接器的工作过程如下:
|
dlopen (“libtest.so”, RTLD_LAZY) |
void* handle = dlopen (“libtest.so”, RTLD_LAZY); void (*test)() = dlsym (handle, “my_function”); (*test)(); dlclose (handle); |
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。