在C中创建有效的共享库 [英] Create a valid shared library in C
问题描述
我正在做一些测试,以学习如何创建共享库。
Code :: Blocks中共享库的模板是这个
I'm doing some test to learn how to create shared library. The template for shared libraries in Code::Blocks is this
library.c
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 64bit
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 :
我在Windows 7 64bit上,在编译器的specs文件中写道:线程模型:win32,gcc版本3.4.5(mingw-vista特殊r3)
,我用作命令
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
在我使用的python中
in python i used
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从二进制程序包中删除,而不是在我的系统上编译它们。
i installed both python3.1 and mingw from the binary package and not compiling them on my system
编辑2:
阅读Marc答案后。
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;
}
编译选项
gcc -c _DBUILD_DLL main.c
gcc -shared -o library.dll main.o -Wl,--out-implib,liblibrary.a
具有gcc 4.5.2
仍然会遇到相同的错误。
with gcc 4.5.2 still get the same error..
推荐答案
我相信在Windows环境中,您需要使用 __ declspec
批注。下面介绍了如何创建共享库以及如何使用 __ declspec
:在MingW中创建DLL 。
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.
这篇关于在C中创建有效的共享库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!