如何使用静态库创建项目文件(Snappy)

时间:2014-04-14 12:22:56

标签: c++ makefile snappy

我有使用Snappy库和makefile的项目:

CXX=g++
CXXFLAGS=-c -Wall
LFLAGS=
OBJS=main.o Utilities.o FramingFormat.o Crc32.o

snappy.out: $(OBJS)
        $(CXX) $(LFLAGS) $^ -o $@

$(OBJS): %.o:%.cpp
        $(CXX) $(CXXFLAGS) $< -o $@
clean:
    -rm -rf *.o
.PHONY: clean

Snappy库早先已经建成。

现在我运行我的makefile我有错误:

g++  main.o Utilities.o FramingFormat.o Crc32.o -o snappy.out
FramingFormat.o: In function `compressToFrame(char*, unsigned long, char*, unsigned long*)':
FramingFormat.cpp:(.text+0x5b): undefined reference to `snappy_compress'
FramingFormat.o: In function `uncompressFromFrameData(char*, unsigned long, char*, unsigned long*)':
FramingFormat.cpp:(.text+0x14a): undefined reference to `snappy_uncompress'
FramingFormat.o: In function `maxFrameLength(unsigned long)':
FramingFormat.cpp:(.text+0x2bf): undefined reference to `snappy_max_compressed_length'
FramingFormat.o: In function `uncompressedDataLength(char*, unsigned long, unsigned long*)':
FramingFormat.cpp:(.text+0x2f8): undefined reference to `snappy_uncompressed_length'
collect2: error: ld returned 1 exit status
make: *** [snappy.out] Error 1

因为makefile不知道我使用snappy libs如何解决这个问题?这是我的目录:

  • 活泼/目录与 - 活泼
  • 活泼/目录与 - 使用-活泼项目

[编辑] 我的makefile看起来像这样:

CXX=g++
CXXFLAGS=-c -Wall
LFLAGS=
OBJS=main.o Utilities.o FramingFormat.o Crc32.o

snappy.out: $(OBJS)
        $(CXX) $(LFLAGS) $^ -L"../../SnappyLib1.1.2/SnappyLib1.1.2" -o $@

$(OBJS): %.o:%.cpp
        $(CXX) $(CXXFLAGS) $< -L"../../SnappyLib1.1.2/SnappyLib1.1.2" -o $@
clean:
    -rm -rf *.o
.PHONY: clean

1 个答案:

答案 0 :(得分:2)

在链接器选项中使用-lsnappy,假设您在可访问目录中有snappy.so或snappy.a。或者您可能必须明确使用该目录