簡體   English   中英

從 Qt C++ 調用已編譯的 C 庫函數會產生意外行為

[英]Calling a compiled C library function from Qt C++ produces unexpected behaviour

我在 Windows 上,我有 2 個項目:一個是用 C 語言制作的,我已經將它編譯成一個 DLL 庫,另一個是 Qt C++ 項目,我將它與 C 庫鏈接。

當我過去嘗試將 C 庫與 C 代碼鏈接時,它工作正常。 但是當我嘗試將它與 C++ 代碼(在 Qt here 和一般的 Linux 上)鏈接時似乎有問題,即使我插入了包含 C 頭文件的extern "C"塊。

最奇怪的部分是在調試時,代碼做了一些……不應該發生的事情?

這是我正在調用的 C 代碼:

// Defined in a header file elsewhere...
typedef struct _nodeTrueProperties {
    bool isDefined;
    bool needFuncName;
    bool _mallocd;

    const DType *inputTypes;
    long numInputs;

    const DType *outputTypes;
    long numOutputs;
} NodeTrueProperties;

// The actual code...
NodeTrueProperties d_semantic_get_node_properties(struct _sheet *sheet,
                                                  const char *name,
                                                  size_t lineNum,
                                                  const char *funcName,
                                                  NameDefinition *definition) {
    VERBOSE(5, "Getting node properties of node %s on line %zu in %s...\n",
            name, lineNum, sheet->filePath)

    NodeTrueProperties out =
        (NodeTrueProperties){false, false, true, NULL, 0, NULL, 0};

    // Most cases use this to build up the output types.
    DType *outputTypes = NULL;

    // First, let's check if it's a special function like Start.
    // We do this now so we don't need to search all of our includes
    // for this function.
    if (strcmp(name, "Start") == 0) {

        // THIS IS THE SECTION THAT SHOULD AND DOES RUN!

        out.isDefined  = true;
        out.numOutputs = 1;

        outputTypes    = (DType *)d_malloc(sizeof(DType));
        outputTypes[0] = TYPE_EXECUTION;

        out.outputTypes = (const DType *)outputTypes;
    } else if (strcmp(name, "Return") == 0) {
        // ... stuff that shouldn't run...
    } // ...

    return out;
}

這是調用該函數的 Qt C++ 代碼:

#include "nodegraphicsitem.h"

#include <QPainter>

extern "C" {
    #include <dsemantic.h>
}

// ...

void NodeGraphicsItem::setupNode(Sheet *sheet, QString name, QPointF position) {
    _name = name;
    _position = position;
    _sheet = sheet;
    _size = STARTING_SIZE;

    // Set the QGraphicsItem flags.
    setFlags(ItemIsMovable | ItemIsSelectable);

    // Get the true properties of the node.
    NameDefinition nodeDefinition;
    QByteArray nameByteArray      = name.toLatin1();
    const char *nameCharArray     = nameByteArray.constData();

    // THIS IS THE CALL TO THE COMPILED C FUNCTION
    NodeTrueProperties properties =
        d_semantic_get_node_properties(sheet, nameCharArray, 1, NULL,
                                       &nodeDefinition);

    // THIS FUNCTION DOES SOME WHACKY STUFF
    d_semantic_free_true_properties(properties);

    // ...
}

一切都編譯正常,但它根本運行不正常:

根據運行的 if 語句(因為傳入的名稱是"Start" ),我希望我的NodeTrueProperties看起來像這樣: {true, false, true, NULL, 0, (pointer to DType array), 1} . 根據調試器{true, false, false, NULL, 1, NULL, 0}我實際得到的是{true, false, false, NULL, 1, NULL, 0}

調試器所說的值是什么

另一個非常奇怪的工件是 C 代碼中的 if 語句激活,即使根據調試器的條件,條件為假( trueProperties._mallocd == false ):

如果語句由於某種原因激活

我在Win32模式下使用CMake和VS2017將C項目編譯成DLL,我使用Qt Creator和MSVC2017 32bit kit編譯Qt C++項目。

試試這個格式: @ #include <dsemantic.h> @我希望這有助於解決您的問題。

由於 Igor Tandetnik 的評論,我找到了罪魁禍首:我在 C 庫中創建的bool (4 個字節 = int )與 C++ 程序中的bool類型(1 個字節)的大小不同,我認為最終改變了NodeTrueProperties程序和庫之間的結構也是如此。

作為參考,我在 C 中更改了我的bool定義:

/**
 * \enum bool
 * \brief A boolean.
 */
typedef enum {
    false, ///< `false`
    true   ///< `true`
} bool;

包括stdbool.h頭文件:

#include <stdbool.h>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM