"UNIX Environment Advanced Programming"--the example compiles, runs __unix in the book

Source: Internet
Author: User

People who want to seriously study UNIX environment advanced programming may want to knock the example in the book and run a look at the effect. But for the novice, there may be some difficulty, because the example used in the header file and some functions do not know where, the compilation may also be problematic. This article here is an example of how to run the examples in this book.

First, you need the header file Apue.h, the header file in another blog post: http://blog.csdn.net/wangyuling1234567890/article/details/20491159

But that's not going to work, if direct compilation would be like a hint to find the definition of the err_*** function, because the error handler used in the example has a declaration in apue.h but is defined in another file.

Please refer to myerr.h:http://blog.csdn.net/wangyuling1234567890/article/details/20492319

Apue.h and Myerr.h These two files are ready, then you can hit the code, compiled execution.

As the first example in the book:

Myls.c

#include "apue.h"
"myerr.h"          //This header file needs to be added to itself
#include <dirent.h>

int main (int argc, char * argv[])
{
	DIR * DP;
	struct dirent *dirp;
	
	if (2!= argc)
	{
		err_quit ("Usage:ls directory_name");
	if (NULL = = (DP = Opendir (argv[1)))
	{
		Err_sys ("can ' t open%s", argv[1]);
	}
	
	while (NULL!= (DIRP = Readdir (DP)))
	{
		printf ("%s\n", dirp->d_name);
	}
	
	Close (DP);
	Exit (0);
}

CC MYLS.C

This can be compiled, but every time you write an instance of apue.h and myerr.h two files to the current directory, this is a bad habit, and unreasonable.

One solution is to put the two public files in a directory (such as: ...). /include), compile the time to specify the header file directory on it.

Cc-i.. /include MYLS.C

File Structure:

Linux:/mnt/hgfs/vmware-share/unix # Tree-a
.
|--Chapter-1
| |--Makefile
| '--MYLS.C
'--Include
|--Apue.h
'--Myerr.h

But then there is a new problem, that is, each compilation requires the keyboard to knock the letters become more, delay time. In particular, when the compilation options are more, the file is also relatively long, this will become almost impossible. At this time, we will naturally think of makefile. Here is the makefile for this example:

CC = gcc
ROOT: = $ (shell pwd)/..
INCLUDE: = $ (ROOT)/include

cflags + + +-i$ (include)

Default:my_ls

my_ls:myls.o
	$ (CC)-O My_ls myls.o
	
myls.o:myls.c
	$ (CC) $ (cflags)-C myls.c Clean 
	
:
	-rm *.o My_ls

This makes it much easier to compile and make directly.

Results:

ll
drwxrwxrwx  1 root root 4096 2014-04-02 22:56.
drwxrwxrwx  1 root root    0 2014-04-02 22:18.
-rwxrwxrwx  1 root  2014-04-02 22:24 makefile
-rwxrwxrwx  1 root root 8892 2014-04-02 22:56-my_ls< c11/>-rwxrwxrwx  1 root  402 2014-04-02 22:07 myls.c
-rwxrwxrwx  1 root root 2132 2014-04-02 22:56 MYLS.O
linux:/mnt/hgfs/vmware-share/unix/chapter-1 # /my_ls
usage:ls directory_name
. /my_ls/mnt/hgfs/vmware-share/unix/
...
Chapter-1
include
.../my_ls/mnt/hgfs/vmware-share/unix/chapter-1/
...
Makefile
myls.c
myls.o
My_ls
linux:/mnt/hgfs/vmware-share/unix/chapter-1 #


For more detailed information about makefile please refer to: http://blog.csdn.net/wangyuling1234567890/article/category/2163845


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.