compiler error on inclusion of string library



  • Hi all -

    When I use a "#include <string>" in a header file, I get the following compiler errors:

    In file included from c:\msys32\opt\xtensa-esp32-elf\xtensa-esp32-elf\include\c++\5.2.0\bits\stringfwd.h:40:0,
                     from c:\msys32\opt\xtensa-esp32-elf\xtensa-esp32-elf\include\c++\5.2.0\string:39,
                     from C:/esp32_projects/wifibutton/main/message.h:5,
                     from C:/esp32_projects/wifibutton/main/wifi_esp32.h:8,
                     from C:/esp32_projects/wifibutton/main/main.cpp:17:
    c:\msys32\opt\xtensa-esp32-elf\xtensa-esp32-elf\include\c++\5.2.0\bits\memoryfwd.h:63:3: error: template with C linkage
       template<typename>
       ^
    c:\msys32\opt\xtensa-esp32-elf\xtensa-esp32-elf\include\c++\5.2.0\bits\memoryfwd.h:66:3: error: template specialization with C linkage
       template<>
       ^
    

    The more I experiment with this, it seems the more confused I get. What am I doing wrong?

    Thanks...

    EDIT: fixed by eliminating unnecessary use of extern "C" declarations. Appropriate for main function, but not class definitions.


  • Lifetime Qt Champion

    Hi,

    When mixing C and C++, you usually need to wrap them like:

    #ifdef __cplusplus
    extern "C" {
    #endif
    
    /// Your stuff
    
    #ifdef __cplusplus
    }  //extern "C"
    #endif
    


  • Yes, and therein lies the dilemma - the extern "C" turns off the function decoration that template function names require. I'd gone through and used extern "C" on all my classes, and that got me into trouble. Took me a while to discover the problem. The solution is a combination of parsimonious usage of the extern "C" along with thorough planning for use of the STL within the application.

    Unfortunately, I had done neither.


  • Moderators

    @mzimmers said in compiler error on inclusion of string library:

    Yes, and therein lies the dilemma - the extern "C" turns off the function decoration that template function names require. I'd gone through and used extern "C" on all my classes, and that got me into trouble.

    That's because C does not support templates (or classes!). The API wrapped in extern "C" must be C-compatible.

    There is a way (I'd call it a hack) to add member functions to extern "C" structs though: https://stackoverflow.com/a/1950150/1144539


Log in to reply
 

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