简体   繁体   中英

Using Static Library instead of Shared Library

I'm working on a small C project that uses curl and miniz. Curl is installed as a shared library and Miniz is inside ./miniz_folder/

It downloads and compress a single file from a given URL. The makefile I'm using is:

CC = gcc
#
ifeq ($(OS),Windows_NT)
    CCFLAGS += -D WIN32
    ifeq ($(PROCESSOR_ARCHITECTURE),AMD64)
        CCFLAGS += -D AMD64
endif
ifeq ($(PROCESSOR_ARCHITECTURE),x86)
    CCFLAGS += -D IA32
endif
else
    UNAME_S := $(shell uname -s)
    ifeq ($(UNAME_S),Linux)
        CCFLAGS += -D LINUX
    endif
    #
    # - - Mac OS X - -
    #
    ifeq ($(UNAME_S),Darwin)
    CFLAGS = -Wall -g
    CFLAGS += -I/usr/local/lib -L/usr/local/lib
    OSNAME = MacOSX
    #
    endif
    #
    # - - Ubuntu 64 bits - -
    #
    UNAME_P := $(shell uname -p)
    ifeq ($(UNAME_P),x86_64)
        CFLAGS = -Wall -g
        CFLAGS += -I/usr/local/lib -L/usr/local/lib
        OSNAME = Linux
    endif
    #
endif
#
# - - - - - - - - - -
#
VERBOSE := -v
CFLAGS += $(VERBOSE) -std=c99
#
example:
    $(CC) -v $(CFLAGS) example_code.c -o example_code_$(OSNAME) -lcurl

Now I want to use curl as a static library. I read that I can copy the libcurl.a file into the same location of the example_code.c, and modify the Makefile line

CFLAGS += -I/usr/local/lib -L/usr/local/lib

to

CFLAGS += -Bstatic -L. -lcurl

And add this

static_example:
    $(CC) -v example_code.c $(CFLAGS) -o static_example_code_$(OSNAME)

By doing

make static_example

I got

gcc -v example_code.c -Wall -g -Bstatic -L. -lcurl -v -std=c99 -o  static_example_code_Linux
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/4.6/lto-wrapper
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu/Linaro 4.6.3-1ubuntu5' --with-bugurl=file:///usr/share/doc/gcc-4.6/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.6 --enable-shared --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.6 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-gnu-unique-object --enable-plugin --enable-objc-gc --disable-werror --with-arch-32=i686 --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5) 
COLLECT_GCC_OPTIONS='-v' '-Wall' '-g' '-B' 'static' '-L.' '-v' '-std=c99' '-o' 'static_example_code_Linux' '-mtune=generic' '-march=x86-64'
/usr/lib/gcc/x86_64-linux-gnu/4.6/cc1 -quiet -v -v -imultilib . -imultiarch x86_64-linux-gnu example_code.c -quiet -dumpbase example_code.c -mtune=generic -march=x86-64 -auxbase example_code -g -Wall -std=c99 -version -fstack-protector -o /tmp/ccOU3ajz.s
GNU C (Ubuntu/Linaro 4.6.3-1ubuntu5) version 4.6.3 (x86_64-linux-gnu)
compiled by GNU C version 4.6.3, GMP version 5.0.2, MPFR version 3.1.0-p3, MPC version 0.9
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
ignoring nonexistent directory "/usr/local/include/x86_64-linux-gnu"
ignoring nonexistent directory "/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../../x86_64-linux-gnu/include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/lib/gcc/x86_64-linux-gnu/4.6/include
 /usr/local/include
 /usr/lib/gcc/x86_64-linux-gnu/4.6/include-fixed
 /usr/include/x86_64-linux-gnu
 /usr/include
End of search list.
GNU C (Ubuntu/Linaro 4.6.3-1ubuntu5) version 4.6.3 (x86_64-linux-gnu)
    compiled by GNU C version 4.6.3, GMP version 5.0.2, MPFR version 3.1.0-p3, MPC version 0.9
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: 75e879ed14f91af504f4150eadeaa0e6
example_code.c:5:23: fatal error: curl/curl.h: No such file or directory
compilation terminated.
make: *** [static_example] Error 1

Is there something I'm missing about using the libcurl.a static library?

The problem is not with the libray, but with the include files that come with it. Basically some of your source files need to use the curl/curl.h header file, and the compiler cannot find it. You'll need to add back the -I/usr/local/lib argument to CFLAGS , as the -I argument tells the compiler about additional directories where it can search for header files:

CFLAGS += -Bstatic -L. -lcurl -I/usr/local/lib

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM