Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [solved]extern "C" works in a new project but does not work when including the C code in an existing project.

[solved]extern "C" works in a new project but does not work when including the C code in an existing project.

Scheduled Pinned Locked Moved General and Desktop
6 Posts 4 Posters 5.1k Views
  • 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.
  • K Offline
    K Offline
    kpkale
    wrote on 24 Apr 2014, 09:19 last edited by
    #1

    I am in process of writing a Graphics application in Qt C++.

    For adding Datamatrix encoding functionality, I am using the C code provided by iec16022 (Stefan Schmidt). It is available in public domain in form of a single C Code File.

    For testing purpose, I include this code in a new Qt Graphics Application. I enclose the complete code in extern "C" and test the functions in the code. Everything works perfect.
    However, when I try to use the same file with the extern "C" in my main Graphics application, it gives a lot of errors as if trying th compile the C code in C++.

    Could someone throw some light on what could be preventing the extern "C" from working?

    Thanks.

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mcosta
      wrote on 24 Apr 2014, 12:39 last edited by
      #2

      Please,

      post the working code and the non working one.

      As general rules, when you want use your C code in C++ files you should do something like this

      @
      extern "C" {
      #include <your_c_header>
      }
      @

      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/)

      1 Reply Last reply
      0
      • K Offline
        K Offline
        kpkale
        wrote on 24 Apr 2014, 14:19 last edited by
        #3

        Thanks for your reply.

        I understand I should be posting these codes.

        As regards the working code, I am doing the following.

        All the C Code is housed in one header file.
        Before the starting of that file I am putting...

        @
        #ifndef IEC16022_H
        #define IEC16022_H

        #ifdef __cplusplus
        extern "C" {
        #endif@

        and at the end of the file I am putting...

        @
        #ifdef __cplusplus
        }
        #endif
        #endif // IEC16022_H
        @

        For testing this, I created a new Qt Widgets Application (Using QtCreator 3.0.0, Based on Qt 5.2.0(MSVC 2010, 32 bit))
        In the MainWindow.cpp file, on pressing a push button, I am calling the main function in the C Header file which is supposed to process a data and give me a String.

        Giving the contents of the MainWindow.cpp for reference.
        @
        #include "mainwindow.h"
        #include "ui_mainwindow.h"
        #include "iec16022.h"

        MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
        {
        ui->setupUi(this);
        }

        MainWindow::~MainWindow()
        {
        delete ui;
        }

        void MainWindow::on_pushButton_clicked()
        {
        int barcodelen = 0;
        char *barcode = 0;
        char *result = 0;
        QString barstr;

        barcode = ui->lineEdit->text().toUtf8().data();
        barcodelen = strlen(barcode);
        
        barstr = QString::fromUtf8(iec16022(barcode, barcodelen));
        
        ui->textBrowser->setText(barstr.left(barstr.indexOf("AB")+2));
        

        // Here the variable barstr saves the desired result.
        }
        @
        This works perfect.

        After this OK testing, I then decided to implement the same code in my main Graphics Application.
        In one of the CPP Classes, in one of the slots, I am required to get the processed String from this same C Header.
        So logically, I just include the same C Header File (with all the extern C code already placed in it) just the way i included it for the testing new application and call the same function to get the processed String.
        The code in Testing application and the main application is exactly same.

        However, when I compile the main application, I get compilation errors on the C Header file as I was getting when I had not placed the C code inside the extern "C".

        The code in the main application is too huge to be included here. Just for basics, it is more like a 2D CAD application.

        What confuses me is when the code is working in a test application, what could be conflicting with the extern "C" when included in the main application.

        Hope I have been in a position to elaborate my question further.

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andreyc
          wrote on 24 Apr 2014, 14:49 last edited by
          #4

          What error messages do you have? Could you put some of them here, please.

          1 Reply Last reply
          0
          • K Offline
            K Offline
            kpkale
            wrote on 24 Apr 2014, 17:28 last edited by
            #5

            iec16022.h:72: error: 'int* log' redeclared as different kind of symbol
            static int *log = NULL, *alog = NULL, *rspoly = NULL;
            and some more errors on some of the other declarations.

            ................ Thanks for asking the question... just thought about it and tried adding the qmath.h to my testing program and i got the same errors.

            So seem to figure out that there was some variable with a name "log" which was clashing with the log of qmath.h
            Just changed the refractor and the solution went ahead smooth.

            Thanks both of you...

            1 Reply Last reply
            0
            • E Offline
              E Offline
              Eddy
              wrote on 24 Apr 2014, 18:59 last edited by
              #6

              Thanks for posting your solution.

              Could you please edit your first post and prepend [solved] to it? This informs others there is a solution here.

              Qt Certified Specialist
              www.edalsolutions.be

              1 Reply Last reply
              0

              4/6

              24 Apr 2014, 14:49

              • Login

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