Multiple definition of 'err _ sys & #39;, errsys

Source: Internet
Author: User

Multiple definition of 'err _ sys ', errsys

Address: http://www.cnblogs.com/yhLinux/p/4079930.html

 

Problem description:

[Click here to view the solution]

The codelist8-7.c uses the functions TELL_WAIT (), WAIT_PARENT (), and TELL_CHILD () in the codelist15-3.c when practicing the list of APUE programs in UNIX environment advanced programming ().

Codelist8-7.c:

1 # include "apue. h "2 3 static void charatatime (char *); 4 5 int main (void) 6 {7 pid_t pid; 8 TELL_WAIT (); 9 10 if (pid = fork () <0) {11 err_sys ("fork error"); 12} else if (pid = 0) {13 WAIT_PARENT (); /* parent goes first */14 charatatime ("output from child \ n"); 15} else {16 charatatime ("output from parent \ n "); 17 TELL_CHILD (pid); 18} 19 exit (0); 20} 21 22 static void charatatime (char * str) 23 {24 Char * ptr; 25 int c; 26 27 setbuf (stdout, NULL);/* set unbuffered */28 for (ptr = str; (c = * ptr ++ )! = 0;) 29 putc (c, stdout); 30}Codelist8-7.c

Codelist15-3.c:

1 # include "apue. h "2 3 static int pfd1 [2], pfd2 [2]; 4 5 void TELL_WAIT (void) 6 {7 if (pipe (pfd1) <0 | pipe (pfd2) <0) 8 err_sys ("pipe error"); 9} 10 11 void TELL_PARENT (pid_t pid) 12 {13 if (write (pfd2 [1], "c ", 1 )! = 1) 14 err_sys ("write error"); 15} 16 17 void WAIT_PARENT (void) 18 {19 char c; 20 21 if (read (pfd1 [0], & c, 1 )! = 1) 22 err_sys ("read error"); 23 24 if (c! = 'P') 25 err_quit ("WAIT_PARENT: incorrect data"); 26} 27 28 void TELL_CHILD (pid_t pid) 29 {30 if (write (pfd1 [1], "p", 1 )! = 1) 31 err_sys ("write error"); 32} 33 34 void WAIT_CHILD (void) 35 {36 char c; 37 38 if (read (pfd2 [0], & c, 1 )! = 1) 39 err_sys ("read error"); 40 41 if (c! = 'C') 42 err_quit ("WAIT_CHILD: incorrect data"); 43}View Code

When compiling 8-7 with the command, the following error is prompted:

$Gcc Codelist8-7.c codelist15-3.c-o 8-7/Tmp/ccMDAwpv. o: In function 'err _ ret ': codelist15-3.c :(. text + 0x0 ):Multiple definitionOf 'err _ ret '/tmp/ccXi2EPL. o: codelist8-7.c :(. text + 0x0): first defined here/tmp/ccMDAwpv. o: In function 'err _ sys ': codelist15-3.c :(. text + 0xa9): multiple definition of 'err _ sys '/tmp/ccXi2EPL. o: codelist8-7.c :(. text + 0xa9): first defined here/tmp/ccMDAwpv. o: In function 'err _ exit ': codelist15-3.c :(. text + 0x15a): multiple definition of 'err _ exit '/tmp/ccXi2EPL. o: codelist8-7.c :(. text + 0x15a): first defined here/tmp/ccMDAwpv. o: In function 'err _ dump ': codelist15-3.c :(. text + 0x209): multiple definition of 'err _ dump'/tmp/ccXi2EPL. o: codelist8-7.c :(. text + 0x209): first defined here/tmp/ccMDAwpv. o: In function 'err _ msg ': codelist15-3.c :(. text + 0x2b5): multiple definition of 'err _ msg '/tmp/ccXi2EPL. o: codelist8-7.c :(. text + 0x2b5): first defined here/tmp/ccMDAwpv. o: In function 'err _ quit ': codelist15-3.c :(. text + 0x360): multiple definition of 'err _ quit '/tmp/ccXi2EPL. o: codelist8-7.c :(. text + 0x360): first defined herecollect2: ld returns 1

Find the following online comments:

1. http://bbs.chinaunix.net/thread-3699788-1-1.html

I think it's because I am in apue. # include "error. c ", although apue. in h # ifndef _ APUE_H _ # define _ APUE_H _ copy the code, but the CompilerSeparate CompilationSo # include "apue. h" in the wait. c and 14.6.c files will contain two error. c files,
In the error. c file, it is the definition of the function (not Declaration. So I deleted it in apue. # include "error. c ", makefile file: inc =/home/lee/program/apue/apue.2e/include/error =/home/lee/program/apue/apue.2e/include/error. c. out: 14.6.c wait. c gcc-I $ (inc)-o. out 14.6.c wait. c $ (error) copy the code apue. the hfile is in the/home/lee/program/apue/apue.2d/include/directory. So there is no problem. I wonder if this is what I think.

# Yes, andDo not include c files if there is no sufficient reason

The above discussion has been well discussed. It turns out that I used to follow the method in this article [http://blog.csdn.net/quan9ing007/article/details/9929659this is not a problem.Apue. hAndError. hCopy/Usr/includeFolder.

In fact, according to the above statement, we should not put # include "error. c" in apue. h, andError. cPut in/Usr/includeDirectory, add error. c during each compilation.

 

Solution (recommended):

ThereforeApue. hPut it in the/usr/include directory, andError. c is often used., We will defineError environment variableIn this way, you do not have to copy error. c to the relevant folder every time for compilation.

Assume that the current user is Lee and error. c is stored in/home/Lee/code_Lee/APUE/part_of_source /:

Sudo cp/home/Lee/code_Lee/APUE/part_of_source/apue. h/usr/include/apue. hsudo chmod a + r/usr/include/apue. hvi/home/Lee /. bashrc in. added the apue_error variable at the end of bashrc: apue_error =/home/Lee/code_Lee/APUE/part_of_source/error. csource ~ /. Bashrc/* this is very important. Be sure to execute */echo$ {Apue_error}/Home/Lee/code_Lee/APUE/part_of_source/error. cgcc codelist8-6.c $ {apue_error}-o 8-6 success! Gcc codelist8-7.c codelist15-3.c $ {apue_error}-o 8-7 successful !!

 

(End)

References:

1. Linux environment variables

Http://www.cnblogs.com/Neddy/archive/2011/03/01/1968018.html

2. linux environment variables ()

Http://www.cnblogs.com/growup/archive/2011/07/02/2096142.html


Makefile Compilation: multiple definition

There is a RMB _chk_str in RMB. o that is repeated with the definition in your testRMB. o. Check which definition is wrong.

The strange thing in makefile is: $ CC $ (LIBFLAGS) $? -O $ @ $ (LIBS1), change it to the following:
$ CC $? -O $ @ $ (LIBFLAGS) $ (LIBS1)

Multiple definition of 'main' in C Language'

Are you defining the main function in other files of the same project? If yes, It must be wrong;

A project can only have one main function. You can change the name of the main function in other files.

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.