antirez / antirez/smaz

"warning: implicit declaration of function ‘random’" message when compiling

Open
#15 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
C
Stars
1.2k
Forks
133
PR merge metrics
No merged PRs in 30d

Description

When I compile, I get the following warning:

```
$ make clean ; make
rm -rf smaz_test
gcc -o smaz_test -O2 -Wall -W -ansi -pedantic smaz.c smaz_test.c
smaz_test.c: In function ‘main’:
smaz_test.c:58:18: warning: implicit declaration of function ‘random’ [-Wimplicit-function-declaration]
ranlen = random() % 512;
```

The program compiles and the tests pass, so it looks like this warning is non-critical.

From looking around, I found an [SO question/answer that offers a solution](https://stackoverflow.com/questions/575350/why-cant-gcc-find-the-random-interface-when-std-c99-is-set/1085096#1085096).

Adding the `-D_XOPEN_SOURCE=600` option to `gcc` in the `Makefile` fixes the issue for me:

```
$ git diff
diff --git a/Makefile b/Makefile
index 62e8ccb..eecbac7 100644
--- a/Makefile
+++ b/Makefile
@@ -1,7 +1,7 @@
all: smaz_test

smaz_test: smaz_test.c smaz.c
- gcc -o smaz_test -O2 -Wall -W -ansi -pedantic smaz.c smaz_test.c
+ gcc -D_XOPEN_SOURCE=600 -o smaz_test -O2 -Wall -W -ansi -pedantic smaz.c smaz_test.c

clean:
rm -rf smaz_test
```

My system information:

```
$ gcc --version
gcc (Ubuntu 5.4.0-6ubuntu1~16.04.12) 5.4.0 20160609
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.