简体   繁体   English

如何编译和运行FNV哈希

[英]How to compile and run FNV Hash

I am looking into open source hash functions to observe how the source code and hashing algorithm handles hash collisions. 我正在研究开源哈希函数,以观察源代码和哈希算法如何处理哈希冲突。 I am currently interested in low bit hash function and I found out about FNV through this other post 我目前对低位哈希函数感兴趣,并且通过另一篇文章我发现了FNV

However, does anyone know how I can actually test this specific source code? 但是,有谁知道我如何才能实际测试此特定源代码? I have tried compiling some of the C files but I keep getting errors from gcc: 我已经尝试编译一些C文件,但是我不断从gcc收到错误消息:

hb2@hb1:~/Desktop/fnv$ gcc test_fnv.c
test_fnv.c: In function ‘unknown_hash_type’:
test_fnv.c:2183:5: warning: incompatible implicit declaration of built-in function ‘exit’ [enabled by default]
test_fnv.c: In function ‘print_fnv32’:
test_fnv.c:2200:2: warning: format ‘%lx’ expects argument of type ‘long unsigned int’, but argument 2 has type ‘Fnv32_t’ [-Wformat]
test_fnv.c:2202:2: warning: format ‘%lx’ expects argument of type ‘long unsigned int’, but argument 2 has type ‘Fnv32_t’ [-Wformat]
test_fnv.c: In function ‘print_fnv64’:
test_fnv.c:2221:2: warning: format ‘%llx’ expects argument of type ‘long long unsigned int’, but argument 2 has type ‘Fnv64_t’ [-Wformat]
test_fnv.c:2223:2: warning: format ‘%llx’ expects argument of type ‘long long unsigned int’, but argument 2 has type ‘Fnv64_t’ [-Wformat]
/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
collect2: ld returned 1 exit status
hb2@hb1:~/Desktop/fnv$

Any ideas on which files I have to specifically compile to run and test the program? 对要运行和测试程序必须专门编译的哪些文件有什么想法?

Thanks for your help in advance 谢谢您的帮助

FNV: info , download FNV: 信息下载

After running make check, I get the following: 运行make check之后,我得到以下信息:

