template with C linkage [Error]



  • Hi,

    I am linking C library, So I have included below in my header file.

    ifdef __cplusplus
    extern "C" {
    #endif
    
    

    Now, I am getting an error that says "template with C linkage". On clicking this error, it takes me to Q_OBJECT inside the class.

    Not sure about this error. Any helps?

    Thanks,
    Kumara



  • Hi,

    If you use this header, you're defining a header that can be included in C source code; so no C++ inside.

    you don't need to specify that in a C++ header



  • @mcosta

    ifdef __cplusplus
    extern "C" {
    #include "db.h"
    #endif
    

    The use case is that, db.h is a C library. I want to include C library in my C++ code. I think, I should be using extern C to do that?

    However, even with extern C, I am getting C compatibility related issues.



  • AFAIK you need the

    #ifdef __cplusplus
    extern "C" {
    #endif
    

    stuff only if you want to ensure that the code enclosed by extern "C" is pure C, especially if you're using a C++ compiler. You can include as many pure C headers as you want in your C++ code without doing anything special.

    In your example, you're getting in trouble because your own code (which contain templates) is enclosed by extern "C", and pure C has no support of templates.


  • Moderators

    Well, you should change it:

    ifdef __cplusplus
    extern "C" {
    #include "db.h"
    } // Add this
    #endif
    

    Else all your C++ stuff will be inside Extern "C"!

    And if you do it in C++ code you do not have to check whether it is C++ or not, just do:

    extern "C" {
    #include "db.h"
    }
    


  • @jsulm That's correct. I was suppose to close it immediately after including C header.

    But I am still getting error when I do this

    extern "C" {
    #include "db.h"
    }

    Here is the error list

    /usr/include/c++/4.6/bits/cpp_type_traits.h:73:3: error: template with C linkage
    /usr/include/c++/4.6/bits/cpp_type_traits.h:86:3: error: template with C linkage
    /usr/include/c++/4.6/bits/cpp_type_traits.h:90:3: error: template specialization with C linkage
    /usr/include/c++/4.6/bits/cpp_type_traits.h:96:3: error: template with C linkage
    /usr/include/c++/4.6/bits/cpp_type_traits.h:104:3: error: template with C linkage
    /usr/include/c++/4.6/bits/cpp_type_traits.h:111:3: error: template with C linkage
    /usr/include/c++/4.6/bits/cpp_type_traits.h:119:3: error: template with C linkage
    /usr/include/c++/4.6/bits/cpp_type_traits.h:126:3: error: template specialization with C linkage
    /usr/include/c++/4.6/bits/cpp_type_traits.h:136:3: error: template with C linkage
    /usr/include/c++/4.6/bits/cpp_type_traits.h:146:3: error: template specialization with C linkage
    /usr/include/c++/4.6/bits/cpp_type_traits.h:153:3: error: template specialization with C linkage
    /usr/include/c++/4.6/bits/cpp_type_traits.h:160:3: error: template specialization with C linkage
    /usr/include/c++/4.6/bits/cpp_type_traits.h:167:3: error: template specialization with C linkage
    /usr/include/c++/4.6/bits/cpp_type_traits.h:175:3: error: template specialization with C linkage
    /usr/include/c++/4.6/bits/cpp_type_traits.h:199:3: error: template specialization with C linkage
    /usr/include/c++/4.6/bits/cpp_type_traits.h:206:3: error: template specialization with C linkage
    /usr/include/c++/4.6/bits/cpp_type_traits.h:213:3: error: template specialization with C linkage
    /usr/include/c++/4.6/bits/cpp_type_traits.h:220:3: error: template specialization with C linkage
    /usr/include/c++/4.6/bits/cpp_type_traits.h:227:3: error: template specialization with C linkage
    /usr/include/c++/4.6/bits/cpp_type_traits.h:234:3: error: template specialization with C linkage
    /usr/include/c++/4.6/bits/cpp_type_traits.h:241:3: error: template specialization with C linkage
    /usr/include/c++/4.6/bits/cpp_type_traits.h:248:3: error: template specialization with C linkage
    /usr/include/c++/4.6/bits/cpp_type_traits.h:258:3: error: template with C linkage
    /usr/include/c++/4.6/bits/cpp_type_traits.h:266:3: error: template specialization with C linkage
    /usr/include/c++/4.6/bits/cpp_type_traits.h:273:3: error: template specialization with C linkage
    /usr/include/c++/4.6/bits/cpp_type_traits.h:280:3: error: template specialization with C linkage
    /usr/include/c++/4.6/bits/cpp_type_traits.h:290:3: error: template with C linkage
    /usr/include/c++/4.6/bits/cpp_type_traits.h:297:3: error: template with C linkage
    /usr/include/c++/4.6/bits/cpp_type_traits.h:307:3: error: template with C linkage
    /usr/include/c++/4.6/bits/cpp_type_traits.h:314:3: error: template with C linkage
    /usr/include/c++/4.6/bits/cpp_type_traits.h:325:3: error: template with C linkage
    /usr/include/c++/4.6/bits/cpp_type_traits.h:333:3: error: template with C linkage
    /usr/include/c++/4.6/bits/cpp_type_traits.h:341:3: error: template with C linkage
    /usr/include/c++/4.6/bits/cpp_type_traits.h:349:3: error: template with C linkage
    /usr/include/c++/4.6/bits/cpp_type_traits.h:356:3: error: template specialization with C linkage
    /usr/include/c++/4.6/bits/cpp_type_traits.h:364:3: error: template specialization with C linkage
    /usr/include/c++/4.6/bits/cpp_type_traits.h:372:3: error: template with C linkage
    /usr/include/c++/4.6/bits/cpp_type_traits.h:379:3: error: template specialization with C linkage
    /usr/include/c++/4.6/bits/cpp_type_traits.h:386:3: error: template specialization with C linkage
    /usr/include/c++/4.6/bits/cpp_type_traits.h:393:3: error: template specialization with C linkage
    /usr/include/c++/4.6/bits/cpp_type_traits.h:403:3: error: template with C linkage
    

  • Moderators

    What is in db.h?
    Is it really pure C?
    And what does db.h include (cpp_type_traits.h?)?



  • Hi,

    as I said in my previous post you can use extern "C" only if your header file does not contain C++ code.
    It must be used to use C code inside C++



  • @jsulm To explain about my db.h,

    db.h is an interface, containing set of APIs. And it's a C code.
    db.h interfaces C++ code (odb.h)which written for accessing database.

    C++ code, gets accessed in C library.
    And in my C++ code and using that C library.

    (Sorry if I confused you)

    In short, db.h is a C header file and I am linking C library (db.a) in my project file.



  • @mcosta Thanks!
    My db.h contains C code, basically a set of global functions acting as an interface.
    It does not contain any C++ code, but it invokes C++ code.

    Example -
    In db.h

    IfcFunc()
    {
    // C Code
       OtherFunc();
    }
    

    In odb.h

    OtherFunc()
    {
    // C++ code
    }
    

    I am linking db library and including the header file.



  • I have removed extern "C" { } now.
    Because, db.h has extern "C" in it.

    After removing that, I don't get template with C linkage error.

    But when I use an API in my code, which comes from the library, it says, undefined reference.

    I am linking as

    LIBS += -L$(PATH)/lib -ldb
    

    Am I missing something?



  • Seems to me that OtherFunc() has C++ linkage, you cannot mention it in a C compatible header file. If you need to access C++ from a C library that has a C compatible header then you must only access the C++ code from within the implementation files of the C library.

    Do some research on how C++ deals with function overloading with name mangling and you should then understand how C++ linkage differs from C linkage and why symbols with C++ linkage cannot be in C compatible header files.



  • I could get rid of issue. I did not want extern C

    Reason is that, my code is accessing a library and C compatibility is taken care in the library.

    Here is a bird eye view -
    myLibrary->linking libdb

    I do have a different problem now. That is,
    I have an application that is linking myLibrary which is accessing libdb

    MyApp->linkingmyLibrary->linking libdb

    I am getting 'undefined reference to' when my Library calls an API from libDB.

    I see it as a linkage order issue. Is there any thoughts on this?

    Thank you,
    Kumara



  • Hello all,

    Problem is solved.

    Issue is that, I was accessing a library, and that link multiple libraries.

    As an application point of view, I should be linking those libraries as well, which I was missing to do.
    By linking those dependencies as well, I could build my application successfully.

    Thanks,
    Kumara


Log in to reply
 

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