Warning: C4251



  • Hey people,

    I get thollowing warnings.
    @
    1>c:\qt\qt-win-opensource-src-4.5.0\include\qtcore../../src/corelib/codecs/qtextcodec.h(106) : warning C4251: 'QTextCodec::ConverterState::flags' : class 'QFlags<Enum>' needs to have dll-interface to be used by clients of struct 'QTextCodec::ConverterState'
    1> with
    1> [
    1> Enum=QTextCodec::ConversionFlag
    1> ]
    1>c:\qt\qt-win-opensource-src-4.5.0\include\qtcore../../src/corelib/concurrent/qtconcurrentexception.h(101) : warning C4251: 'QtConcurrent::internal::ExceptionStore::exceptionHolder' : class 'QtConcurrent::internal::ExceptionHolder' needs to have dll-interface to be used by clients of class 'QtConcurrent::internal::ExceptionStore'
    1> c:\qt\qt-win-opensource-src-4.5.0\include\qtcore../../src/corelib/concurrent/qtconcurrentexception.h(83) : see declaration of 'QtConcurrent::internal::ExceptionHolder'
    1>c:\qt\qt-win-opensource-src-4.5.0\include\qtcore../../src/corelib/concurrent/qtconcurrentresultstore.h(98) : warning C4251: 'QtConcurrent::ResultIteratorBase::mapIterator' : class 'QMap<Key,T>::const_iterator' needs to have dll-interface to be used by clients of class 'QtConcurrent::ResultIteratorBase'
    1> with
    1> [
    1> Key=int,
    1> T=QtConcurrent::ResultItem
    1> ]
    1> c:\qt\qt-win-opensource-src-4.5.0\include\qtcore../../src/corelib/tools/qmap.h(268) : see declaration of 'QMap<Key,T>::const_iterator'
    1> with
    1> [
    1> Key=int,
    1> T=QtConcurrent::ResultItem
    1> ]
    1>c:\qt\qt-win-opensource-src-4.5.0\include\qtcore../../src/corelib/concurrent/qtconcurrentresultstore.h(146) : warning C4251: 'QtConcurrent::ResultStoreBase::m_results' : class 'QMap<Key,T>' needs to have dll-interface to be used by clients of class 'QtConcurrent::ResultStoreBase'
    1> with
    1> [
    1> Key=int,
    1> T=QtConcurrent::ResultItem
    1> ]
    1> // .... and so on
    @

    The reason is following: In one of my classes, I include #include "DataController.h", because I need it. Before I included that header, I had none of these warnings.
    I linked statically, but how else?

    Thanks in advise. Cheers
    Huck



  • One of your classes is exported. Everything aggregated in or used publicly by that class should also be exported.



  • Yeah. This warning can be a pain to get rid of especially when using template types. As Franzk says it is to do with ensuring that all types exposed in an API are actually exported.

    Sometimes you get this when using templates (just google on the error code to see lots of examples). In such cases you can disable the warning for the specific case by adding:

    @
    #if defined (_MSC_VER)
    #pragma warning(push)
    #pragma warning(disable:4251)
    #endif
    @

    at the top of the file that triggers the warning and

    @
    #if defined (_MSC_VER)
    #pragma warning(pop)
    #endif
    @

    at the bottom.

    Only use this trick to hide false positives though and make sure that you understand the issue

    There is an MSDN article on it too but this "article":http://www.unknownroad.com/rtfm/VisualStudio/warningC4251.html gives a more readable overview



  • How do I recognize which of my classes are exported? Is there a kinda keyword?

    I neither have used __declspec( dllexport ) nor __declspec( dllimport ).. I am quite unfamilliar with exporting classes anyway..



  • Ah hang on. These warnings are from Qt headers. Are you building an application or a library?

    Also, is there some specific reason why you are using such an old version of Qt? 4.5.0 is really old now. 4.7.3 is the latest.



  • I set: Project Properties --> Configuration Properties --> General --> Configuration Type --> Dynamic Libraly (.dll)

    (NEITHER application .exe NOR static .lib)

    Regarding the 4.5.0: The vendor of these libaries which I use told me that they encoded the path "C:\Qt\qt-win-opensource-src-4.5.0" hard in their libraries (I know bad programming style). I have complained that already and I hope they release time much more dynamic libraries ;)



  • OK. If you are making a library then you need to be mindful of exporting your classes otherwise nobody will be able to link against your library. Have a read of "this":http://doc.qt.nokia.com/latest/sharedlibrary.html



  • [quote author="huckfinn" date="1311248308"]
    I neither have used __declspec( dllexport ) nor __declspec( dllimport ).. I am quite unfamilliar with exporting classes anyway..[/quote]

    Q_DECL_IMPORT and Q_DECL_EXPORT are the portable versions of _declspec(...). In the Qt sources you will find Q<package>_EXPORT too, which will be replaced by the import / export declarations depeding on if you are importing or exporting.

    "How to create a library with Qt and use it in an application":http://developer.qt.nokia.com/wiki/How_to_create_a_library_with_Qt_and_use_it_in_an_application.



  • Thank you for the links.

    Question 1: I didn't get the sense of the syntax? When I want to create a library, then this library provides functionality to other processes (or libraries).
    *Q_DECL_EXPORT must be added to the declarations of symbols used when compiling a shared library.
    *Q_DECL_IMPORT must be added to the declarations of symbols used when compiling a client that uses the shared library.
    @
    #if defined TEST
    #define TEST_COMMON_DLLSPEC Q_DECL_EXPORT
    #else
    #define TEST_COMMON_DLLSPEC Q_DECL_IMPORT
    #endif
    @
    Why should I set the macro Q_DECL_IMPORT when I am not going to use that as a client? Or do I?

    Question 2: Does anybody know how to set @DEFINES += TEST@ in *.vcpro ?



  • [quote author="huckfinn" date="1311316759"]
    Question 1: I didn't get the sense of the syntax? When I want to create a library, then this library provides functionality to other processes (or libraries).
    *Q_DECL_EXPORT must be added to the declarations of symbols used when compiling a shared library.
    *Q_DECL_IMPORT must be added to the declarations of symbols used when compiling a client that uses the shared library.
    @
    #if defined TEST
    #define TEST_COMMON_DLLSPEC Q_DECL_EXPORT
    #else
    #define TEST_COMMON_DLLSPEC Q_DECL_IMPORT
    #endif
    @
    Why should I set the macro Q_DECL_IMPORT when I am not going to use that as a client? Or do I?
    [/quote]

    Import and export declarations are unfortunately compiler-specific. Q_DECL_IMPORT and Q_DECL_EXPORT are portable ones, which translate for example to __declspec(...) for MSVS at compile time.

    Shared libraries can be roughly imagined as classes. There are public members which are accessible from outside (symbols marked for export using Q_DECL_EXPORT) and private members (symbol not marked for export) which cannot be accessed from outside.

    In addition, the compiler usually generates a so called import library, which is basically a list of exported symbols and their relative addresses within the dynamically linked library. This enables you to reference symbols within the dynamically linked library at compile time, as the linker can use the import library to resolve symbols.

    However, symbols are "mangled":http://en.wikipedia.org/wiki/Name_mangling differently for import libraries. So if your application links against an import library instead of a static library the compiler needs to mangle symbols found in dynamically linked libraries (through the import library) differently. This is the purpose of Q_DECL_IMPORT, which instructs the compiler to use the mangling schema for import libraries.

    This would mean that you'll need two versions of header files when creating dynamically linked libraries. One which is used to compile the library iteself and declares all symbols to be exported, and one to be used to link against the library and declares all symbols to be imported. To avoid this the preprocessor is used to decide wheter to import or export at compile time.
    @
    #ifdef CURRENTLY_COMPILING_LIBRARY
    #define IMPORT_OR_EXPORT_SYMBOL Q_DECL_EXPORT
    #else
    #define IMPORT_OR_EXPORT_SYMBOL Q_DECL_IMPORT
    #endif
    ...
    class IMPORT_OR_EXPORT_SYMBOL LibraryClass { ... }
    ...
    library.pro: DEFINES += CURRENTLY_COMPILING_LIBRARY
    ...
    application.pro: DEFINES +=
    @

    Symbols found in dynamically linked libraries can be resolved at runtime too. This is what - for example - QPluginLoader does.

    [quote author="huckfinn" date="1311316759"]Question 2: Does anybody know how to set @DEFINES += TEST@ in *.vcpro ?[/quote]

    Should be somewhere Project, Properties, C++, Preprocessor



  • Super, thank you; that solved my warnings.

    I looked in Project Properties, C++, Preprocessor.. --> there was a macro listed "MyProject_EXPORTS"

    Then I just changed
    @
    #ifdef MyProject_EXPORTS
    #define CURRENT_DLG Q_DECL_EXPORT
    #else
    #define CURRENT_DLG Q_DECL_IMPORT #endif
    @
    for each header, which included Q_OBJECT.



  • Unfortunately, I have to open this thread.

    I got that warnings again due to minimal modifications to an existing class, which has no Q_OBJECT macro set.
    I just added a method, which in turn includes another class (just a few attributes and their corresponding getters).
    The project is compiled as an *.dll.

    When I comment that method and the #include macro, the warnings are gone.



  • Is the class you are pulling in via the #include also exported from the dll? Can you post a small example that reproduces the problem please? It's difficult to tell without seeing the code.



  • Yes, of course:

    The header of the included class:
    @
    #ifndef DataITEM_H
    #define DataITEM_H

    #include <QString>
    #include <SomeHeader.h>

    class DataItem {
    public:
    DataItem();
    DataItem(qint32 tId, QString tName, qint32 tCat, qint32 tCon);
    ~DataItem(void);

    qint32 getDataID() const;
    QString getDataName() const;
    qint32 getDataCat() const;
    qint32 getDataCon() const;

    private:
    qint32 DataID;
    QString DataName;
    qint32 DataCat;
    qint32 DataCon;
    };
    #endif
    @

    Its source:
    @
    #include "DataItem.h"

    DataItem::DataItem()
    {
    this->DataID = 0;
    this->DataCat = 0;
    this->DataName = "defName";
    this->DataCon = 0;
    }

    DataItem::DataItem(qint32 tId, QString tName, qint32 tCat, qint32 tCon)
    {
    this->DataID = tId;
    this->DataCat = tCat;
    this->DataName = tName;
    this->DataCon = tCon;
    }

    qint32 DataItem::getDataID() const
    {
    return this->DataID;
    }

    QString DataItem::getDataName() const
    {
    return this->DataName;
    }

    qint32 DataItem::getDataCat() const
    {
    return this->DataCat;
    }

    qint32 DataItem::getDataCon() const
    {
    return this->DataCon;
    }

    DataItem::~DataItem(void)
    {
    }
    @

    Yes my project contains this DataItem-Class, also other classes in this same project use this DataItem-Class..

    Thankyou.



  • And can you post the header of the class which does the #include of the above please? At this stage it looks as if you need to export the DataItem class from your dll too.



  • Yes, here you are:
    @
    #ifndef ModuleData_H
    #define ModuleData_H

    // inherits
    #include <QtCore/QThread>
    #include "QtDemoModule.h"

    // instantiates
    #include <QtGui/QStandardItemModel>
    #include "SignalCondition.h"
    #include <StateMachine.h>
    #include <QtCore/QWaitCondition>

    #include <QObject>
    #include <QList>
    #include <DataInfo.h>

    // here my stuff
    //#include "DataItem.h"

    class ModuleLoader;
    class DataHandler;
    class EventHandler;
    class ModuleDockWidget;
    class QModelIndex;

    class ModuleData
    : public QtDemoModule
    , public QThread
    {
    public:

        /// @brief Constructor
        ModuleData( void );
    
        /// @brief Destructor
        virtual ~ModuleData( void );
    
        /// @brief For getting singalton instance of this class.
        static ModuleData *GetInstance( void );
    
        /// @brief Called by the DLL interface on startup
        void Startup( ModuleLoader &rLoader );
    
        /// @brief Called by the DLL interface on shutdown
        void Shutdown( void );
    
    // inherited from EventChecker
    public:
    
        /// @brief handle incoming events like mouse movement
        virtual bool EventOccurred( quint32 eventID );
    
    // inherited from MsgChecker
    public:
    
        /// @brief Called on incoming message.
        /// This method is inherited from MsgChecker
        virtual void HandleMessage( const MsgHandler c_msg );
    
        /// @brief Called on runlevel changes.
        /// This method is inherited from MsgChecker
        virtual quint32 HandleRunlevel( const quint32 c_runlevel, const qint8 c_direction );  
    
        /// @brief Called by messenger to publish interfaces.
        /// This method is inherited from MsgChecker
        virtual void HandleInterface( const quint32 interfaceId, const quint32 version, const quint32 validRunlevel, void * const pObj );
    
    // inherited by QtDemoModule
    public:
    
        virtual QDockWidget * setup_gui( QWidget *pParent );
    
        /// @brief Implement this method to do things after started
        virtual void init( RadioScannerInterface * );
    
        /// @brief this method will be called before the Crunner will be shut down
        virtual void deinit( void );
    

    /// Display Data currently on display if visible
    void displayData(qint32 DataType, qint32 xData, qint32 yData);

    /// Draw Edge via its id
    void drawEdge(qint32 edgeId);

    /// Clear Edges
    void clearEdges();

    // calc field to active Data - outcomment by my side
    //void calc_field_to_Data(const DataItem * tPI);
    //bool set_m_listfieldCalcDatas(const DataItem * tPI);

    // Prepare and stop calculation
    bool prepareCalculation();
    bool field_simulation_active();
    void stop_field_simulation();

    private:
    
        /// @brief does the actual reading of the Data details
        virtual void run( void );
    
        /// @brief
        void loadCategoryNames( void );
    
        /// @brief
        void importDatas( void );
    
    private:
    
        /// different Data trees you can address
        enum DataTreeMode 
        {
            GENERIC = 0,
            PREFERRED_DATAS = 1,
            DATAS_ALONG_THE_FIELD = 2,
            DATAS_ON_THE_FIELD = 3
        };
    
        /// Data search result item
        struct DataSearchResult 
        {
            QString m_DataName;
            int     m_DataType;
        };
    

    /// Categorie by name, mapped to its CAT ID
    QMap< int, QString > m_categoryNameByID;

    /// Preparation Ordered edges according to current field
    void prepare_extraction_current_fields_edgeIDs();
    /// Extraction Ordered edges according to current field
    void extract_current_fields_edgeIDs();

    ///
    QList<DFieldInfo> m_listfieldCalcDatas; ///< List of field calculation positions.

        /// SignalConditions
    

    SignalCondition m_signalfieldListExtraction; ///< Signal to wait for result of event GET_FIELD_LIST.
    SignalCondition m_signalfieldReset; ///< Signal to wait for result of event GET_LIST_RESET.

        bool m_fieldCalcSuccess; ///< Flag to represent field calculation was successful or not.
    

    bool m_fieldSimulationActive; ///< Flag to represent that field calculation is active or not

        /// Datanter to this module's main dock window 
        CDataDockWidget *m_pDockWidget; 
    
        /// interface to the data pool.
        DataHandler *m_pDataHandler;
    
        ///  interface to the event system. 
        EventHandler *m_pEventHandler; 
    
        ///  interface to the RF
        Crunner *m_pCrunnerRF; 
    
        /// current runlevel direction will be stored 
        qint8 m_direction;
    
        /// the mode the map was rendered
        qint32 m_renderMode;
    
        /// this will store the enabled/disabled state for each Data category
        QStandardItemModel m_model;
    
        QWaitCondition m_condition_Data_query;
    
        QMutex m_condition_Data_query_mutex;
    
        /// flag indicating that the query thread has to be stopped
        bool m_cancel;
    
        //the resolution of the drawn map
        qint32 m_mapResolution;
    
        //stores the Data search results
        QList<DataSearchResult> m_DataSearchResults;
    
        /// the size in pixels of the Data icon
        static const qint32 C_Data_ICON_SIZE;
    
    // static elements
    private:
    
        /// Singleton instance of this class
        static ModuleData *m_pInstance; 
    

    };
    #endif
    @

    Just including //#include "DataItem.h" without using it, causes the warnings above..



  • You don't seem to be exporting this class either. You need to export these classes from the dll in order to be able to use them in applications that link against it.



  • I've tried it, in vain:

    ModuleData.h
    @
    #include "DataItem.h"

    #if defined(SWITCH)

    define MODULE_DATA_EXP Q_DECL_EXPORT

    #else

    define MODULE_DATA_EXP Q_DECL_IMPORT

    #endif

    class ModuleLoader;
    class DataHandler;
    class EventHandler;
    class ModuleDockWidget;
    class QModelIndex;

    class MODULE_DATA_EXP ModuleData
    {
    //...
    @
    and in DataItem.h
    @
    #if defined(SWITCH)

    define DATA_ITEM_EXP Q_DECL_EXPORT

    #else

    define DATA_ITEM_EXP Q_DECL_IMPORT

    #endif

    class DATA_ITEM_EXP DataItem
    {
    //...
    @



  • Anybody any hints?



  • Try either:

    • Start a new shared library project and start adding your code to it until you see this error
    • Start stripping stuff out of your existing library until you don't see the error.

    Either way be sure to export the necessary classes from your shared library and import them into your application.

    Often making a small and simple test case that reproduces the problem makes it much easier to find the solution which you often do in the process of making the test case.



  • I started a new .dll with Qt Creator and there I have example.cpp and a example.h.
    Additionally a file named example_global.h has been automatically generated, where following is defined:
    @
    #if defined(DDDG_LIBRARY)

    define DDDGSHARED_EXPORT Q_DECL_EXPORT

    #else

    define DDDGSHARED_EXPORT Q_DECL_IMPORT

    #endif
    @
    and in the example.h there is:
    @
    #include "dddg_global.h"
    class DDDGSHARED_EXPORT Dddg {
    //...
    @

    In my project I do not have such a global header file. I entered this export stuff

    @
    #if defined(DDDG_LIBRARY)

    define DDDGSHARED_EXPORT Q_DECL_EXPORT

    #else

    define DDDGSHARED_EXPORT Q_DECL_IMPORT

    #endif
    @

    in each header where I got those warnings (each with different nomenclature).
    So I assume I misunderstood the export functionality. I need one global header with that Q_DECL_EXPORT stuff, an each other class-header needs a
    @
    #include "dddg_global.h"
    class DDDGSHARED_EXPORT Dddg {
    //...
    @
    ? When I create such a global header manually, how do I configure the properties settings, that the compiler recognize this header as such a global header? Or is this automatically when I include that in each header?
    Or this is regardless and both ways are possible?

    Cheers Huck



  • A header is a header is a header. There is nothing special about it apart from that it contains only the export/import macros.

    As you say, just include it in each class and add DDDGSHARED_EXPORT before each class that you wish (or need) to export and all should be good.

    I tend to name the macro something like DDDGSHARED_API so as not to fool myself into thinking that it always resolves to Q_DECL_EXPORT but that's just for the benefit of my poor little brain.

    As a side note, to enable symbol visibility with gcc you need to add

    @
    CONFIG += hide_symbols
    @

    to your .pro file. I'm not sure why this is not enabled by default for gcc nowadays.



  • Add CONFIG += hide_symbols to your .pro file?

    Unfortunately, I do not use gcc. It is the VC++ Compiler version 9 I think.



  • I was just mentioning it in case you ever decide to use GCC.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.