我目前正在研究arm-unknown-linux-gnueabi,并将编译器从gcc4.9.2降级到gcc4.4.1。
我使用crosstool-ng用gcc4.4.1。编译编译器。众所周知,gcc4.9.2比gcc4.4.1支持更多的c++ 11功能。
使用4.9.2进行的代码编译没有错误,但是面对4.4.1的错误。
以下是我不明白为什么的错误?
代码是

#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <list>
#include <cassert>
#include <dlfcn.h>
#include <cstring>
#include <stdexcept>

class dl_loader {
    // Dl loader
    dl_loader( dl_loader& ) = delete;
    dl_loader& operator=( dl_loader& ) = delete;
    dl_loader() {
    }
public:
    ~dl_loader() {
        if (_instance_) {
            delete _instance_; _instance_ = NULL;
        }
    }
public:
    static dl_loader * get_instance();
    //Resolve name by svc_net.so.1:name
    template <typename Func, typename ...Args>
    typename std::function<Func>::result_type call( const char* name, Args && ...args )
    {
        Func* fptr = reinterpret_cast<Func*>( find_or_load_symbol( name ) );
        assert(fptr);
        return fptr( args ... );
    }
    template <typename Func>
    typename std::function<Func>::result_type call( const char* name )
    {
        Func* fptr = reinterpret_cast<Func*>( find_or_load_symbol( name ) );
        assert(fptr);
        return fptr( );
    }
private:
    //! Find or load symbol from the dynamic library
    void* find_or_load_symbol( const char *name );
private:
    std::map<std::string, void*> m_funcs;   //Function loaded names
**//    using lib_ptr_t = std::unique_ptr<void,std::function<void(void*)>>;  IQ:b/c gcc4.4.1 doesn't support aliases keyword "using"
    typedef std::unique_ptr<void ,std::function<void(void* )>>  lib_ptr_t;**
    std::map<std::string, lib_ptr_t> m_libs;    //Libs loaded
    static dl_loader * _instance_;
};

/! Find or load symbol from the dynamic library
void* dl_loader::find_or_load_symbol( const char *name )
{
    const auto f_it = m_funcs.find( name );
    if( f_it == m_funcs.end() )
    {
        const auto sep = std::strstr( name, ":" );
        if( !sep )
        {
            throw std::logic_error("Invalid syntax token : not found");
        }
        const auto name_len = sep - name;
        char library_name[ name_len + sizeof('\0') ];
        std::memcpy( library_name, name, name_len );
        library_name[name_len] = '\0';
        const auto symbol_name = sep + 1;
        const auto l_it = m_libs.find( library_name );
        void* lib_hwnd {};
        if( l_it == m_libs.end() )
        {
            const auto handle = dlopen( library_name, RTLD_LAZY );
            if( !handle )
            {
                throw std::logic_error( std::string("Unable to find library: ") +
                        library_name );
            }

//          m_libs[ library_name ] = lib_ptr_t ( handle, [](void*d){ dlclose(d); } );


            lib_hwnd = handle;
        }
        else
        {
            lib_hwnd = l_it->second.get();
        }
        //Try to open function name from library
        const auto fn_ptr = dlsym( lib_hwnd, symbol_name );
        if( fn_ptr )
        {
            m_funcs[ name ] = fn_ptr;
            return fn_ptr;
        }
        else
        {
            throw  std::logic_error( std::string("Unable to find symbol: ") +
                symbol_name + " in library: " +  library_name );
        }
    }
    else
    {
        return f_it->second;
    }
    return NULL;
}

提前致谢

错误是
find_or_load_symbol(const char*)':
dynlib_loader.cpp:67: error: expected primary-expression before '(' token
dynlib_loader.cpp:68: error: expected primary-expression before '[' token
dynlib_loader.cpp:68: error: expected primary-expression before ']' token
dynlib_loader.cpp:68: error: expected primary-expression before 'void'
dynlib_loader.cpp:69: error: expected ')' before '{' token
dynlib_loader.cpp:69: error: expected ';' before ')' token

最佳答案

您正在使用C++ 11特定的代码,如果我没记错的话,(部分)支持C++ 11的第一个gcc版本是4.7。编译器在使用void * lib_hwnd {};时遇到麻烦,统一初始化为C++ 11。将其更改为void* lib_hwnd = NULL;并检查您的代码中是否有其他C++ 11特定的东西。

关于c++ - 将交叉编译器从gcc4.9.2降级到gcc4.4.1后,源代码中出现错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47054807/

10-11 22:46