miniz.c get"已定义" VS中的错误

时间:2014-07-14 14:23:59

标签: c++ visual-c++

将miniz.c添加到项目中并包含它:

#include "miniz.c"

int main(){
    return 0;
}

链接器会发出大量“已定义”的错误。

1>  miniz.c
1>  Source.cpp
1>Source.obj : error LNK2005: _mz_zip_extract_archive_file_to_heap already defined in miniz.obj
1>Source.obj : error LNK2005: _mz_zip_add_mem_to_archive_file_in_place already defined in miniz.obj
1>Source.obj : error LNK2005: _mz_zip_writer_end already defined in miniz.obj
1>Source.obj : error LNK2005: _mz_zip_writer_finalize_heap_archive already defined in miniz.obj
1>Source.obj : error LNK2005: _mz_zip_writer_finalize_archive already defined in miniz.obj
1>Source.obj : error LNK2005: _mz_zip_writer_add_from_zip_reader already defined in miniz.obj
1>Source.obj : error LNK2005: _mz_zip_writer_add_file already defined in miniz.obj
1>Source.obj : error LNK2005: _mz_zip_writer_add_mem_ex already defined in miniz.obj
1>Source.obj : error LNK2005: _mz_zip_writer_add_mem already defined in miniz.obj
1>Source.obj : error LNK2005: _mz_zip_writer_init_from_reader already defined in miniz.obj
1>Source.obj : error LNK2005: _mz_zip_writer_init_file already defined in miniz.obj
1>Source.obj : error LNK2005: _mz_zip_writer_init_heap already defined in miniz.obj
1>Source.obj : error LNK2005: _mz_zip_writer_init already defined in miniz.obj
1>Source.obj : error LNK2005: _mz_zip_reader_extract_file_to_file already defined in miniz.obj
1>Source.obj : error LNK2005: _mz_zip_reader_end already defined in miniz.obj
1>Source.obj : error LNK2005: _mz_zip_reader_extract_to_file already defined in miniz.obj
1>Source.obj : error LNK2005: _mz_zip_reader_extract_file_to_callback already defined in miniz.obj
1>Source.obj : error LNK2005: _mz_zip_reader_extract_to_callback already defined in miniz.obj
1>Source.obj : error LNK2005: _mz_zip_reader_extract_file_to_heap already defined in miniz.obj
1>Source.obj : error LNK2005: _mz_zip_reader_extract_to_heap already defined in miniz.obj
1>Source.obj : error LNK2005: _mz_zip_reader_extract_file_to_mem already defined in miniz.obj
1>Source.obj : error LNK2005: _mz_zip_reader_extract_to_mem already defined in miniz.obj
1>Source.obj : error LNK2005: _mz_zip_reader_extract_file_to_mem_no_alloc already defined in miniz.obj
1>Source.obj : error LNK2005: _mz_zip_reader_extract_to_mem_no_alloc already defined in miniz.obj
1>Source.obj : error LNK2005: _mz_zip_reader_locate_file already defined in miniz.obj
1>Source.obj : error LNK2005: _mz_zip_reader_get_filename already defined in miniz.obj
1>Source.obj : error LNK2005: _mz_zip_reader_file_stat already defined in miniz.obj
1>Source.obj : error LNK2005: _mz_zip_reader_is_file_a_directory already defined in miniz.obj
1>Source.obj : error LNK2005: _mz_zip_reader_is_file_encrypted already defined in miniz.obj
1>Source.obj : error LNK2005: _mz_zip_reader_get_num_files already defined in miniz.obj
1>Source.obj : error LNK2005: _mz_zip_reader_init_file already defined in miniz.obj
1>Source.obj : error LNK2005: _mz_zip_reader_init_mem already defined in miniz.obj
1>Source.obj : error LNK2005: _mz_zip_reader_init already defined in miniz.obj
1>Source.obj : error LNK2005: _tdefl_write_image_to_png_file_in_memory already defined in miniz.obj
1>Source.obj : error LNK2005: _tdefl_write_image_to_png_file_in_memory_ex already defined in miniz.obj
1>Source.obj : error LNK2005: _tdefl_create_comp_flags_from_zip_params already defined in miniz.obj
1>Source.obj : error LNK2005: _tdefl_compress_mem_to_mem already defined in miniz.obj
1>Source.obj : error LNK2005: _tdefl_compress_mem_to_heap already defined in miniz.obj
1>Source.obj : error LNK2005: _tdefl_compress_mem_to_output already defined in miniz.obj
1>Source.obj : error LNK2005: _tdefl_get_adler32 already defined in miniz.obj
1>Source.obj : error LNK2005: _tdefl_get_prev_return_status already defined in miniz.obj
1>Source.obj : error LNK2005: _tdefl_init already defined in miniz.obj
1>Source.obj : error LNK2005: _tdefl_compress_buffer already defined in miniz.obj
1>Source.obj : error LNK2005: _tdefl_compress already defined in miniz.obj
1>Source.obj : error LNK2005: _tinfl_decompress_mem_to_callback already defined in miniz.obj
1>Source.obj : error LNK2005: _tinfl_decompress_mem_to_mem already defined in miniz.obj
1>Source.obj : error LNK2005: _tinfl_decompress_mem_to_heap already defined in miniz.obj
1>Source.obj : error LNK2005: _tinfl_decompress already defined in miniz.obj
1>Source.obj : error LNK2005: _mz_error already defined in miniz.obj
1>Source.obj : error LNK2005: _mz_uncompress already defined in miniz.obj
1>Source.obj : error LNK2005: _mz_inflateEnd already defined in miniz.obj
1>Source.obj : error LNK2005: _mz_inflate already defined in miniz.obj
1>Source.obj : error LNK2005: _mz_inflateInit already defined in miniz.obj
1>Source.obj : error LNK2005: _mz_inflateInit2 already defined in miniz.obj
1>Source.obj : error LNK2005: _mz_compressBound already defined in miniz.obj
1>Source.obj : error LNK2005: _mz_compress already defined in miniz.obj
1>Source.obj : error LNK2005: _mz_compress2 already defined in miniz.obj
1>Source.obj : error LNK2005: _mz_deflateBound already defined in miniz.obj
1>Source.obj : error LNK2005: _mz_deflateEnd already defined in miniz.obj
1>Source.obj : error LNK2005: _mz_deflate already defined in miniz.obj
1>Source.obj : error LNK2005: _mz_deflateReset already defined in miniz.obj
1>Source.obj : error LNK2005: _mz_deflateInit2 already defined in miniz.obj
1>Source.obj : error LNK2005: _mz_deflateInit already defined in miniz.obj
1>Source.obj : error LNK2005: _mz_version already defined in miniz.obj
1>Source.obj : error LNK2005: _mz_free already defined in miniz.obj
1>Source.obj : error LNK2005: _mz_crc32 already defined in miniz.obj
1>Source.obj : error LNK2005: _mz_adler32 already defined in miniz.obj

