Skip to content

Commit

Permalink
szip: Fix example compile with size_t and extern "C" (cpp-pm#521)
Browse files Browse the repository at this point in the history
szlib is compiled as a pure C static library. So there is no C++
name-mangling done. When including the `szlib.h` header into a C++
project the compiler assumes the header to be a C++ header and does
name-mangling. This results in linker errors when using szip-functions.

The second problem is the usage of `size_t` inside `szlib.h` whithout a
`stddef.h` include. Probably some other include provided the `size_t` in
the past, but maybe the stdlib changed and this missing include is
surfacing now.
  • Loading branch information
NeroBurner authored Feb 23, 2022
1 parent 869d135 commit 129b650
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
2 changes: 1 addition & 1 deletion examples/szip/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ hunter_add_package(szip)
find_package(szip REQUIRED)

add_executable(foo foo.cpp)
target_link_libraries(foo szip::szip)
target_link_libraries(foo PRIVATE szip::szip)
10 changes: 9 additions & 1 deletion examples/szip/foo.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
#include <cstddef> // szlib.h uses size_t, but doesn't include stddef.h, workaround that
extern "C" {
// szlib.h is a C-library, but the header doesn't handle C++, so do it for the lib
// otherwise the functions (for example SZ_encoder_enabled()) will not be found
#include <szlib.h>
}
#include <iostream>

int main()
{
std::cout << "SZLIB_VERSION: " << SZLIB_VERSION << std::endl;
std::cout << "SZ_encoder_eabled: " << SZ_encoder_enabled() << std::endl;
return 0;
}
}

0 comments on commit 129b650

Please sign in to comment.