Friday, November 21, 2008

Prepare to run a "p-threaded" c project

here is a good quick reference to learning pthreads, but normal building and running won't work as the gcc must be explicitly directed to load the runtime library pthread upon running, for the line "#include " to work. How will we do it?

  1. From command line:
    Instead of normal compiling way:
    gcc sourc_file.c

    , you are supposed to use:
    gcc -lpthread sourc_file.c

    directing the linker to link the runtime library "pthread".


  2. Using eclipse IDE:
    It is fairly easy too, we will do the same something but from configuration of this project, steps:

    • Create a new project copy-paste any pthread-using code, (for example: use the above link)
    • Right click your project and select properties (alternatively hit alt+enter)
    • Expand C/C++, and click Settings, you should see something like:
    • Expand the GCC linker and click libraries, you should see something like:
    • At the text are "libraries (-l)", click add, as figured in the above snapshot.
    • Type the name of the run time library you want to add (in out case: "pthread") without the preceding "-l"
    • Now build and run your project .. enjoy pthreads

No comments:

Post a Comment