为那些不熟悉图书馆https://code.google.com/p/miniz/

的人链接到miniz.c

2 个答案:

答案 0 :(得分:8)

永远不会包含源文件!他们不打算这样做。

头文件.h中的单独声明和.c文件中的定义。

将头文件包含在需要引用声明的任何位置。


至于您的具体情况,您应该包含miniz.c,如下所示

#define MINIZ_HEADER_FILE_ONLY
#include "miniz.c"

int main(){
    return 0;
}

这在miniz.c文件本身(第928行)

中有记录
// ------------------- End of Header: Implementation follows. 
// (If you only want the header, define MINIZ_HEADER_FILE_ONLY.)

答案 1 :(得分:2)

为了使用这个疯狂设计的库作为作者(显然)的意图,你需要:

  • 仅包含一个源文件 - 对库的所有访问都必须通过该文件。
  • 也不要编译和链接图书馆;它的定义已经在包含它的文件中。不要将它添加到项目中,或者找到一种方法告诉IDE它不应该被视为源文件。
  • 如果您正在编写图书馆,则
  • 不要使用,因为这会阻止任何使用图书馆的程序也使用此库。

或者,您可以编写自己的包含库声明的标头,包括而不是.c文件,并将库编译并链接为普通源文件。或者您可以查找由了解编译模型的人编写的库。