跨不同编译器的C ++库 [英] C++ library across different compilers

查看:128
本文介绍了跨不同编译器的C ++库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用MinGW(4.8.0 dw2 posix)编写C ++库。

I'm writing a C++ library using MinGW (4.8.0 dw2 posix). This library is used in another C++ project that use another compiler( in this case msvc ).

此库在使用另一个编译器的另一个C ++项目中使用。引用我正在重新设计我的C ++库。我不知道该怎么办两件事:

Refering to this I was redesign my C++ library. There are two things that I don't know how can I do:


  1. 我可以使用名称空间吗?

  2. 我注意到MinGW上的time_t是32位,而在msvc中是64位。我该怎么办?

1)

这是否破坏了ABI:

// Window.h

// MYLIB_API defined as __declspec( dllexports )
// MYLIB_CALL defined as __stdcall

namespace mylib {

class Window {
public:
  virtual void MYLIB_CALL destroy() = 0;
  virtual void MYLIB_CALL setTitle(const char* title) = 0;
  virtual const char* MYLIB_CALL getTitle() = 0;

  void operator delete(void* p) {
    if (p) {
      Window* w = static_cast<Window*>(p);
      w->destroy();
    }
  }
};

} // mylib

extern "C" MYLIB_API mylib::Window* MYLIB_CALL CreateWindow(const char* title);

2

如何确保不同编译器的基本类型相同。例如,在这种情况下, time_t 在MinGW上定义为 unsigned long 在msvc上定义为 __int64。我该怎么办?

How can I be sure that foundamental types are the same accross different compiler. For example in this case time_t is defined as unsigned long on MinGW and `__int64' on msvc. What can I do?

推荐答案

使用我的库cppcomponents在 https://github.com/jbandela/cppcomponents

Use my library cppcomponents at https://github.com/jbandela/cppcomponents

这是通过Visual C ++ 2013测试的(以前的版本没有在Windows上具有足够的c ++ 11支持)和mingw gcc 4.7+。它是在boost许可下发布的仅标头库。这使您可以在整个编译器中使用c ++ 11功能,例如std :: string,vector,tuple,pair,time_point。我很乐意回答您有关如何将其用于特定案例的任何问题。

This is tested with Visual C++ 2013 (Previous versions did not have enough c++11 support) and mingw gcc 4.7+ on windows. It is a header only library released under the boost license. This allows you to use c++11 features like std::string, vector, tuple, pair, time_point across the compilers. I would be happy to answer any questions you have regarding how to use it for your particular case.

以下是您的案例的一个示例

Here is an example for your case

首先在Window.h中定义类。

First define the class in Window.h

#include <cppcomponents/cppcomponents.hpp>

namespace mylib{

    struct IWindow :cppcomponents::define_interface<
        cppcomponents::uuid<0x0d02ac9a, 0x4188, 0x48fc, 0x8054, 0xafe7252ec188 >>
    {
        std::string getTitle();
        void setTitle(std::string new_title);

        CPPCOMPONENTS_CONSTRUCT(IWindow, getTitle, setTitle);

    };

    inline std::string WindowID(){ return "windowlibdll!Window"; }
    typedef cppcomponents::runtime_class<WindowID, cppcomponents::object_interfaces<IWindow>> Window_t;
    typedef cppcomponents::use_runtime_class<Window_t> Window;

}

然后在WindowImp.cpp中实现该类

Then implement the class in WindowImp.cpp

#include "Window.h"


struct ImplementWindow:cppcomponents::implement_runtime_class<ImplementWindow,mylib::Window_t>
{
    std::string title_;
    ImplementWindow(){}
    std::string getTitle(){
        return title_;
    }
    void setTitle(std::string new_title){
        title_ = new_title;
    }

};

CPPCOMPONENTS_DEFINE_FACTORY()

最后使用UseWindow.cpp中的代码

Finally Use the code in UseWindow.cpp

#include "Window.h"
#include <iostream>

int main(){
    mylib::Window w;
    w.setTitle("my title");
    std::cout << w.getTitle();
}

以下是使用g ++构建库的方法

Here is how you build it the library using g++

g++ WindowImp.cpp -std=c++11 -shared -o windowlibdll.dll -I Source\Repos\cppcomponents

这是使用MSVC 2013使用程序进行构建的方式

And here is how you build with program using MSVC 2013

cl UseWindow.cpp /EHsc /I Source\Repos\cppcomponents

Source\Repose\cppcomponents 替换为您拥有cppcomponents的路径

Replace Source\Repose\cppcomponents with the path were you have cppcomponents

确保生成的结果Windowslibdll.dll和UseWindow.exe在同一目录中并运行UseWindow.exe

Make sure the resulting windowslibdll.dll and the UseWindow.exe are in the same directory and run UseWindow.exe

这篇关于跨不同编译器的C ++库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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