繁体   English   中英

跨不同编译器的C ++库

[英]C++ library across different compilers

我正在使用MinGW(4.8.0 dw2 posix)编写C ++库。 该库在使用另一个编译器(在此情况下为msvc)的另一个C ++项目中使用。

参照此,我正在重新设计我的C ++库。 我不知道该怎么办两件事:

  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”。 我能做什么?

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

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

这是您的情况的一个例子

首先在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中实现该类

#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中的代码

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

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

这是使用g ++构建库的方式

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

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

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

如果您具有cppcomponents,请用路径替换Source\\Repose\\cppcomponents cppcomponents

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM