不能包含libs到cmake

时间:2017-06-04 12:33:32

标签: c++ cmake x11 cairo

我有src / main.cpp绘制叠加矩形

#include <stdio.h>
#include <time.h>
#include <X11/Xlib.h>

#include <X11/extensions/Xcomposite.h>
#include <X11/extensions/shape.h>

#include <cairo/cairo.h>
#include <cairo/cairo-xlib.h>

Display *d;
Window overlay;
Window root;
int width, height;

void allow_input_passthrough (Window w) {
    XserverRegion region = XFixesCreateRegion (d, NULL, 0);

    XFixesSetWindowShapeRegion (d, w, ShapeBounding, 0, 0, 0);
    XFixesSetWindowShapeRegion (d, w, ShapeInput, 0, 0, region);

    XFixesDestroyRegion (d, region);
}

void prep_overlay (void) {
    overlay = XCompositeGetOverlayWindow (d, root);
    allow_input_passthrough (overlay);
}

void draw(cairo_t *cr) {
    int quarter_w = width / 4;
    int quarter_h = height / 4;
    cairo_set_source_rgb(cr, 1.0, 0.0, 0.0);
    cairo_rectangle(cr, quarter_w, quarter_h, quarter_w * 2, quarter_h * 2);
    cairo_fill(cr);
}

int main() {
    struct timespec ts = {0, 5000000};

    d = XOpenDisplay(NULL);

    int s = DefaultScreen(d);
    root = RootWindow(d, s);

    XCompositeRedirectSubwindows (d, root, CompositeRedirectAutomatic);
    XSelectInput (d, root, SubstructureNotifyMask);

    width = DisplayWidth(d, s);
    height = DisplayHeight(d, s);

    prep_overlay();

    cairo_surface_t *surf = cairo_xlib_surface_create(d, overlay,
                                                      DefaultVisual(d, s),
                                                      width, height);
    cairo_t *cr = cairo_create(surf);

    XSelectInput(d, overlay, ExposureMask);

    draw(cr);

    XEvent ev;
    while(1) {
        overlay = XCompositeGetOverlayWindow (d, root);
        draw(cr);
        XCompositeReleaseOverlayWindow (d, root);
        nanosleep(&ts, NULL);
    }

    cairo_destroy(cr);
    cairo_surface_destroy(surf);
    XCloseDisplay(d);
    return 0;
}

和CMakeLists.txt

cmake_minimum_required(VERSION 3.7)
project(7dtd_external)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -m64 -std=c++14 -fpermissive")
set(EXECUTABLE_OUTPUT_PATH ../)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -lX11")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -lXfixes")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -lXcomposite")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -lcairo -I /usr/include/cairo")

file(GLOB_RECURSE SOURCE_FILES "${CMAKE_SOURCE_DIR}/src/main.cpp")

add_executable(7dtd_external ${SOURCE_FILES})

当我尝试在CLion中编译它时,它会输出:

/home/user/Documents/clion-2017.1.2/bin/cmake/bin/cmake --build /home/user/Documents/Projects/7dtd/cmake-build-debug --target 7dtd_external -- -j 8
Scanning dependencies of target 7dtd_external
[ 50%] Linking CXX executable ../7dtd_external
CMakeFiles/7dtd_external.dir/src/main.cpp.o: In function `allow_input_passthrough(unsigned long)':
/home/user/Documents/Projects/7dtd/src/main.cpp:17: undefined reference to `XFixesCreateRegion'
.......
/home/user/Documents/Projects/7dtd/src/main.cpp:22: undefined reference to `XFixesDestroyRegion'
CMakeFiles/7dtd_external.dir/src/main.cpp.o: In function `prep_overlay()':
/home/user/Documents/Projects/7dtd/src/main.cpp:26: undefined reference to `XCompositeGetOverlayWindow'
..........
/home/user/Documents/Projects/7dtd/src/main.cpp:35: undefined reference to `cairo_fill'
CMakeFiles/7dtd_external.dir/src/main.cpp.o: In function `main':
/home/user/Documents/Projects/7dtd/src/main.cpp:41: undefined reference to `XOpenDisplay'
/home/user/Documents/Projects/7dtd/src/main.cpp:46: undefined reference to `XCompositeRedirectSubwindows'
.........
/home/user/Documents/Projects/7dtd/src/main.cpp:67: undefined reference to `XCompositeReleaseOverlayWindow'
collect2: error: ld returned 1 exit status
CMakeFiles/7dtd_external.dir/build.make:94: recipe for target '../7dtd_external' failed
make[3]: *** [../7dtd_external] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/7dtd_external.dir/all' failed
make[2]: *** [CMakeFiles/7dtd_external.dir/all] Error 2
CMakeFiles/Makefile2:79: recipe for target 'CMakeFiles/7dtd_external.dir/rule' failed
make[1]: *** [CMakeFiles/7dtd_external.dir/rule] Error 2
Makefile:118: recipe for target '7dtd_external' failed
make: *** [7dtd_external] Error 2

但我可以在终端编译它:

# g++ src/main.cpp -o x11drawoverlay -lX11 -lXfixes -lXcomposite -lcairo -I /usr/include/cairo

并启动它。

那么,我应该在CMakeLists.txt中编写什么来编译呢?

0 个答案:

没有答案