创建一个可移植库在Linux和Windows上运行 [英] Creating a portable library to run on both linux and windows

查看:223
本文介绍了创建一个可移植库在Linux和Windows上运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

gcc (GCC) 4.7.2

您好,

我创建一个共享库,编译Linux上,这将在编译使用相同的源$ C ​​$ C窗户的DLL。所以我创造了Linux和Windows的便携库。

I am creating a shared library that will compile on linux and a dll that will compile on windows using the same source code. So i am creating an portable library for both linux and windows.

在该库我的头文件是这样的module.h中,即

In my header file for the library is this i.e. module.h

#ifdef __cplusplus
extern "C" {
#endif

#ifdef _WIN32
#define LIB_INTERFACE(type) EXTERN_C __declspec(dllexport) type
#else
#define LIB_INTERFACE(type) type
#endif

LIB_INTERFACE(int) module_init();

#ifdef __cplusplus
}
#endif

在源我有以下即的module.c

In the source I have the following i.e. module.c

#include "module.h"

LIB_INTERFACE(int) module_init()
{
    /* do something useful
    return 0;
}

在我的测试应用程序将连接并使用此module.so我有这样的:

And in my test application that will link and use this module.so I have this:

#include "module.h"

int main(void)
{
    if(module_init() != 0) {
    return -1;
    }
    return 0;
}

1)是上面我所要做的就是它一个正确的实施创造了Linux和Windows的便携式库?

1) Is what I have done above is it a correct implementation of creating a portable library for linux and windows?

2)我只是想知道,因为我已经在的externC包装的功能让这个库可以从已经在C ++编译程序被调用。我还需要这 EXTERN_C 在以下几点:

2) I am just wondering as I have wrapped the functions in extern "C" so that this library can been called from a program that has been compiled in C++. Do I still need this EXTERN_C in the following:

#define LIB_INTERFACE(type) EXTERN_C __declspec(dllexport) type

3)什么是的目的 EXTERN_C

在事先非常感谢,

推荐答案

这是导出一个DLL API for Windows和仍然支持Linux的一个典型方式:

This is a typical way to export a DLL API for Windows and still support Linux:

#ifdef __cplusplus
extern "C" {
#endif

#ifdef _WIN32
#  ifdef MODULE_API_EXPORTS
#    define MODULE_API __declspec(dllexport)
#  else
#    define MODULE_API __declspec(dllimport)
#  endif
#else
#  define MODULE_API
#endif

MODULE_API int module_init();

#ifdef __cplusplus
}
#endif

在DLL源:

#define MODULE_API_EXPORTS
#include "module.h"

MODULE_API int module_init()
{
    /* do something useful */
    return 0;
}

您的应用程序源代码是正确的。

Your application source is correct.

使用上述模型,在Windows DLL将导出的API,而应用程序将其导入。如果不是在Win32中, __ declspec 装修被删除。

Using the above model, on Windows the DLL will export the API while the application will import it. If not on Win32, the __declspec decoration is removed.

由于头包装在的externC整个界面,使用每个接口上的 EXTERN_C 宏不是必需的。 的externC是用来告诉链接器使用 C 键代替 C ++ <的/ code>。 C链接是跨编译器的标准,而C ++不是,限制使用的DLL与相同的编译器生成的应用程序。

Since the header wraps the entire interface in extern "C", using the EXTERN_C macro on each interface is not required. extern "C" is used to tell the linker to use C linkage instead of C++. C linkage is standard across compilers, whereas C++ is not, limiting the use of a DLL to application built with the same compiler.

有没有必要返回类型集成到该API宏

There is no need to integrate the return type into the API macro.

这篇关于创建一个可移植库在Linux和Windows上运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