From mboxrd@z Thu Jan 1 00:00:00 1970 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: fee84,ac7856c5338f6e5,start X-Google-Attributes: gidfee84,public From: Dave Butenhof Subject: Re: compile and link for multithread? Date: 1997/11/18 Message-ID: <3471C401.D344D79D@zko.dec.com> X-Deja-AN: 290589082 References: <3470C14D.AA9D0B34@unisys.com> Organization: Digital Equipment Corporation Newsgroups: comp.unix.osf.osf1 --------------2EE1E5A2F708F463E1742944 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Bob Blum wrote: > I'm trying to create a multithreaded executable on Dec Alpha with 4.0 > The executable is being linked with a shared library I also made. > > The shared library was made with > > cc -pthread -shared -lc -lm -lpthread -lpthreads If you're using the POSIX thread interface, then you should use: cc -o .so -pthread -shared -lm .c The "-pthread" takes care of the proper compile-time definitions and libraries. In particular, -pthread supplies "-lpthread", in the proper place in the list of libraries (right at the end, but before libc). Also, libpthreads.so (-lpthreads) is used only for "legacy" code (using the DCE thread [draft 4 posix] or CMA interfaces). If you are using legacy interfaces, build with cc -o .so -threads -shared -lm .c Also, when you link with cc, the driver automatically places -lc in the proper place, which is way at the end of the list of libraries. Including libraries in the wrong order can cause problems, although I'm not entirely certain that's your trouble. In cases where you can't (or don't want to) use -pthread or -threads, -pthread essentially supplies -D_REENTRANT for compilation, and "-lpthread -lmach -lexc" for linkage. Similarly, -threads supplies -D_REENTRANT and -D_PTHREAD_USE_D4 for compilation, and "-lpthreads -lpthread -lmach -lexc" for linkage. Note that these may change in the future! While programs compiled and linked properly will continue to work, programs that were linked with the wrong definitions, or with libraries missing or out of order may NOT continue to work, and you may also end up pulling in stuff that you no longer need, even after rebuilding on later versions where dependencies have been trimmed. (For example, prior to 4.0 you needed -lc_r, which is now identical to -lc, and is no longer needed.) > I'm making the executable with: > > cc -pthread -lc -lpthread Similarly, this should be just cc -pthread > When I try and run the executable it tells me > > executableFileName: /sbin/loader: Error: unresolved symbol in > /usr/shlib/libc.so __ldr_data > > other unresolved symbols are: _end, __Argc, and __Argv Most likely, this is due to your premature inclusion of libc, and will be solved by removing the unnecessary explicit -lc. (You need to specify that only when you link with ld instead of cc!) /---------------------------[ Dave Butenhof ]--------------------------\ | Digital Equipment Corporation butenhof@zko.dec.com | | 110 Spit Brook Rd ZKO2-3/Q18 http://members.aol.com/drbutenhof | | Nashua NH 03062-2698 http://www.awl.com/cseng/titles/0-201-63392-2/ | \-----------------[ Better Living Through Concurrency ]----------------/ --------------2EE1E5A2F708F463E1742944 Content-Type: text/html; charset=us-ascii Content-Transfer-Encoding: 7bit Bob Blum wrote:
I'm trying to create a multithreaded executable on Dec Alpha with 4.0
The executable is being linked with a shared library I also made.

The shared library was made with

    cc -pthread  -shared -lc -lm -lpthread -lpthreads

If you're using the POSIX thread interface, then you should use:
    cc -o <file>.so -pthread -shared -lm <file>.c
The "-pthread" takes care of the proper compile-time definitions and libraries. In particular, -pthread supplies "-lpthread", in the proper place in the list of libraries (right at the end, but before libc). Also, libpthreads.so (-lpthreads) is used only for "legacy" code (using the DCE thread [draft 4 posix] or CMA interfaces).

If you are using legacy interfaces, build with

    cc -o <file>.so -threads -shared -lm <file>.c
Also, when you link with cc, the driver automatically places -lc in the proper place, which is way at the end of the list of libraries. Including libraries in the wrong order can cause problems, although I'm not entirely certain that's your trouble.

In cases where you can't (or don't want to) use -pthread or -threads, -pthread essentially supplies -D_REENTRANT for compilation, and "-lpthread -lmach -lexc" for linkage. Similarly, -threads supplies -D_REENTRANT and -D_PTHREAD_USE_D4 for compilation, and "-lpthreads -lpthread -lmach -lexc" for linkage. Note that these may change in the future! While programs compiled and linked properly will continue to work, programs that were linked with the wrong definitions, or with libraries missing or out of order may NOT continue to work, and you may also end up pulling in stuff that you no longer need, even after rebuilding on later versions where dependencies have been trimmed. (For example, prior to 4.0 you needed -lc_r, which is now identical to -lc, and is no longer needed.)

I'm making the executable with:

    cc -pthread -lc -lpthread

Similarly, this should be just
    cc -pthread
When I try and run the executable it tells me

    executableFileName: /sbin/loader: Error: unresolved symbol in
/usr/shlib/libc.so __ldr_data

    other unresolved symbols are: _end, __Argc, and __Argv

Most likely, this is due to your premature inclusion of libc, and will be solved by removing the unnecessary explicit -lc. (You need to specify that only when you link with ld instead of cc!)
/---------------------------[ Dave Butenhof ]--------------------------\
| Digital Equipment Corporation                   butenhof@zko.dec.com |
| 110 Spit Brook Rd ZKO2-3/Q18       http://members.aol.com/drbutenhof |
| Nashua NH 03062-2698  http://www.awl.com/cseng/titles/0-201-63392-2/ |
\-----------------[ Better Living Through Concurrency ]----------------/
  --------------2EE1E5A2F708F463E1742944--