简体   繁体   English

C POSIX API是否与C ++ STL兼容

[英]Is C POSIX API compatible with C++ STL

I will be using the C POSIX API Library to learn about socket programming. 我将使用C POSIX API库来学习套接字编程。 I will be creating servers which will be listening for connections and clients which will connect to the server. 我将创建将监听连接的服务器和将连接到服务器的客户端。 On the server there is a text file which looks like this 在服务器上有一个文本文件,看起来像这样

Peter,Male,10
Mary,Female,20
Tim,Male,30
Shrek,Male,40

The server will read in the text file and store it in a data structure. 服务器将读取文本文件并将其存储在数据结构中。 I am wondering if there will be any compatibility problems with using a C++ STL like vector together with the C POSIX API to store all the text file information? 我想知道将C ++ STL(例如vector与C POSIX API一起使用来存储所有文本文件信息是否会出现兼容性问题?

Can someone advise me if I should use C struct or C++ STL? 有人可以建议我使用C struct还是C ++ STL?

You can call C functions from C++ programs. 您可以从C ++程序调用C函数。 To do this, you have to declare them as "extern C" so that the compiler knows how to call them. 为此,您必须将它们声明为“ extern C”,以便编译器知道如何调用它们。 But the good news is that your C library include files almost certainly have something like this in them: 但是好消息是,您的C库包含的文件几乎可以肯定是这样的:

#ifdef __cplusplus
extern "C" {
#endif

/* C callable stuff goes here */

#ifdef __cplusplus
}
#endif

so that if you include them from a C++ program, the functions are already declared as C functions. 因此,如果您从C ++程序中包含它们,则这些函数已被声明为C函数。

The arguments you pass to the C functions must be of the type which their declaration specifies - so you will need to use structs, pointers, etc as required. 传递给C函数的参数必须是其声明指定的类型-因此,您将需要根据需要使用结构,指针等。 However internally your program can make use of whatever STL or other C++ libraries you want to. 但是,您的程序可以在内部使用所需的任何STL或其他C ++库。

There are networking libraries available for C++ which would make it easier to use sockets from C++. C ++有可用的网络库,这将使使用C ++的套接字更加容易。 They are essentially wrappers around the POSIX functoins (or equivalent for other platforms). 它们实质上是POSIX函数(或其他平台的等效函数)的包装。 However, if you are trying to learn the POSIX APIs or want to do something not supported by these libraries then you are able to call the POSIX APIs directly. 但是,如果您尝试学习POSIX API或想做这些库不支持的操作,则可以直接调用POSIX API。

C++ has its own libraries much more suited to do networking tasks (Boost.Asio?), so this questions can only serve educational purpose. C ++拥有自己的库,非常适合执行网络任务(Boost.Asio?),因此,这些问题只能用于教育目的。

It is certainly possible to use C API from C++. 当然可以使用C ++中的C API。

Unfortunately doing so requires some knowledge about name mangling, C++ containers and the API being used. 不幸的是,这样做需要一些有关名称处理,C ++容器和所用API的知识。 As you asked this question you might have difficulties trying to learn both C++ and POSIX API. 当您问这个问题时,尝试学习C ++和POSIX API可能会遇到困难。

You can use a std::vector of struct or class to store the data. 您可以使用structclassstd::vector来存储数据。 Reading and writing from/to a file using C POSIX API shouldn't be a problem at all. 使用C POSIX API读写文件完全不是问题。

struct Record
{
   enum Gender {MALE, FEMALE};

   std::string name;
   Gender gender;
   int age;
}

std::vector<Record> records;

/* Read the data from the file using C POSIX API and store them in records */

/* Use the data from records and save them in file using C POSIX API */

POSIX is a C-API which often uses pointers to blocks of memory to be transferred or received. POSIX是一种C-API,通常使用指向要传输或接收的内存块的指针。 This works in C++ as long as you restrict your data to POD -types. 只要将数据限制为POD类型,此方法就可以在C ++中工作。 As soon as you transfer a struct with something like a std::string you will have undefined behavior. 一旦您传输带有std :: string之类的结构的文件,您将具有未定义的行为。 That means that you can't send or receive something like 这意味着您无法发送或接收类似

struct Person
{
    std::string name;
}

over the network without converting it to a POD before sending and then converting it back. 通过网络传输,而不必先将其转换为POD,然后再发送回。

What you basically need is a way to serialize your data structure/class in a defined way, so that you can then easily transmit it over a transmission line. 您基本上需要的是一种以定义的方式序列化数据结构/类的方法,以便您可以轻松地在传输线上进行传输。

This is not particular to the case of C vs C++ APIs you are referring to, but is also necessary for correct transmission of data between for instance two machines with different endianness . 这并不是您所指的C vs C ++ API的情况所独有的,但是对于在例如两个字节序不同的机器之间正确传输数据也是必需的。

There already exists a number of solutions for serializing structures, some easier to use than others. 已经存在许多用于序列化结构的解决方案,其中一些解决方案比其他解决方案更易于使用。 Currently I'm mainly working on a system where we use Thrift , which lets you define datatypes and structures in an easy-to-read IDL file, and can then generate files implementing these for a large number of languages, so that you can for instance very easily transfer a class from a C++ program over a network to a class in a Java program. 目前,我主要在使用Thrift的系统上工作,该系统可让您在易于阅读的IDL文件中定义数据类型和结构,然后可以生成用于多种语言的文件,以实现这些功能。实例非常容易通过网络将类从C ++程序转移到Java程序中的类。

Other possibilities include JSON, XML, and probably a large number of others also. 其他可能性包括JSON,XML,可能还有很多其他可能性。 Since your case seems relatively simple, you can of course also just write your own - for instance by including serialize() and unserialize() functions, that respectively convert all member variables of your class to an array of char (serialization), and set all member variables of your class from an array of char (unserialization). 由于您的情况看起来相对简单,因此您当然也可以编写自己的示例-例如,通过包含serialize()unserialize()函数,这些函数分别将类的所有成员变量转换为char数组(序列化)并进行设置类的所有成员变量均来自char数组(反序列化)。

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

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