hb2@hb1:~/Desktop/fnv$ make check
forming longlong.h
longlong.h formed
cc -O3 -g3 fnv32.c -c
fnv32.c: In function ‘test_fnv32’:
fnv32.c:156:11: warning: format ‘%lx’ expects argument of type ‘long unsigned int’, but argument 3 has type ‘Fnv32_t’ [-Wformat]
fnv32.c:169:8: warning: format ‘%lx’ expects argument of type ‘long unsigned int’, but argument 4 has type ‘Fnv32_t’ [-Wformat]
fnv32.c:169:8: warning: format ‘%lx’ expects argument of type ‘long unsigned int’, but argument 5 has type ‘Fnv32_t’ [-Wformat]
fnv32.c:183:8: warning: format ‘%lx’ expects argument of type ‘long unsigned int’, but argument 4 has type ‘Fnv32_t’ [-Wformat]
fnv32.c:183:8: warning: format ‘%lx’ expects argument of type ‘long unsigned int’, but argument 5 has type ‘Fnv32_t’ [-Wformat]
fnv32.c:197:8: warning: format ‘%lx’ expects argument of type ‘long unsigned int’, but argument 4 has type ‘Fnv32_t’ [-Wformat]
fnv32.c:197:8: warning: format ‘%lx’ expects argument of type ‘long unsigned int’, but argument 5 has type ‘Fnv32_t’ [-Wformat]
cc -O3 -g3 hash_32.c -c
cc -O3 -g3 hash_64.c -c
cc -O3 -g3 hash_32a.c -c
cc -O3 -g3 hash_64a.c -c
cc -O3 -g3 test_fnv.c -c
test_fnv.c: In function ‘unknown_hash_type’:
test_fnv.c:2183:5: warning: incompatible implicit declaration of built-in function ‘exit’ [enabled by default]
test_fnv.c: In function ‘print_fnv32’:
test_fnv.c:2200:2: warning: format ‘%lx’ expects argument of type ‘long unsigned int’, but argument 2 has type ‘Fnv32_t’ [-Wformat]
test_fnv.c:2202:2: warning: format ‘%lx’ expects argument of type ‘long unsigned int’, but argument 2 has type ‘Fnv32_t’ [-Wformat]
test_fnv.c: In function ‘print_fnv64’:
test_fnv.c:2221:2: warning: format ‘%llx’ expects argument of type ‘long long unsigned int’, but argument 2 has type ‘Fnv64_t’ [-Wformat]
test_fnv.c:2223:2: warning: format ‘%llx’ expects argument of type ‘long long unsigned int’, but argument 2 has type ‘Fnv64_t’ [-Wformat]
rm -f libfnv.a
ar rv libfnv.a hash_32.o hash_64.o hash_32a.o hash_64a.o test_fnv.o
ar: creating libfnv.a
a - hash_32.o
a - hash_64.o
a - hash_32a.o
a - hash_64a.o
a - test_fnv.o
: libfnv.a
cc fnv32.o libfnv.a -o fnv032
cc -O3 -g3 fnv64.c -c
fnv64.c: In function ‘test_fnv64’:
fnv64.c:160:10: warning: format ‘%llx’ expects argument of type ‘long long unsigned int’, but argument 3 has type ‘Fnv64_t’ [-Wformat]
fnv64.c:175:8: warning: format ‘%llx’ expects argument of type ‘long long unsigned int’, but argument 4 has type ‘Fnv64_t’ [-Wformat]
fnv64.c:175:8: warning: format ‘%llx’ expects argument of type ‘long long unsigned int’, but argument 5 has type ‘Fnv64_t’ [-Wformat]
fnv64.c:190:8: warning: format ‘%llx’ expects argument of type ‘long long unsigned int’, but argument 4 has type ‘Fnv64_t’ [-Wformat]
fnv64.c:190:8: warning: format ‘%llx’ expects argument of type ‘long long unsigned int’, but argument 5 has type ‘Fnv64_t’ [-Wformat]
fnv64.c:205:8: warning: format ‘%llx’ expects argument of type ‘long long unsigned int’, but argument 4 has type ‘Fnv64_t’ [-Wformat]
fnv64.c:205:8: warning: format ‘%llx’ expects argument of type ‘long long unsigned int’, but argument 5 has type ‘Fnv64_t’ [-Wformat]
cc fnv64.o libfnv.a -o fnv064
rm -f fnv132
cp -f fnv032 fnv132
rm -f fnv164
cp -f fnv064 fnv164
rm -f fnv1a32
cp -f fnv032 fnv1a32
rm -f fnv1a64
cp -f fnv064 fnv1a64
FNV-0 32 bit tests: passed
FNV-1 32 bit tests: passed
FNV-1a 32 bit tests: passed
FNV-0 64 bit tests: passed
FNV-1 64 bit tests: passed
FNV-1a 64 bit tests: passed
hb2@hb1:~/Desktop/fnv$ 

Maybe this is what I am supposed to get, I will read the FNV more thoroughly to see how to actually run tests, but I didnt see that the first time 也许这就是我应该得到的,我将更彻底地阅读FNV,以了解如何实际运行测试,但是我没有第一次看到

When I download FNV 5.0.3 and build it on a Mac, I get a number of compiler warnings about mismatches between the conversion specifications in the format strings and the argument actually passed. 当我下载FNV 5.0.3并在Mac上构建时,我收到许多编译器警告,提示格式字符串中的转换规范与实际传递的参数不匹配。 However, when I run make check , the code reports that the test vectors all pass. 但是,当我运行make check ,代码报告测试向量全部通过。 (It uses echo -n but the echo I've got doesn't recognize -n as an option, so the output looks a little odd.) (它使用echo -n但是我得到的echo无法识别-n作为选项,因此输出看起来有些奇怪。)

Example build output: 示例生成输出:

$ make
forming longlong.h
longlong.h formed
cc -O3 -g3 hash_32.c -c
cc -O3 -g3 hash_64.c -c
cc -O3 -g3 hash_32a.c -c
cc -O3 -g3 hash_64a.c -c
cc -O3 -g3 test_fnv.c -c
test_fnv.c:2183:5: warning: implicitly declaring library function 'exit' with type 'void (int)
      __attribute__((noreturn))'
    exit(code);
    ^
test_fnv.c:2183:5: note: please include the header <stdlib.h> or explicitly provide a declaration
      for 'exit'
test_fnv.c:2200:25: warning: format specifies type 'unsigned long' but the argument has type
      'unsigned int' [-Wformat]
        printf("0x%08lx %s\n", hval & mask, arg);
                  ~~~~~        ^~~~~~~~~~~
                  %08x
