Bigicecream Bigicecream - 3 months ago 17
C++ Question

CMake "uuid" linking FAIL

when trying to build i get this errors:


undefined reference to 'uuid_generate'

undefined reference to 'uuid_unparse'


this my CMakeLists.txt file:

cmake_minimum_required(VERSION 3.6)
project(Synergy)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -luuid -pthread")
find_package (Threads)

set(SOURCE_FILES Functions.cpp Inotify.cpp Inotify.h main.cpp Master.h Message.cpp Message.h Server.cpp Server.h Threads.cpp )
add_executable(synergy_server ${SOURCE_FILES})


I can solve this problem by creating Makefile by myself and add -luuid flag.

but I want to do it with CMake,I tried to add this flag in the CMakeLists.txt file but it doesn't help :(

I have installed uuid-dev(it's not the Problem )

Hope you can help me.

Have a nice day

edit:
I add
target_link_libraries(Synergy uuid)
to the end of the file and it works but there is better why in what written in the answers

Answer

This is not a good way to write a CMake script. CMake come with a rich set of functionalities that you must use to describe what you want to do.

In your case, you must avoid setting flags directly as you have done. In this way, you will be able to change compiler or even OS and your script will still work.

In your case, I would write something like this:

cmake_minimum_required(VERSION 3.6)
project(Synergy)

set(CMAKE_CXX_STANDARD 11)

# using pkg-config to configure uuid
find_package(PkgConfig REQUIRED)
find_package(Threads REQUIRED)

pkg_search_module(UUID REQUIRED uuid)

set(SOURCE_FILES Functions.cpp Inotify.cpp Inotify.h main.cpp Master.h message.cpp Message.h Server.cpp Server.h Threads.cpp )
add_executable(synergy_server ${SOURCE_FILES})
target_include_directories(synergy_server PUBLIC ${UUID_INCLUDE_DIRS})
target_link_libraries(synergy_server ${UUID_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT})

By the way, what means uuid-dev? I have no such thing in my system