[ 
https://issues.apache.org/jira/browse/AVRO-2598?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17400160#comment-17400160
 ] 

Alexander Puchmayr commented on AVRO-2598:
------------------------------------------

The problem seems to be a fundamental one, especially when you're using avrocpp 
as library installed by some system maintainer (usually with defaults) and your 
project requires a certain standard different to the one the library was 
compiled with.

Consider, someone built libavrocpp.a with c++11 or 14, and in your project's 
CMakeLists.txt you require C++17. The result is that the library is using 
boost::any while your code is using std::any, resulting in the segfaults the 
original reporter mentioned.

A quick workaround is to ignore any system installed library of avrocpp, embed 
the source into your project and complie it with the same settings.

The clean way would be:
* Replace each and every check for c++17 when using class any by a #define 
USE_BOOST_ANY
* When compiling the system library, add a proper compiler definition whether 
BOOST or STD as been used to some package config (pkconfig, cmake)
* Projects using this correctly would then automatically get the correct 
USE_BOOST_ANY=1 or 0 and hence produce correct and working code, no matter what 
c++ standard the project actually uses.

> C++ standard of library implies C++ standard of projects using Avro
> -------------------------------------------------------------------
>
>                 Key: AVRO-2598
>                 URL: https://issues.apache.org/jira/browse/AVRO-2598
>             Project: Apache Avro
>          Issue Type: Bug
>          Components: c++
>    Affects Versions: 1.9.0, 1.9.1
>            Reporter: Marcel Pfütze
>            Priority: Major
>
> SInce Avro 1.9.0 there is an if macro in a lot of headers that uses the 
> current C++ standard.
>  If you build the library from source and use it in another project with a 
> different C++ standard this can lead to segfaults.
> Example of macro:
> {code:c++}
> template<typename T> T& GenericDatum::value() {
>     return (type_ == AVRO_UNION) ?
> #if __cplusplus >= 201703L
>         std::any_cast<GenericUnion>(&value_)->datum().value<T>() :
>         *std::any_cast<T>(&value_);
> #else
>         boost::any_cast<GenericUnion>(&value_)->datum().value<T>() :
>         *boost::any_cast<T>(&value_);
> #endif
> {code}
> In our case we build the library from source (which uses the c+11 by default) 
> and used it in a project using C+17. There's very little indication to the 
> user why the crash happens.
> My proposals:
>  * Move implementation from header to source file so that the used standard 
> is decided at build time.
>  * Use the [CMAKE_CXX_STANDARD 
> |https://cmake.org/cmake/help/v3.1/variable/CMAKE_CXX_STANDARD.html] 
> functionality provided by cmake to set the standard. In this way the standard 
> can also easily set when building the code without actually manipulating the 
> CMakeLists.txt file. Of course the --std=c++11 
> [here|https://github.com/apache/avro/blob/89218262cde62e98fcb3778b86cd3f03056c54f3/lang/c%2B%2B/CMakeLists.txt#L55]
>  can be removed.



--
This message was sent by Atlassian Jira
(v8.3.4#803005)

Reply via email to