test_fnv.c:2202:22: warning: format specifies type 'unsigned long' but the argument has type
      'unsigned int' [-Wformat]
        printf("0x%08lx\n", hval & mask);
                  ~~~~~     ^~~~~~~~~~~
                  %08x
3 warnings generated.
rm -f libfnv.a
ar rv libfnv.a hash_32.o hash_64.o hash_32a.o hash_64a.o test_fnv.o
ar: creating archive libfnv.a
a - hash_32.o
a - hash_64.o
a - hash_32a.o
a - hash_64a.o
a - test_fnv.o
: libfnv.a
cc -O3 -g3 fnv32.c -c
fnv32.c:156:21: warning: format specifies type 'unsigned long' but the argument has type
      'unsigned int' [-Wformat]
                    tstnum-1, hval & mask);
                              ^~~~~~~~~~~
fnv32.c:168:17: warning: format specifies type 'unsigned long' but the argument has type
      'unsigned int' [-Wformat]
                            program, (hval&mask),
                                     ^~~~~~~~~~~
fnv32.c:169:8: warning: format specifies type 'unsigned long' but the argument has type
      'unsigned int' [-Wformat]
                            (fnv0_32_vector[tstnum-1].fnv0_32 & mask));
                            ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
fnv32.c:182:17: warning: format specifies type 'unsigned long' but the argument has type
      'unsigned int' [-Wformat]
                            program, (hval&mask),
                                     ^~~~~~~~~~~
fnv32.c:183:8: warning: format specifies type 'unsigned long' but the argument has type
      'unsigned int' [-Wformat]
                            (fnv1_32_vector[tstnum-1].fnv1_32 & mask));
                            ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
fnv32.c:196:17: warning: format specifies type 'unsigned long' but the argument has type
      'unsigned int' [-Wformat]
                            program, (hval&mask),
                                     ^~~~~~~~~~~
fnv32.c:197:8: warning: format specifies type 'unsigned long' but the argument has type
      'unsigned int' [-Wformat]
                            (fnv1a_32_vector[tstnum-1].fnv1a_32 & mask));
                            ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
fnv32.c:159:14: warning: 4 enumeration values not handled in switch: 'FNV_NONE', 'FNV0_64',
      'FNV1_64'... [-Wswitch]
            switch (hash_type) {
                    ^
8 warnings generated.
cc fnv32.o libfnv.a -o fnv032
cc -O3 -g3 fnv64.c -c
fnv64.c:164:14: warning: 4 enumeration values not handled in switch: 'FNV_NONE', 'FNV0_32',
      'FNV1_32'... [-Wswitch]
            switch (hash_type) {
                    ^
1 warning generated.
cc fnv64.o libfnv.a -o fnv064
rm -f fnv132
cp -f fnv032 fnv132
rm -f fnv164
cp -f fnv064 fnv164
rm -f fnv1a32
cp -f fnv032 fnv1a32
rm -f fnv1a64
cp -f fnv064 fnv1a64
$

Running make check : 运行make check

$ make check
-n FNV-0 32 bit tests: 
passed
-n FNV-1 32 bit tests: 
passed
-n FNV-1a 32 bit tests: 
passed
-n FNV-0 64 bit tests: 
passed
-n FNV-1 64 bit tests: 
passed
-n FNV-1a 64 bit tests: 
passed
$

The warnings are not ideal. 警告并不理想。 If I get energetic, I might work on a patch and submit that to the FNV site. 如果我精力充沛,我可能会制作补丁并将其提交给FNV网站。 It depends on how much choss I get when I run with more stringent compilation options. 这取决于我使用更严格的编译选项运行时会得到多少选择。

Your main problem — resolved 您的主要问题-已解决

However, your immediate problem is that test_fnv.c is not designed to be compiled into a program on its own; 但是,您test_fnv.c直接问题是test_fnv.c并非旨在自己编译成程序。 it is a part of the library. 它是图书馆的一部分。

So you have the libfnv.a library already, you just need to include the fnv header in your own program, and then run gcc like this 因此,您已经有了libfnv.a库,您只需要在自己的程序中包含fnv标头,然后像这样运行gcc

gcc -I/path/to/header/of/fnv yourmain.c -L/path/to/libfnv.a -lfnv

assuming the main function is in yourmain.c . 假设主要功能在yourmain.c

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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