1

共有ライブラリの作成方法を学ぶためにいくつかのテストを行っています。Code::Blocks の共有ライブラリのテンプレートはこれです

library.c

// The functions contained in this file are pretty dummy
// and are included only as a placeholder. Nevertheless,
// they *will* get included in the shared library if you
// don't remove them :)
//
// Obviously, you 'll have to write yourself the super-duper
// functions to include in the resulting library...
// Also, it's not necessary to write every function in this file.
// Feel free to add more files in this project. They will be
// included in the resulting library.

// A function adding two integers and returning the result
int SampleAddInt(int i1, int i2)
{
    return i1 + i2;
}

// A function doing nothing ;)
void SampleFunction1()
{
    // insert code here
}

// A function always returning zero
int SampleFunction2()
{
    // insert code here

    return 0;
}

コンパイルしようとしましたが、エラーや警告なしでコンパイルされました。しかし、Python 3 で ctyped.cdll.LoadLibrary("library path.dll") (実際には C 関数のように動作するはずです) で使用しようとすると、有効な win32 アプリケーションではないと表示されました。python と code::blocks の両方が 32 ビットです (code:blocks は gcc でコンパイルされ、システムにインストールされているバージョンの mingw を使用しようとしましたが、ライブラリが見つからないというエラーが発生します)。 64ビット

何が問題なのか、または私が何か間違っているのか知っていますか?

EDIT1:私はWindows 7 64ビットを使用しています。コンパイラのスペックファイルには、「スレッドモデル:win32、gccバージョン3.4.5(mingw-vista special r3)」と書かれており、コマンドとして使用されています

gcc.exe -shared -o library.dll library.c

Pythonで私が使用した

from ctypes import *

lib = cdll.LoadLibrary("C:\\Users\\Francesco\\Desktop\\C programmi\\Python\\Ctypes DLL\\library.dll")

エラーは

WindowsError: [Error 193] %1 is not a valid Win32 application

バイナリパッケージからpython3.1とmingwの両方をインストールし、システムでコンパイルしていません

EDIT2:マークの回答を読んだ後。

main.h

#ifndef __MAIN_H__
#define __MAIN_H__

#include <windows.h>

#ifdef BUILD_DLL
    #define DLL_EXPORT __declspec(dllexport)
#else
    #define DLL_EXPORT __declspec(dllimport)
#endif


#ifdef __cplusplus
extern "C"
{
#endif

DLL_EXPORT int MySimpleSum(int A, int B);

#ifdef __cplusplus
}
#endif

#endif // __MAIN_H__

main.c

#include "main.h"

// a sample exported function
DLL_EXPORT int MySimpleSum(int A, int B)
{
    return A+B;
}

コンパイル オプション

gcc -c _DBUILD_DLL main.c
gcc -shared -o library.dll main.o -Wl,--out-implib,liblibrary.a

gcc 4.5.2 でも同じエラーが発生します。

4

1 に答える 1

1

__declspec注釈を使用する必要があるWindows環境を信じています。共有ライブラリの作成方法と使用方法については、MingW での DLL の作成 で__declspec説明しています。

于 2011-03-08T20:39:51.203 に答える