My team is working on a super light-weight project written in C, where we want to render shapes and GUI elements using Xlib and Cairo. We're working with Kdevelop and thus depend on CMake, but none of us is really well acquainted with it. We managed to bind Xlib, but we just can't get Cairo up and running.
I included PkgConfig and we're importing Cairo with #include <cairo/cairo.h>
. I also tried the following:
find_package(Cairo REQUIRED)
link_libraries(${Cairo_LIBRARIES})
include_directories(${Cairo_INCLUDE_DIR})
but it didn't work.
Our current CMake file:
cmake_minimum_required(VERSION 3.10)
project(SAGE VERSION 1.0)
set(BASEPATH "${SAGE_SOURCE_DIR}/src")
# set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${SAGE_BINARY_DIR}/build)
file(GLOB_RECURSE SOURCES RELATIVE ${CMAKE_SOURCE_DIR} "${BASEPATH}/*.c")
# Link X11 libraries
# Removing any of the following three lines results in an error
find_package(X11 REQUIRED)
link_libraries(${X11_LIBRARIES})
include_directories(${X11_INCLUDE_DIR})
add_executable(SAGE "${BASEPATH}/main.c" ${SOURCES})
aux_source_directory(dir src)
target_include_directories(SAGE PRIVATE "${BASEPATH}")
find_package(PkgConfig)
pkg_check_modules(Cairo REQUIRED cairo)
include_directories(${Cairo_INCLUDE_DIRS})
# include_directories(src)
When executing code we get the following error message in Kdevelop:
/usr/bin/ld: /home/luxdragon/Dokumente/GitHub/SAGE/src/main.c:134: undefined reference to `cairo_new_path'
/usr/bin/ld: /home/luxdragon/Dokumente/GitHub/SAGE/src/main.c:135: undefined reference to `cairo_translate'
/usr/bin/ld: /home/luxdragon/Dokumente/GitHub/SAGE/src/main.c:138: undefined reference to `cairo_fill'
/usr/bin/ld: /home/luxdragon/Dokumente/GitHub/SAGE/src/main.c:140: undefined reference to `cairo_stroke'
How do we integrate Cairo into CMake? Note that Xlib works flawlessly. Any help would be greatly appreciated.
EDIT
I tried the following:
I put message("Cairo libs: '${Cairo_LIBRARIES}'")
after pkg_check_modules(Cairo REQUIRED cairo)
. I also added:
target_link_libraries(SAGE PUBLIC
PkgConfig::Cairo)
The output is as follows:
Cairo libs: 'cairo'
-- Configuring done
CMake Error at CMakeLists.txt:23 (target_link_libraries):
Target "SAGE" links to:
PkgConfig::Cairo
but the target was not found. Possible reasons include:
* There is a typo in the target name.
* A find_package call is missing for an IMPORTED target.
* An ALIAS target is missing.
NEW EDIT I changed
pkg_check_modules(Cairo REQUIRED cairo)
to
pkg_check_modules(Cairo REQUIRED IMPORTED_TARGET cairo)
and when running cmake -S .
no error message is returned. This looks good:
-- Checking for module 'cairo'
-- Found cairo, version 1.17.6
Cairo libs: 'cairo'
-- Configuring done
-- Generating done
-- Build files have been written to: /home/luxdragon/Dokumente/SAGE
LAST EDIT and it works in Kdevelop now!