通用C ++库unsing Visual Studio 2010 [英] Generic C++ Library unsing Visual Studio 2010

查看:103
本文介绍了通用C ++库unsing Visual Studio 2010的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我正在开发一个具有客户端和服务器的临时应用程序。客户端使用C#编写为带有表单等的普通Windows应用程序。服务器将接收TCP / IP套接字消息以进行处理和响应。



我希望SERVER成为一个C ++公共代码,可以链接到Windows和Linux,只需更少的更改尽可能地,我可以在Windows和Linux机器上运行服务器。



所有常见的类和方法(由CLIENT和SERVER使用)都存储在一个可以链接到CLIENT和SERVER的COMMONLIBRARY。



我将它全部放入Visual Studio 2010解决方案中:



客户端 - 客户端C#代码。

服务器 - 服务器C ++代码。

我还创建了一个公共库作为DLL项目 - COMMONLIBRARY



我将库创建为将生成DLL的C ++ CLASS LIBRARY。所以代码看起来像:



Hi all,

I´m developing a from scratch application that will have a client and a server. The client is being written with C# as a normal Windows application with forms and so forth. The server will receive TCP/IP socket messages for processing and respond.

I want the SERVER to be a C++ common code to be linked into Windows and Linux with less changes as possible, so that I can run the server on both Windows and Linux machines.

All the common classes and methods (used by CLIENT and SERVER) are being stored on a COMMONLIBRARY that can be linked to both CLIENT and SERVER.

I put it all into a Visual Studio 2010 solution:

CLIENT - The client C# code.
SERVER - The server C++ code.
I had also created a common library as a DLL project - COMMONLIBRARY

I created the library as a C++ CLASS LIBRARY that will generate a DLL. So the code look likes:

#pragma once

using namespace System;

namespace COMMONLIBRARY {

#include <string>
	
	public ref class clsLog
	{
	private:
		//
		// Properties
		//
		string	 m_strCurLogUser;
	public:
		// Methods
		int clsLog::Init (std::string FileName)
	};
}


#include <string>
class Foo 
{  
   string m_strMessage;
}



字符串类型无法识别。当我将std :: string添加为:




The string type is not being recognized. When I add std::string as:

std::string m_strCurLogUser;



然后我收到C4368错误 - 不支持混合类型。



如果我关闭公共语言运行时以使用标准C ++语言(att项目 - >属性),代码没有编译,有几个错误和内部库。



有人可以给出一些暗示吗?



在该环境中定义和使用字符串的最佳选择是什么?



如何让SERVER尽可能多地通用并将其绑定到Visual Studio环境?



任何帮助赞。



Rds



门德斯


Then I´m getting C4368 error - mixed types are not supported.

If I turn off "Common Language Runtime" to use the standard C++ language (att Project->properties), the code doesn´t compile with several errors and the internal libraries.

Can someone gimme a hint of what´s going on ?

What´s the best option to define and use strings on that environment ?

How can I get the SERVER as much generic as possible and tying it to the Visual Studio environment ?

Any help appreciated.

Rds

Mendes

推荐答案

.NET 框架不是完全可移植的。您要么坚持使用标准的 C ++ ,要么使用 Mono框架 [ ^ ]。请注意,即使您选择标准 C ++ Windows Linux <的网络层也是如此/ code>完全不同。
.NET framework is not exactly portable. You have either to stick to standard C++ or use the Mono framework[^]. Note that, even if you choose standard C++, the networking layers of Windows and Linux are quite different.


C4368错误 - 不支持混合类型



您正在尝试尽管您可以在CLI类的成员函数中访问标准C ++类,但使用std C ++成员创建C ++ CLI类是不可能的。如果关闭对公共语言运行库的支持,则在尝试编译CLI类时会出现很多错误。



看来你正在尝试写一个。使用C ++的.NET库,但是你谈到希望库是可移植的。正如已经提到的,您可以使用Mono Framework执行此操作,但是,我假设您不希望在C ++库中依赖.NET但希望它是该平台的本机库你安装它。



如果你想要一个本地公共库,其中有网络功能,你需要提供一些网络层的抽象,如它们与CPallini提到的不同。有很多很好的跨平台网络库。 Qt [ ^ ]有一些和 ACE框架 [ ^ ]也有很多好的功能。
"C4368 error - mixed types are not supported"

You are trying to create a C++ CLI class with std C++ members, which is not possible, although you can access standard C++ classes form within member functions of a CLI class. If you turn off support for Common Language Runtime, you get lots of errors as you are trying to compile a CLI class.

It appears you are trying to write a .NET library with C++, but you talk about wanting the library to be portable. As has been mentioned, you may be able to do this with the Mono Framework, however, I am presuming that you don''t want to be dependent upon .NET in your C++ library but want it to be a native library for the platform you install it on.

If you want a native common library, with networking functionailty in it, you will need to provide some sort of abstraction of the network layers, as they are different as CPallini has mentioned. There are plenty of good cross platform networking libraries about. Qt[^] has some and the ACE framework[^] also has a lot of good features.


正如别人所说的那样,不要使用C ++ / CLI或任何.Net的东西如果你想要移植到Linux,除非你准备用C#/ Mono垃圾C ++。



除此之外我注意到了这个:



As mentioned by others don''t use C++/CLI or any .Net stuff if you want portability to Linux unless you''re prepared to junk C++ for C#/Mono.

Apart from that I noticed this:

namespace COMMONLIBRARY {
 
#include <string>
...
</string>





这是一个非常糟糕的主意,因为它试图将< string> 标题中的所有内容定义为 COMMONLIBRARY 名称空间。这可能看起来像一个简洁的想法,但相信我,它将无法奏效。在Windows环境中包含< string> 会带来数十个标题,包括 Windows.h 本身有效地重新定义了所有内容。命名空间内的Win32 API。

我会尝试在 COMMONLIBRARY 之外移动 #include 命名空间。





That''s a really bad idea as it tries to define everything in and included by the <string> header as being in the COMMONLIBRARY namespace. That might seem like a neat idea but believe me it will not work. Including <string> in a Windows environment brings in dozens of headers including Windows.h itself effectively redefining everything from the Win32 API inside the namespace.
I would try moving the #include outside the COMMONLIBRARY namespace.

#include <string>

namespace COMMONLIBRARY {
...
</string>


这篇关于通用C ++库unsing Visual Studio 2010的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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