Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't gcc find my static library?

Tags:

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.

like image 746
sdasdadas Avatar asked Aug 29 '12 21:08

sdasdadas


People also ask

Where do I put my static library?

Static libraries belong next to their corresponding dynamic libraries, and in accordance with the FHS. Keep in mind that static libraries are usually only needed to build software, not run it.

Can a DLL link to static library?

When your DLL refers to an external content (like function or variable), it is resolved at linking time - together with all dependencies. But that's all. If your static library has a function named print_sample_string() , but your DLL does not use it, it won't be attached to DLL image.


1 Answers

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.)

like image 135
Paul R Avatar answered Oct 09 '22 18:10

Paul R