使用静态库而不是共享库

时间:2015-04-17 13:52:06

标签: c curl makefile static-libraries

我正在开发一个使用curl和miniz的小型C项目。 Curl作为共享库安装,Miniz位于./miniz_folder /

它从给定的URL下载并压缩单个文件。 我正在使用的makefile是:

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

现在我想使用curl作为静态库。我读到我可以将libcurl.a文件复制到example_code.c的相同位置,并修改Makefile行

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

CFLAGS += -Bstatic -L. -lcurl

并添加此

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

通过

make static_example

我得到了

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

使用libcurl.a静态库有什么遗漏吗?

1 个答案:

答案 0 :(得分:2)

问题不在于libray,而在于随附的include文件。基本上一些源文件需要使用curl/curl.h头文件,编译器无法找到它。您需要将-I/usr/local/lib参数添加回CFLAGS,因为-I参数告诉编译器有关其可以搜索头文件的其他目录:

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