2014-04-14 39 views
1

我都需要的文件建立活泼库:如何创建编译文件斯纳皮作为静态库

  • snappy.h
  • snappy.cc
  • 活泼-CH
  • snappy-c.cc
  • 活泼-internal.h
  • 活泼-sinksource.h
  • 活泼-sinksource..cc
  • 活泼存根-internal.h
  • snappy-stubs-internal.cc
  • 活泼存根-public.h
  • 活泼-test.h
  • snappy-test.cc
  • snappy- unittest.cc

如何创建makefile将Snappy制作为静态库。

我现在知道我必须从每个.cpp文件创建.o文件,所以我有变量和最重要的规则。

OBJS=snappy.o snappy-c.o snappy-sinksource.o snappy-stubs-internal.o\ 
snappy-test.o snappy_unittest.o 
CC=gcc 
CCFLAGS=-Wall -static 

$(OBJS): %.o:%.cc 
    $(CC) $(CCFLAGS) $< -o [email protected] 
clean: 
-rm -rf *.o 
.PHONY: clean 

我下一步做什么?

[编辑]: 我现在有:

OBJS=snappy.o snappy-c.o snappy-sinksource.o snappy-stubs-internal.o snappy-test.o snappy_unittest.o 
CC=gcc 
CCFLAGS=-Wall -static 
LIBNAME=libsnappy.a 

all:$(LIBNAME) 

$(LIBNAME): $(OBJS) 
    ar rcs [email protected] $^ 
$(OBJS): %.o:%.cc 
    $(CC) $(CCFLAGS) $< -o [email protected] 
clean: 
    -rm -rf *.o 
.PHONY: clean 

,但它仍然不能正常工作:

