0

.cpp ファイルと .h ファイルから静的リンクのスタンドアロン .exe ファイルを作成する必要があります。

m_pListAll()克服しなければならない唯一の障害は、2 つの .cpp ファイルから同じ関数を呼び出せるようにすることとmain.cppwindow.cpp(というクラスを定義することWindow) です。

唯一の問題は、(理由は不明ですが) #includem_peDO() を定義するヘッダー ファイルを 2 回定義できないことmain.cppwindow.cpp、1 回しか実行できないことです。これは、ヘッダー ファイルが「動的リンク」と呼ばれるものをHINSTANCE(間違った: 実際の理由は回答セクションにあります):

            //linking.h

            //  This is an extra header file for dynamic linking of the LabJackUD driver.
            //  support@labjack.com

            #ifdef  __cplusplus
            extern "C"
            {
            #endif 

            //For dynamic linking, we first define structures that have the same format
            //as the desired function prototype.
            typedef LJ_ERROR (CALLBACK *tListAll) (long, long, long *, long *, long *, double *);


            //Define a variable to hold a handle to the loaded DLL.
            HINSTANCE hDLLInstance;

            //Define variables for the UD functions.
            tListAll m_pListAll;

            #ifdef  __cplusplus
            } // extern C
            #endif

他にも関数はありますが、main.cpp と window.cpp で tListAll m_pListAll を使用したいとします。Qt プロジェクトには次のファイルが含まれています。

main.cpp //main code
window.h //header defining a class used in main
window.cpp //cpp thing defining that class's methods
peripheral.h //header file to control my peripheral device
peripheral.lib //library file to control my peripheral device (VC6 only not minGW?!)
linking.h //thing that solves(?) the minGW/VC6 library incompatibility (read on)

Scenario 1) #include <linking.h> in **only** main.cpp
Outcome: m_pListAll() only in scope of main.cpp, out of scope for window.cpp
Scenario 2) #include <linking.h> in **both** main.cpp AND window.cpp
Outcome: Redefinition errors for redefining hDLLInstance and m_pListAll().

なぜ私はこの奇妙な HINSTANCE を使用しているのですか? 私の.libファイルがminGWと互換性がないことに関係があります。ライブラリをコンパイルの一部として静的に追加すると、次のようになります。 EXE'。止まる。

私は何をすべきか?その関数をwindow.cppのスコープに入れたいだけですが、エラーのためにそのヘッダーを2回使用したくありません。

4

1 に答える 1

0

あなたの問題は、単に宣言するのではなく、ヘッダーでandを定義 したことです。したがって、このヘッダーを含むすべての翻訳単位は、これらの変数のそれぞれの公開実装を作成し、それらはすべてリンク時に衝突します。hDLLInstancem_pListAll

externヘッダー内のこれらの各定義にキーワードを追加して宣言に変換し、元の定義 (つまり、キーワードなし)を翻訳単位の1 つだけコピーする必要があります (おそらく)。リンク時に定義された実装は 1 つだけです。したがって、ヘッダーには次のものがあります。externmain.cpp

//Declare a variable to hold a handle to the loaded DLL.
extern HINSTANCE hDLLInstance;

//Declare variables for the UD functions.
extern tListAll m_pListAll;

および ではmain.cpp、(例):

//Define a variable to hold a handle to the loaded DLL.
HINSTANCE hDLLInstance;

//Define variables for the UD functions.
tListAll m_pListAll;
于 2015-04-14T06:39:45.130 に答える