C++ 如何在mac中编译/运行cpp文件

C++ 如何在mac中编译/运行cpp文件,c++,C++,我从GitHub下载了一个webcam\u face\u pose\u ex.cpp文件,现在我想在mac上编译并运行它 #include <dlib/opencv.h> #include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp> #include <dlib/image_processing/frontal_face_detector.h> #include &l

我从GitHub下载了一个webcam\u face\u pose\u ex.cpp文件,现在我想在mac上编译并运行它

#include <dlib/opencv.h>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <dlib/image_processing/frontal_face_detector.h>
#include <dlib/image_processing/render_face_detections.h>
#include <dlib/image_processing.h>
#include <dlib/gui_widgets.h>
#include <X11/Xlib.h>

using namespace dlib;
using namespace std;

int main()
{
    try
    {
        cv::VideoCapture cap(0);
        if (!cap.isOpened())
        {
            cerr << "Unable to connect to camera" << endl;
            return 1;
        }

        image_window win;

        // Load face detection and pose estimation models.
        frontal_face_detector detector = get_frontal_face_detector();
        shape_predictor pose_model;
        deserialize("shape_predictor_68_face_landmarks.dat") >> pose_model;

        // Grab and process frames until the main window is closed by the user.
        while(!win.is_closed())
        {
            // Grab a frame
            cv::Mat temp;
            if (!cap.read(temp))
            {
                break;
            }
            // Turn OpenCV's Mat into something dlib can deal with.  Note that this just
            // wraps the Mat object, it doesn't copy anything.  So cimg is only valid as
            // long as temp is valid.  Also don't do anything to temp that would cause it
            // to reallocate the memory which stores the image as that will make cimg
            // contain dangling pointers.  This basically means you shouldn't modify temp
            // while using cimg.
            cv_image<bgr_pixel> cimg(temp);

            // Detect faces 
            std::vector<rectangle> faces = detector(cimg);
            // Find the pose of each face.
            std::vector<full_object_detection> shapes;
            for (unsigned long i = 0; i < faces.size(); ++i)
                shapes.push_back(pose_model(cimg, faces[i]));

            // Display it all on the screen
            win.clear_overlay();
            win.set_image(cimg);
            win.add_overlay(render_face_detections(shapes));
        }
    }
    catch(serialization_error& e)
    {
        cout << "You need dlib's default face landmarking model file to run this example." << endl;
        cout << "You can get it from the following URL: " << endl;
        cout << "   http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2" << endl;
        cout << endl << e.what() << endl;
    }
    catch(exception& e)
    {
        cout << e.what() << endl;
    }
}

#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间dlib;
使用名称空间std;
int main()
{
尝试
{
cv::视频捕获上限(0);
如果(!cap.isOpened())
{
cerr-u模型;
//抓取并处理帧,直到用户关闭主窗口。
而(!win.is_closed())
{
//抓住一个框架
cv::垫温;
如果(!上限读数(温度))
{
打破
}
//把OpenCV的Mat变成dlib可以处理的东西。注意,这只是
//包装Mat对象,它不会复制任何内容。因此cimg仅在以下情况下有效:
//只要temp是有效的。也不要对temp做任何会导致它的事情
//重新分配存储映像的内存,这将使cimg
//包含悬空指针。这基本上意味着您不应该修改temp
//在使用cimg时。
cv_图像cimg(温度);
//检测人脸
标准::向量面=检测器(cimg);
//找到每张脸的姿势。
向量形状;
for(无符号长i=0;i-I
标志和
#include
语句的一些信息:

webcam\u face\u pose\u ex.cpp
是一个较大项目的一部分,您将无法单独编译它,因为它依赖于其他文件。
#include
指令规定,为了编译此程序,必须先编译由
#include
指定的文件中的代码。这意味着必须编译整个程序在编译
网络摄像头\u face\u pose\u ex.cpp
之前下载。此项目还需要下载,以便我们可以将opencv2文件夹放置在dlib项目文件夹中

现在,我们可以打开terminal并将目录更改为dlib项目文件夹,并使用以下命令编译文件:

g++ -I. examples/webcam_face_pose_ex.cpp
注意:我们正在指定查找由#include指定的文件的目录,使用
-I
参数作为
-I.
这意味着搜索当前工作目录中的文件。在那里它将找到dlib文件夹和
dlib/opencv.h

但是,这还不够。当您执行命令时,会遇到错误
opencv2/opencv\u modules.hpp:没有这样的文件或目录

解决方案
说明应使用构建示例。请确保使用cmake编译示例。

嘿,欢迎使用Stack Overflow!请先发布一段您试图编译的代码,以便我们可以帮助您解决与您相同的问题,并告诉您如何解决。您需要一个
-I
,并且要使用它,您还需要e> -L
-L
嘿@Alex这将不得不进入你的问题。当你粘贴它时,突出显示它并单击{}“编码”按钮以使其格式化。@CausingUnderflowsEverywhere谢谢我用我认为是导致错误的代码编辑了这篇文章。嘿@Alex你为什么要编译那个特定的文件?您好,这样做给了我这个结果:
dlib master/examples/webcam\u face\u pose\u ex.cpp:30:10:致命错误:'dlib/opencv.h'文件不存在”d#include^~~~~~~~~~~~~~~1生成错误。
我这样做了,但没有任何区别。主文件夹名为dlib masterOnce,我们需要使用-I参数指定包含文件的位置。
g++ -I. examples/webcam_face_pose_ex.cpp