我尝试使用CMakeLists.txt从c++ fast screenshots in linux for use with opencv编译程序。cmake
运行良好,但当使用make
时,我得到了这个错误:
/usr/bin/ld: CMakeFiles/demoScreenShot.dir/main.cpp.o: undefined reference to symbol 'XGetWindowAttributes'
/usr/bin/ld: /lib/x86_64-linux-gnu/libX11.so.6: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/demoScreenShot.dir/build.make:112: demoScreenShot] Error 1
make[1]: *** [CMakeFiles/Makefile2:83: CMakeFiles/demoScreenShot.dir/all] Error 2
make: *** [Makefile:91: all] Error 2
字符串
这是我的CMakeLists.txt
# Personal Project (Necesary)
cmake_minimum_required(VERSION 3.18.0)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED True)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
project(demoScreenShot)
add_executable(${PROJECT_NAME} main.cpp)
# OpenCV
set(CMAKE_CXX_STANDARD 17)
find_package(OpenCV REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})
target_link_libraries(${PROJECT_NAME} ${OpenCV_LIBS})
# X11 dependecies. It throws the same error using this block or not using it
include_directories( /usr/X11/include ) # It fails with this line or without it
link_directories( /usr/X11/lib ) # It fails with this line or without it
set (OPENGL_LIBRARIES GL GLU X11) # It fails with this line or without it
型
我的main.cpp
#include <opencv4/opencv2/opencv.hpp>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <cstdint>
#include <cstring>
#include <vector>
void ImageFromDisplay(std::vector<uint8_t>& Pixels, int& Width, int& Height, int& BitsPerPixel)
{
Display* display = XOpenDisplay(nullptr);
Window root = DefaultRootWindow(display);
XWindowAttributes* attributes = {0};
XGetWindowAttributes(display, root, attributes);
// XWindowAttributes attributes = {0};
// XGetWindowAttributes(display, root, &attributes);
// Width = attributes.width;
// Height = attributes.height;
Width = attributes -> width;
Height = attributes -> height;
XImage* img = XGetImage(display, root, 0, 0 , Width, Height, AllPlanes, ZPixmap);
BitsPerPixel = img->bits_per_pixel;
Pixels.resize(Width * Height * 4);
memcpy(&Pixels[0], img->data, Pixels.size());
XDestroyImage(img);
XCloseDisplay(display);
}
int main()
{
int Width = 0;
int Height = 0;
int Bpp = 0;
std::vector<std::uint8_t> Pixels;
ImageFromDisplay(Pixels, Width, Height, Bpp);
if (Width && Height)
{
//Mat(Size(Height, Width), Bpp > 24 ? CV_8UC4 : CV_8UC3, &Pixels[0]);
cv::Mat img = cv::Mat(Height, Width, Bpp > 24 ? CV_8UC4 : CV_8UC3, &Pixels[0]);
namedWindow("WindowTitle", cv::WINDOW_AUTOSIZE);
imshow("Display window", img);
cv::waitKey(0);
}
return 0;
}
型
正如你所看到的,我也试过把变量属性变成指针,但是没有成功!
如何才能成功编译这段代码?
1条答案
按热度按时间ohfgkhjo1#
这帮助我成功地编写了程序。
字符串