Home > front end >  Install and access C/C Library from Github Repository (CMAKE)
Install and access C/C Library from Github Repository (CMAKE)

Time:12-02

I am currently struggling to install and use a C Library I found. This is clearly a simple question but I can't connect the knowledge, to find the right sentence to google it.

I am trying to install the following library:

https://github.com/kwhat/libuiohook

I did the described steps which seem to work with no error.

$ git clone https://github.com/kwhat/libuiohook
$ cd uiohook
$ mkdir build && cd build
$ cmake -S .. -D BUILD_SHARED_LIBS=ON -D BUILD_DEMO=ON -DCMAKE_INSTALL_PREFIX=../dist
$ cmake --build . --parallel 2 --target install  

The problem is I don't know what to do next.

I tried to copy the code from the simple hook example and tried to execute the code in a different executable with the following cmake.

cmake_minimum_required(VERSION 3.20)
project(libuihook_test)


include_directories("/Users/ahoehne/libuiohook/dist/include")
find_library(lib uiohook)

if(NOT lib)
    message(FATAL_ERROR "uiohook library not found")
endif()

set(CMAKE_CXX_STANDARD 17)

add_executable(libuihook_test main.cpp)

target_link_libraries(libuihook_test lib)

which doesn't work and results in a

CMake Error at CMakeLists.txt:9 (message):
  uiohook library not found

the log from installing is the following:

[ 14%] Building C object CMakeFiles/uiohook.dir/src/darwin/input_helper.c.o
[ 14%] Building C object CMakeFiles/uiohook.dir/src/logger.c.o
[ 21%] Building C object CMakeFiles/uiohook.dir/src/darwin/input_hook.c.o
[ 28%] Building C object CMakeFiles/uiohook.dir/src/darwin/post_event.c.o
[ 35%] Building C object CMakeFiles/uiohook.dir/src/darwin/system_properties.c.o
[ 42%] Linking C shared library libuiohook.dylib
[ 42%] Built target uiohook
[ 57%] Building C object CMakeFiles/demo_post.dir/demo/demo_post.c.o
[ 57%] Building C object CMakeFiles/demo_properties.dir/demo/demo_properties.c.o
[ 71%] Linking C executable demo_properties
[ 71%] Linking C executable demo_post
[ 71%] Built target demo_post
[ 71%] Built target demo_properties
[ 78%] Building C object CMakeFiles/demo_hook.dir/demo/demo_hook.c.o
[ 85%] Building C object CMakeFiles/demo_hook_async.dir/demo/demo_hook_async.c.o
[ 92%] Linking C executable demo_hook
[100%] Linking C executable demo_hook_async
[100%] Built target demo_hook
[100%] Built target demo_hook_async
Install the project...
-- Install configuration: ""
-- Installing: /Users/ahoehne/libuiohook/dist/lib/libuiohook.1.2.0.dylib
-- Up-to-date: /Users/ahoehne/libuiohook/dist/lib/libuiohook.1.dylib
-- Up-to-date: /Users/ahoehne/libuiohook/dist/lib/libuiohook.dylib
-- Up-to-date: /Users/ahoehne/libuiohook/dist/include/uiohook.h
-- Installing: /Users/ahoehne/libuiohook/dist/lib/cmake/uiohook/uiohook-config.cmake
-- Installing: /Users/ahoehne/libuiohook/dist/lib/cmake/uiohook/uiohook-config-noconfig.cmake
-- Installing: /Users/ahoehne/libuiohook/dist/bin/demo_hook
-- Installing: /Users/ahoehne/libuiohook/dist/bin/demo_hook_async
-- Installing: /Users/ahoehne/libuiohook/dist/bin/demo_post
-- Installing: /Users/ahoehne/libuiohook/dist/bin/demo_properties
-- Installing: /Users/ahoehne/libuiohook/dist/lib/pkgconfig/uiohook.pc

I consulted the CMAKE Docs but honestly right now confuses me more than it helps. I am sure at some point I will have an aha moment and connect the dots. Most tutorials do an excellent job of explaining you C/C but they all lack the more advanced stuff around build tools, testing, debugging, and so on, everything you basically need to build more complex projects. So I am also grateful for book/video suggestions.

I also tried different combinations of include_directories, add, link library also an absolute path but nothing seems to work and I get a library not found error or uiohook.h not found.

I am well aware that C and C are completely different languages and different practices apply I am not sure If I could have fixed this question to one. But basically try to learn C but need to access a lot of C libs

CodePudding user response:

As the library provides a uiohook-config.cmake you should use that to link to with find_package rather than using find_library.

Something like this should work:

set(CMAKE_PREFIX_PATH /Users/ahoehne/libuiohook/dist/lib/cmake/uiohook/)
find_package(uiohook REQURIED)
target_link_libraries(libuihook_test uiohook)
  • Related