Search code examples
clinkerpthreadsunix-ar

link a library I wrote with pthread library


This is actually part of an assignment. Basically, we need to write a library, which will be linked against test programs the professor writes, like so:

gcc -o libexample.o -c libexample.c
ar rvs libexample.a libexample.o

#later
gcc -o test test.c -L . -lexample

The thing is that libexample uses POSIX semaphores, which needs linking with the pthread library when generating the final executable. Without changing the way test program compiles, is there a way to package the pthread library with libexample.a?

Thanks!


Solution

  • Without changing the way test program compiles, is there a way to package the pthread library with libexample.a?

    No.

    Are you restricted to supplying just the single libexample.a file?

    If not (and if you are using GNU linker), a possible solution is to provide libexample.a as linker script, which will link against e.g. libexample_code.a containing your object files and add -lpthread.