2 Setting Out to C++

1 C++ Intiation

1.1 Features of the main() Function
1.2 The C++ Preprocessor and the iostream File

Here’s the short version of what you need to know. If your program is to use the usual C++ input or output facilities, you provide these two lines:

#include <iostream>
using namespace std;

There are some alternatives to using the second line, but let’s keep things simple for now. (If your compiler doesn’t like these lines, it’s not C++98 compatible,and it will have many other problems with the examples in this book.) That’s all you really must know to
make your programs work, but now let’s take a more in-depth look.

C++, like C, uses a preprocessor.This is a program that processes a source file before the main compilation takes place. (Some C++ implementations,as you might recall from Chapter 1, use a translator program to convert a C++ program to C.Although the translator is also a form of preprocessor, we’re not discussing that preprocessor; instead, we’re discussing the one that handles directives whose names begin with #.) You don’t have to do
anything special to invoke this preprocessor. It automatically operates when you compile the program.

Listing 2.1 uses the #include directive:

#include <iostream> // a PREPROCESSOR directive

This directive causes the preprocessor to add the contents of the iostream file to your program.This is a typical preprocessor action:adding or replacing text in the source code before it’s compiled.

This raises the question of why you should add the contents of the iostream file to the program.The answer concerns communication between the program and the outside world.The io in iostream refers to input, which is information brought into the program,and to output, which is information sent out from the program. C++’s input/output scheme involves several definitions found in the iostream file.Your first program needs these definitions to use the cout facility to display a message.The #include directive causes the contents of the iostream file to be sent along with the contents of your file to the compiler. In essence, the contents of the iostream file replace the #include <iostream> line in the program.Your original file is not altered, but a composite file formed from your file and iostream goes on to the next stage of compilation.

1.3 Header Filenames

Files such as iostream are called include files (because they are included in other files) or header files (because they are included at the beginning of a file). C++ compilers come with many header files, each supporting a particular family of facilities.The C tradition
has been to use the h extension with header files as a simple way to identify the type of file by its name. For example, the C math.h header file supports various C math functions. Initially, C++ did the same. For instance, the header file supporting input and output was named iostream.h. But C++ usage has changed. Now the h extension is reserved for the old C header files (which C++ programs can still use), whereas C++ header files have no
extension.There are also C header files that have been converted to C++ header files. These files have been renamed by dropping the h extension (making it a C+±style name) and prefixing the filename with a c (indicating that it comes from C). For example,
the C++ version of math.h is the cmath header file. Sometimes the C and C++ versions of C header files are identical, whereas in other cases the new version might have a few changes. For purely C++ header files such as iostream, dropping the h is more than a
cosmetic change, for the h-free header files also incorporate namespaces, the next topic in this chapter.Table 2.1 summarizes the naming conventions for header files.

在这里插入图片描述

2 C++ Statements

  • 24
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的v4l2打开摄像头的C代码示例: ``` #include <stdio.h> #include <stdlib.h> #include <string.h> #include <fcntl.h> #include <errno.h> #include <sys/ioctl.h> #include <sys/mman.h> #include <linux/videodev2.h> #define CAMERA_DEVICE "/dev/video0" struct buffer { void *start; size_t length; }; int main(int argc, char **argv) { int fd = -1; struct buffer *buffers = NULL; unsigned int n_buffers = 0; // 1. 打开摄像头设备 fd = open(CAMERA_DEVICE, O_RDWR | O_NONBLOCK, 0); if (fd == -1) { fprintf(stderr, "Error opening device %s: %d, %s\n", CAMERA_DEVICE, errno, strerror(errno)); return EXIT_FAILURE; } // 2. 查询摄像头设备信息 struct v4l2_capability cap; if (ioctl(fd, VIDIOC_QUERYCAP, &cap) == -1) { fprintf(stderr, "Error querying device %s: %d, %s\n", CAMERA_DEVICE, errno, strerror(errno)); goto fail; } // 3. 设置摄像头采集格式 struct v4l2_format fmt; memset(&fmt, 0, sizeof(fmt)); fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; fmt.fmt.pix.width = 640; fmt.fmt.pix.height = 480; fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV; fmt.fmt.pix.field = V4L2_FIELD_INTERLACED; if (ioctl(fd, VIDIOC_S_FMT, &fmt) == -1) { fprintf(stderr, "Error setting format on device %s: %d, %s\n", CAMERA_DEVICE, errno, strerror(errno)); goto fail; } // 4. 请求摄像头缓冲区 struct v4l2_requestbuffers req; memset(&req, 0, sizeof(req)); req.count = 4; req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; req.memory = V4L2_MEMORY_MMAP; if (ioctl(fd, VIDIOC_REQBUFS, &req) == -1) { fprintf(stderr, "Error requesting buffers on device %s: %d, %s\n", CAMERA_DEVICE, errno, strerror(errno)); goto fail; } // 5. 映射摄像头缓冲区到用户空间 buffers = calloc(req.count, sizeof(*buffers)); if (buffers == NULL) { fprintf(stderr, "Error allocating memory for buffers: %d, %s\n", errno, strerror(errno)); goto fail; } for (n_buffers = 0; n_buffers < req.count; n_buffers++) { struct v4l2_buffer buf; memset(&buf, 0, sizeof(buf)); buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; buf.memory = V4L2_MEMORY_MMAP; buf.index = n_buffers; if (ioctl(fd, VIDIOC_QUERYBUF, &buf) == -1) { fprintf(stderr, "Error querying buffer %d on device %s: %d, %s\n", n_buffers, CAMERA_DEVICE, errno, strerror(errno)); goto fail; } buffers[n_buffers].length = buf.length; buffers[n_buffers].start = mmap(NULL, buf.length, PROT_READ | PROT_WRITE, MAP_SHARED, fd, buf.m.offset); if (buffers[n_buffers].start == MAP_FAILED) { fprintf(stderr, "Error mapping buffer %d on device %s: %d, %s\n", n_buffers, CAMERA_DEVICE, errno, strerror(errno)); goto fail; } } // 6. 启动摄像头采集 enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE; if (ioctl(fd, VIDIOC_STREAMON, &type) == -1) { fprintf(stderr, "Error starting capture on device %s: %d, %s\n", CAMERA_DEVICE, errno, strerror(errno)); goto fail; } // 7. 采集图像并处理 printf("Press Ctrl-C to stop.\n"); while (1) { fd_set fds; FD_ZERO(&fds); FD_SET(fd, &fds); struct timeval tv = {0}; tv.tv_sec = 2; int r = select(fd + 1, &fds, NULL, NULL, &tv); if (r == -1) { if (errno == EINTR) continue; fprintf(stderr, "Error waiting for frame on device %s: %d, %s\n", CAMERA_DEVICE, errno, strerror(errno)); goto fail; } if (r == 0) { fprintf(stderr, "Error waiting for frame on device %s: select timeout\n", CAMERA_DEVICE); goto fail; } struct v4l2_buffer buf; memset(&buf, 0, sizeof(buf)); buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; buf.memory = V4L2_MEMORY_MMAP; if (ioctl(fd, VIDIOC_DQBUF, &buf) == -1) { fprintf(stderr, "Error dequeueing buffer on device %s: %d, %s\n", CAMERA_DEVICE, errno, strerror(errno)); goto fail; } // 在这里处理采集到的图像数据 printf("Got a frame with %d bytes\n", buf.bytesused); if (ioctl(fd, VIDIOC_QBUF, &buf) == -1) { fprintf(stderr, "Error queueing buffer on device %s: %d, %s\n", CAMERA_DEVICE, errno, strerror(errno)); goto fail; } } return EXIT_SUCCESS; fail: if (buffers != NULL) { for (unsigned int i = 0; i < n_buffers; i++) { if (buffers[i].start != MAP_FAILED) munmap(buffers[i].start, buffers[i].length); } free(buffers); } if (fd != -1) close(fd); return EXIT_FAILURE; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值