Missing `#include <stdlib.h>` in `modules/audio_coding/codecs/opus/opus_interface.cc`

118 views
Skip to first unread message

Piasy Xu

unread,
Dec 13, 2019, 7:31:51 PM12/13/19
to discuss-webrtc
Hey guys, I'm maintaining a set of CMakeLists to enable C++ code debugging on Android Studio (https://github.com/Piasy/webrtc/tree/hack_webrtc/sdk/android_gradle), I notice that in recent commits, the original `opus_interface.c` is renamed into `opus_interface.cc`, and the `#include <stdlib.h>` is deleted.

Although ninja could compile with this setup, but my CMakeLists build breaks, it fails with errors like below:

error: use of undeclared identifier "calloc"

adding `#include <stdlib.h>` could fix it.

My question is:

1. why could ninja build successfully without `#include <stdlib.h>`?
2. could we add `#include <stdlib.h>` back to `opus_interface.cc`?

Thanks in advance.

Piasy Xu

unread,
Dec 21, 2019, 7:43:38 PM12/21/19
to discuss-webrtc
Anybody help me? Thanks!

在 2019年12月14日星期六 UTC+8上午8:31:51,Piasy Xu写道:

Patrik Höglund

unread,
Jan 7, 2020, 3:09:46 AM1/7/20
to discuss-webrtc
Hi!

It comes from the "sysroot". I did the following to figure that out:

diff --git a/modules/audio_coding/BUILD.gn b/modules/audio_coding/BUILD.gn
index 14a1bdc0e5..9a2a5df543 100644
--- a/modules/audio_coding/BUILD.gn
+++ b/modules/audio_coding/BUILD.gn
@@ -821,6 +821,7 @@ rtc_library("webrtc_multiopus") {
 
 rtc_library("webrtc_opus_wrapper") {
   poisonous = [ "audio_codecs" ]
+  cflags = ["-E"]  # https://stackoverflow.com/questions/4900870/can-gcc-output-c-code-after-preprocessing
   sources = [
     "codecs/opus/opus_inst.h",
     "codecs/opus/opus_interface.cc",

Then
$ gn gen out/Default
$ autoninja -C out/Default webrtc_opus_wrapper
$ less out/Default/obj/modules/audio_coding/webrtc_opus_wrapper/opus_interface.o

I can find the signature for calloc in the preprocessed source:

# 395 "../../build/linux/debian_sid_amd64-sysroot/usr/include/stdlib.h" 2 3 4
<snip>
extern void *calloc (size_t __nmemb, size_t __size)
     throw () __attribute__ ((__malloc__)) ;

I find that rather surprising. Like you suggest, I would expect that you would have to include stdlib.h and not get it forced upon you. I tried checking where it comes from with -H to GCC:

. ../../rtc_base/checks.h
.. ../../buildtools/third_party/libc++/trunk/include/string
...... ../../buildtools/third_party/libc++/trunk/include/utility
........ ../../buildtools/third_party/libc++/trunk/include/cstdlib
......... ../../buildtools/third_party/libc++/trunk/include/stdlib.h
.......... ../../build/linux/debian_sid_amd64-sysroot/usr/include/stdlib.h

I think that means rtc_base/checks.h includes <string>, which includes <utility>, which includes <cstdlib>, which includes stdlib.h. Makes sense? I guess you're compiling with a different stdlib than what we are since it's not doing that for you?

/ P

--

---
You received this message because you are subscribed to the Google Groups "discuss-webrtc" group.
To unsubscribe from this group and stop receiving emails from it, send an email to discuss-webrt...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/discuss-webrtc/f45e3fb3-bc79-4352-bacf-b13b1824be5a%40googlegroups.com.

Niels Moller

unread,
Jan 7, 2020, 8:21:39 AM1/7/20
to discuss...@googlegroups.com
As for question (2), I think that's the correct fix (or #include <cstdlib>, depending on taste). The file uses the calloc function, and shouldn't rely on indirect includes via other headers. 

Regards,
/Niels

Niels Moller

unread,
Jan 7, 2020, 8:27:09 AM1/7/20
to discuss...@googlegroups.com

Patrik Höglund

unread,
Jan 7, 2020, 8:30:09 AM1/7/20
to discuss-webrtc
Yes. We should follow the include-what-you-use rule.

/ P

--

---
You received this message because you are subscribed to the Google Groups "discuss-webrtc" group.
To unsubscribe from this group and stop receiving emails from it, send an email to discuss-webrt...@googlegroups.com.

Piasy Xu

unread,
Jan 7, 2020, 7:06:32 PM1/7/20
to discuss-webrtc
Neat! Thank you guys!

在 2020年1月7日星期二 UTC+8下午9:27:09,Niels Moller写道:
Reply all
Reply to author
Forward
0 new messages