24

I am trying to link the cspec library into my C project. This is my Makefile located in the tests folder:

all: test

test: sample.o
    gcc -Wall -o test sample.o -L ../lib/cspec -llibcspec.a

sample.o: sample.c
    gcc -Wall -c sample.c -I../lib/cspec

clean:
    rm -rf *o test

My directory is:

/
/src
/lib
/lib/cspec
/tests

When I run make I receive the following error:

gcc -Wall -o test sample.o -L ../lib/cspec -llibcspec.a
/usr/bin/ld: cannot find -llibcspec.a

I have made sure that the the libcspec.a file is located in the lib/cspec folder but to be sure I have also tried placing it within the tests folder and removing the -L command, to no avail.

Paul R
  • 208,748
  • 37
  • 389
  • 560
sdasdadas
  • 23,917
  • 20
  • 63
  • 148

1 Answers1

48

Change:

gcc -Wall -o test sample.o -L ../lib/cspec -llibcspec.a

to:

gcc -Wall -o test sample.o -L ../lib/cspec -lcspec

(By convention, gcc and other *nix compilers automatically add the lib prefix and the appropriate suffix.)

Paul R
  • 208,748
  • 37
  • 389
  • 560
  • 4
    Note that if you happen to have a `libcspec.so` in the same directory as `libcspec.a`, your command will result in linking with the `.so` library. If you'd really like to use the `.a` library, use `-l:cspec.a`. (See https://stackoverflow.com/questions/6578484/telling-gcc-directly-to-link-a-library-statically.) – Jay Sullivan Jul 13 '20 at 23:09