Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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



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



  • 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>
    }
    @



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



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



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



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


Log in to reply