c++avi

#include <avifile.h> requires #include <stdlib.h> before to make build succeed


I need to use the avifile library (https://packages.debian.org/stretch/libavifile-0.7-dev) in C++.

I have the following main.cpp file as a minimal reproducible example :

#include <avifile.h>

int main(void) {
    return 0;
}

I'm trying to build the main.cpp file inside a Docker container (for reproducible builds), using this Dockerfile to install only needed dependencies :

FROM    debian:stretch

RUN     apt update \
 &&     apt install -y \
            g++ \
            libavifile-0.7-dev

When I build inside the container :

g++ -o main.o main.cpp `avifile-config --libs` `avifile-config --cflags` 

, which resolves to the following command :

g++ -o main.o main.cpp -L/usr/lib/x86_64-linux-gnu -laviplay -I/usr/include/avifile-0.7

The build gives many errors, for example :

In file included from /usr/include/avifile-0.7/avm_stl.h:8:0,
                 from /usr/include/avifile-0.7/avifile.h:4,
                 from main.cpp:1:
/usr/include/avifile-0.7/avm_stl.h: In member function 'void avm::vector<Type>::erase(avm::vector<Type>::iterator)':
/usr/include/avifile-0.7/avm_stl.h:333:12: error: 'm_size' was not declared in this scope
     assert(m_size > 0);
            ^
In file included from main.cpp:1:0:
/usr/include/avifile-0.7/avifile.h: At global scope:
/usr/include/avifile-0.7/avifile.h:96:13: error: 'size_t' does not name a type
     virtual size_t GetHeader(void* pheader = 0, size_t size = 0) const =0;
             ^~~~~~
/usr/include/avifile-0.7/avifile.h:104:13: error: 'size_t' does not name a type
     virtual size_t GetAudioFormat(void* format = 0, size_t size = 0) const =0;
             ^~~~~~

The import #include <avifile.h> is responsible of the errors. The errors are related to avifile headers, so I can't modify them. It looks like the build cannot find some basic stuff, like m_size and size_t. If I add #include <stdlib.h> before :

#include <stdlib.h>
#include <avifile.h>

int main(void) {
    return 0;
}

Then, it builds fine.

Why do I need to include stdlib.h along with avifile.h here? I'm not very familiar with C++, but I understand that m_size, size_t, ... (all required by avifile) is defined in stdlib.h though.


Solution

  • The problem, as you have already deduced, is that there is code in the avifile.h that uses variables or class members declared with the size_t type, which is not an in-built data type but a 'user'-defined typedef declared in one of the standard system headers. The stdlib.h header (though it's better to use <cstdlib> in C++ code) will automatically include that header, if required, as will other system headers, like <iostream> and <cstddef>.

    Generally, third-party headers, like avifile.h should themselves check for the necessary header dependencies, and explicitly #include them if required. However, it seems that, in your case, that header does not do so.

    So, for now, you should ensure that you always #include one of the system headers before your #include <avifile.h> line. In your situation, I would also contact the providers of that library to let them know about the problem, which (IMHO) should be considered an error.

    Incidentally, the m_size you mention is not a standard type, but is more likely to be a member variable of one of the classes that is declared as a size_t variable.