我正在使用 CMakeLists.txt 从 c++ 快速屏幕截图编译程序,以便与 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;
}
正如你所看到的,我也尝试将变量属性设置为指针,但它不起作用!
我怎样才能成功编译这段代码?
这帮助我成功编译了程序。
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
find_package(OpenCV REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS} /usr/include/X11)
set(X11)
link_directories( /usr/lib/X11 )
target_link_libraries(${PROJECT_NAME} ${OpenCV_LIBS} X11)