Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. template with C linkage [Error]
Forum Updated to NodeBB v4.3 + New Features

template with C linkage [Error]

Scheduled Pinned Locked Moved Solved Mobile and Embedded
14 Posts 5 Posters 15.2k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M mcosta

    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

    K Offline
    K Offline
    kumararajas
    wrote on last edited by kumararajas
    #3

    @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.

    --Kumar

    1 Reply Last reply
    0
    • JohanSoloJ Offline
      JohanSoloJ Offline
      JohanSolo
      wrote on last edited by JohanSolo
      #4

      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.

      `They did not know it was impossible, so they did it.'
      -- Mark Twain

      1 Reply Last reply
      0
      • jsulmJ Online
        jsulmJ Online
        jsulm
        Lifetime Qt Champion
        wrote on last edited by jsulm
        #5

        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"
        }
        

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        K 1 Reply Last reply
        0
        • jsulmJ jsulm

          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"
          }
          
          K Offline
          K Offline
          kumararajas
          wrote on last edited by kumararajas
          #6

          @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
          

          --Kumar

          1 Reply Last reply
          0
          • jsulmJ Online
            jsulmJ Online
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #7

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

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            K 1 Reply Last reply
            0
            • M Offline
              M Offline
              mcosta
              wrote on last edited by
              #8

              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++

              Once your problem is solved don't forget to:

              • Mark the thread as SOLVED using the Topic Tool menu
              • Vote up the answer(s) that helped you to solve the issue

              You can embed images using (http://imgur.com/) or (http://postimage.org/)

              K 1 Reply Last reply
              0
              • jsulmJ jsulm

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

                K Offline
                K Offline
                kumararajas
                wrote on last edited by
                #9

                @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.

                --Kumar

                1 Reply Last reply
                0
                • M mcosta

                  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++

                  K Offline
                  K Offline
                  kumararajas
                  wrote on last edited by
                  #10

                  @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.

                  --Kumar

                  1 Reply Last reply
                  0
                  • K Offline
                    K Offline
                    kumararajas
                    wrote on last edited by
                    #11

                    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?

                    --Kumar

                    1 Reply Last reply
                    0
                    • B Offline
                      B Offline
                      bsomervi
                      wrote on last edited by bsomervi
                      #12

                      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.

                      1 Reply Last reply
                      0
                      • K Offline
                        K Offline
                        kumararajas
                        wrote on last edited by
                        #13

                        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

                        --Kumar

                        1 Reply Last reply
                        0
                        • K Offline
                          K Offline
                          kumararajas
                          wrote on last edited by
                          #14

                          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

                          --Kumar

                          1 Reply Last reply
                          0

                          • Login

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • Users
                          • Groups
                          • Search
                          • Get Qt Extensions
                          • Unsolved