0

I'm new with cmake. With cmake I was able to compile my project on my laptop, but on the raspberry is not working.

This is the error i get on raspberry:

-- The C compiler identification is GNU 4.9.2 -- The CXX compiler identification is GNU 4.9.2 -- Check for working C compiler: /usr/bin/cc -- Check for working C compiler: /usr/bin/cc -- works -- Detecting C compiler ABI info -- Detecting C compiler ABI info - done -- Check for working CXX compiler: /usr/bin/c++ -- Check for working CXX compiler: /usr/bin/c++ -- works -- Detecting CXX compiler ABI info -- Detecting CXX compiler ABI info - done -- Found PkgConfig: /usr/bin/pkg-config (found version "0.28") -- checking for one of the modules 'glib-2.0' -- Found GLib: /usr/lib/arm-linux-gnueabihf/libglib-2.0.so (found version "2.42.1") -- Found mhd: /usr/include CMake Error at cmake/FindGLIB.cmake:39 (add_library): add_library cannot create imported target "glib-2.0" because another target with the same name already exists. Call Stack (most recent call first):librerie/CMakeLists.txt:2 (find_package) -- Found GLib: /usr/lib/arm-linux-gnueabihf/libglib-2.0.so (found version "2.42.1") -- Configuring incomplete, errors occurred! See also "/home/pi/pl1/CMakeFiles/CMakeOutput.log". 

This is my project structure:

src-> ---- CMakeLists.txt ---- main.c ---- librerie-> -------------- CMakeLists.txt -------------- cJSON.c -------------- cJSON.h -------------- config.c -------------- config.h -------------- server_web.c -------------- server_web.h -------------- funzioni_thread.c -------------- funzioni_thread.h ---- cmake-> -------------- FindGLIB.cmake -------------- FindMHD.cmake 

This is First CMakeLists :

cmake_minimum_required (VERSION 2.6) project (TestPL) include_directories("${PROJECT_BINARY_DIR}") list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake") find_package(GLIB REQUIRED) find_package(MHD REQUIRED) add_subdirectory (librerie) set (EXTRA_LIBS ${EXTRA_LIBS} librerie) include_directories (${GLib_INCLUDE_DIRS} ${EXTRA_LIBS}) # add the executable add_executable(TestPL main.c) target_link_libraries (TestPL ${GLib_LIBRARY} ${MHD_LIBRARY} ${EXTRA_LIBS} m) 

This is CMakeLists in librerie directory:

find_package(GLIB REQUIRED) find_package(MHD REQUIRED) include_directories (${GLib_INCLUDE_DIRS} ${EXTRA_LIBS}) add_library (librerie cJSON.c config.c generic.c server_web.c funzioni_thread.c) target_link_libraries (librerie ${GLib_LIBRARY} ${MHD_LIBRARY}) 

What am I doing wrong?

1 Answer 1

1

You call twice

find_package(GLIB REQUIRED) 

First time it is called from top-level CMakeLists.txt and defines glib-2.0 target. Second time it is called from librerie/CMakeLists.txt and attempt to create glib-2.0 again. That is why you see that error message: the target is already defined in this scope.

Possible workaround is to step into library subdirectory first, and only then call find_package() in top-level CMakeLists.txt:

add_subdirectory (librerie) find_package(GLIB REQUIRED) find_package(MHD REQUIRED) 

Because IMPORTED targets has local visibility, glib-2.0 target defined by find_package(GLIB) call from subdirectory librerie/ will not be visible after returning from this subdirectory. So the second call from top-level directory will succeed.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you very much!! that was the problem, now it works! what I do not understand is why even the wrong way on my laptop runs quietly
Probably, your laptop has different FindGLIB.cmake script, which does not define imported targets but variables instead. Unlike to targets, redefinition of variables is not an error on CMake.
The FindGLIB.cmake script is the same for raspberry and laptop, I copied all the project files, maybe on arm does things differently?, boh, anyway, thanks again!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.