Since the zlib is a C library, to use it in a c program one would need to use extern "C"
when including the header, as in the following test program:
#include <iostream>
extern "C" {
#include <zlib.h>
}
int main()
{
gzFile file;
file = gzopen("test.gz", "w");
gzwrite(file, "hello\0", 6);
gzclose(file);
char text[6];
file = gzopen("test.gz", "r");
gzread(file, text, 6);
std::cout << text;
return 0;
}
To my surprise, the code compiles and behaves correctly even if I remove the extern "C"
. Version of zlib is 1.2.13.
Why does the linkage works even without the extern "C"
?
CodePudding user response:
If you had looked at zlib.h, you would have found very near the top:
#ifdef __cplusplus
extern "C" {
#endif
(and a corresponding close brace near the bottom).