gcc -Wall -static snappy.cc -o snappy.o 
snappy.cc: In member function ‘bool snappy::SnappyIOVecWriter::Append(const char*, size_t)’: 
snappy.cc:1013:36: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] 
     if (curr_iov_index_ + 1 >= output_iov_count_) { 
            ^
snappy.cc: In member function ‘bool snappy::SnappyIOVecWriter::AppendFromSelf(size_t, size_t)’: 
snappy.cc:1095:38: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] 
      if (curr_iov_index_ + 1 >= output_iov_count_) { 
            ^
snappy.cc: At global scope: 
snappy.cc:567:13: warning: ‘void snappy::ComputeTable()’ defined but not used [-Wunused-function] 
static void ComputeTable() { 
      ^
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 0 has invalid symbol index 11 
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 1 has invalid symbol index 12 
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 2 has invalid symbol index 2 
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 3 has invalid symbol index 2 
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 4 has invalid symbol index 11 
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 5 has invalid symbol index 13 
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 6 has invalid symbol index 13 
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 7 has invalid symbol index 13 
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 8 has invalid symbol index 12 
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 9 has invalid symbol index 13 
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 10 has invalid symbol index 13 
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 11 has invalid symbol index 13 
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 12 has invalid symbol index 13 
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 13 has invalid symbol index 13 
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 14 has invalid symbol index 13 
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 15 has invalid symbol index 13 
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 16 has invalid symbol index 13 
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 17 has invalid symbol index 13 
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 18 has invalid symbol index 13 
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 19 has invalid symbol index 21 
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_line): relocation 0 has invalid symbol index 2 
/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu/crt1.o: In function `_start': 
(.text+0x20): undefined reference to `main' 
/tmp/ccTyhFNH.o: In function `snappy::internal::WorkingMemory::GetHashTable(unsigned long, int*)': 
snappy.cc:(.text+0x5f9): undefined reference to `operator new[](unsigned long)' 
/tmp/ccTyhFNH.o: In function `snappy::Compress(snappy::Source*, snappy::Sink*)': 
snappy.cc:(.text+0x1608): undefined reference to `operator new[](unsigned long)' 
snappy.cc:(.text+0x17ff): undefined reference to `operator new[](unsigned long)' 
snappy.cc:(.text+0x1926): undefined reference to `operator delete[](void*)' 
snappy.cc:(.text+0x193f): undefined reference to `operator delete[](void*)' 
/tmp/ccTyhFNH.o: In function `snappy::RawUncompressToIOVec(char const*, unsigned long, snappy::iovec const*, unsigned long)': 
snappy.cc:(.text+0x19ec): undefined reference to `snappy::ByteArraySource::~ByteArraySource()' 
snappy.cc:(.text+0x19ff): undefined reference to `snappy::ByteArraySource::~ByteArraySource()' 
/tmp/ccTyhFNH.o: In function `snappy::RawUncompress(char const*, unsigned long, char*)': 
snappy.cc:(.text+0x1aaa): undefined reference to `snappy::ByteArraySource::~ByteArraySource()' 
snappy.cc:(.text+0x1abd): undefined reference to `snappy::ByteArraySource::~ByteArraySource()' 
/tmp/ccTyhFNH.o: In function `snappy::Uncompress(char const*, unsigned long, std::string*)': 
snappy.cc:(.text+0x1b58): undefined reference to `std::string::max_size() const' 
/tmp/ccTyhFNH.o: In function `snappy::IsValidCompressedBuffer(char const*, unsigned long)': 
snappy.cc:(.text+0x1c03): undefined reference to `snappy::ByteArraySource::~ByteArraySource()' 
snappy.cc:(.text+0x1c16): undefined reference to `snappy::ByteArraySource::~ByteArraySource()' 
/tmp/ccTyhFNH.o: In function `snappy::RawCompress(char const*, unsigned long, char*, unsigned long*)': 
snappy.cc:(.text+0x1caf): undefined reference to `snappy::UncheckedByteArraySink::~UncheckedByteArraySink()' 
snappy.cc:(.text+0x1cbb): undefined reference to `snappy::ByteArraySource::~ByteArraySource()' 
snappy.cc:(.text+0x1ccc): undefined reference to `snappy::UncheckedByteArraySink::~UncheckedByteArraySink()' 
snappy.cc:(.text+0x1cdd): undefined reference to `snappy::ByteArraySource::~ByteArraySource()' 
/tmp/ccTyhFNH.o: In function `snappy::Compress(char const*, unsigned long, std::string*)': 
snappy.cc:(.text+0x1d23): undefined reference to `std::string::resize(unsigned long)' 
snappy.cc:(.text+0x1d59): undefined reference to `std::string::resize(unsigned long)' 
/tmp/ccTyhFNH.o: In function `snappy::STLStringResizeUninitialized(std::string*, unsigned long)': 
snappy.cc:(.text._ZN6snappy28STLStringResizeUninitializedEPSsm[_ZN6snappy28STLStringResizeUninitializedEPSsm]+0x1f): undefined reference to `std::string::resize(unsigned long)' 
/tmp/ccTyhFNH.o: In function `snappy::string_as_array(std::string*)': 
snappy.cc:(.text._ZN6snappy15string_as_arrayEPSs[_ZN6snappy15string_as_arrayEPSs]+0x15): undefined reference to `std::string::empty() const' 
snappy.cc:(.text._ZN6snappy15string_as_arrayEPSs[_ZN6snappy15string_as_arrayEPSs]+0x31): undefined reference to `std::string::begin()' 
/tmp/ccTyhFNH.o: In function `snappy::internal::WorkingMemory::~WorkingMemory()': 
snappy.cc:(.text._ZN6snappy8internal13WorkingMemoryD2Ev[_ZN6snappy8internal13WorkingMemoryD5Ev]+0x2b): undefined reference to `operator delete[](void*)' 
/tmp/ccTyhFNH.o: In function `snappy::Sink::Sink()': 
snappy.cc:(.text._ZN6snappy4SinkC2Ev[_ZN6snappy4SinkC5Ev]+0xf): undefined reference to `vtable for snappy::Sink' 
/tmp/ccTyhFNH.o: In function `snappy::Source::Source()': 
snappy.cc:(.text._ZN6snappy6SourceC2Ev[_ZN6snappy6SourceC5Ev]+0xf): undefined reference to `vtable for snappy::Source' 
/tmp/ccTyhFNH.o: In function `snappy::ByteArraySource::ByteArraySource(char const*, unsigned long)': 
snappy.cc:(.text._ZN6snappy15ByteArraySourceC2EPKcm[_ZN6snappy15ByteArraySourceC5EPKcm]+0x27): undefined reference to `vtable for snappy::ByteArraySource' 
/tmp/ccTyhFNH.o: In function `snappy::UncheckedByteArraySink::UncheckedByteArraySink(char*)': 
snappy.cc:(.text._ZN6snappy22UncheckedByteArraySinkC2EPc[_ZN6snappy22UncheckedByteArraySinkC5EPc]+0x23): undefined reference to `vtable for snappy::UncheckedByteArraySink' 
/tmp/ccTyhFNH.o:(.eh_frame+0x1ab): undefined reference to `__gxx_personality_v0' 
collect2: error: ld returned 1 exit status 
make: *** [snappy.o] Error 1 

[EDIT2]: 我的文件中有如下内容(这是工作现在):

OBJS=snappy.o snappy-c.o snappy-sinksource.o snappy-stubs-internal.o 
CC=gcc 
CCFLAGS=-Wall 
LIBNAME=libsnappy.a 
all: $(LIBNAME) 
$(LIBNAME): $(OBJS) 
    ar rcs [email protected] $^ 
    ranlib [email protected] 
$(OBJS): %.o:%.cc 
    $(CC) $(CCFLAGS) $< -c -o [email protected] 
clean: 
    -rm -rf *.o 
.PHONY: clean 

回答

1

你的规则编译和每一个源文件,这是十分错误的链接,你只需要编译它。

$(OBJS): %.o:%.cc 
    $(CC) $(CCFLAGS) $< -o [email protected] 

需要一个-c在它

$(OBJS): %.o:%.cc 
    $(CC) $(CCFLAGS) $< -c -o [email protected] 

-static标志不应该在CCFLAGS,这对链接阶段(和静态库,当你创建它们不获取链接)。

CCFLAGS=-Wall 
+0

'ranlib'太过分了,'ar'上的''s'选项可以做到。 – naab

1

创建与静态库命令:

LIBNAME=libsnappy.a 

$(LIBNAME): $(OBJS) 
     ar rcs [email protected] $^ 

all : $(LIBNAME) 

这里all规则将调用$(LIBNAME)这将调用所有*.o规则编译它们,然后将使用ar上生成的.o

基本上ar只是创建一个tar存档与提供的所有*.o文件。

选项ar可能是重要的:

  • -c告诉AR到创建的文件(libsnappy.a)
  • -r告诉欧德替换可能重复的符号。 (仅在重新编译时才有用。O)
  • -s相当于ranlib的磁带库
+0

为什么所有的规则都是在$(LIBNAME)之后,但之前没有? – CppMonster

+1

@CppMonster你可以先设置它,规则顺序在Makefile中没有什么区别 – naab