在 C 中创建有效的共享库

发布于 2024-10-21 00:07:00 字数 2331 浏览 2 评论 0原文

我正在做一些测试来学习如何创建共享库。 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 的安装版本,但它给出了一些关于缺少库的错误),而我正在使用 win 7 64位

你知道问题是什么,或者我做错了什么吗?

编辑1: 我在Windows 7 64位上,在编译器的规格文件中写道:“线程模型:win32,gcc版本3.4.5(mingw-vista特殊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 仍然遇到同样的错误..

I'm doing some test to learn how to create shared library.
The template for shared libraries in Code::Blocks is this

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;
}

I tried to compile it, and it compiled without any error or warning. But when i tried to use it with the ctyped.cdll.LoadLibrary("library path.dll") in python 3(that actually should work like the C function), it said that it wasn't a valid win32 application. Both python and code::blocks are 32 bit (code:blocks compile with gcc, and i tryed to use an installed version of mingw on my system, but it gives some error about a missing library) while i'm working on win 7 64bit

Do you know what the problem can be, or if i'm doing something wrong?

EDIT1:
i'm on windows 7 64bit, in the specs file of the compiler is wrote: "Thread model: win32, gcc version 3.4.5 (mingw-vista special r3)"
and i used as command

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

in python i used

from ctypes import *

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

and the error was

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

i installed both python3.1 and mingw from the binary package and not compiling them on my system

EDIT2:
After reading Marc answer.

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;
}

compiling options

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

with gcc 4.5.2
still get the same error..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

萌面超妹 2024-10-28 00:07:00

我相信在Windows环境中你需要使用__declspec注释。如何创建共享库以及 __declspec 的使用如下所述:在 MingW 中创建 DLL< /a>.

I believe in the windows environment you need to use the __declspec annotation. How to create a shared library and the use of __declspec is described here: DLL Creation in MingW.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文