CMake find_package cannot find SDL2 - c++

Below there is the CMakeLists.txt:
cmake_minimum_required(VERSION 3.20)
project(sdl_test)
set(CMAKE_CXX_STANDARD 20)
list(APPEND CMAKE_PREFIX_PATH "D:/lib/SDL2_mingw")
MESSAGE(STATUS "CMAKE_PREFIX_PATH: ${CMAKE_PREFIX_PATH}")
find_package(sdl2 REQUIRED)
MESSAGE(STATUS "SDL2_INCLUDE_DIRS is ${SDL2_INCLUDE_DIRS}")
MESSAGE(STATUS "SDL2_LIBRARIES is ${SDL2_LIBRARIES}")
include_directories(${SDL2_INCLUDE_DIRS})
include_directories(${SDL2_INCLUDE_DIRS}-config-)
add_executable(sdl_test main.cpp)
target_link_libraries(sdl_test ${SDL2_LIBRARIES})
And the CMake console output is:
-- CMAKE_PREFIX_PATH: D:/lib/SDL2_mingw
-- SDL2_INCLUDE_DIRS is D://include;D://include/SDL2
-- SDL2_LIBRARIES is SDL2::SDL2
-- Configuring done
CMake Error at CMakeLists.txt:15 (add_executable):
Target "sdl_test" links to target "SDL2::SDL2" but the target was not
found. Perhaps a find_package() call is missing for an IMPORTED target, or
an ALIAS target is missing?
-- Generating done
CMake Generate step failed. Build files cannot be regenerated correctly.
Not only the ${SDL2_LIBRARIES} (SDL::SDL) cannot be linked, ${SDL2_INCLUDE_DIRS} (D://include;D://include/SDL2) is also wrong. Facutually, it should be D:/lib/SDL2_mingw/include.
My SDL2 lib is under D:/lib/SDL2_mingw. And there does exist a SDL2Config.cmake file. I have write list(APPEND CMAKE_PREFIX_PATH "D:/lib/SDL2_mingw") in the CMakeList.
D:/lib/SDL2_mingw Directory
What's going on here?
Update:
if I enable CMake_GNUtoMS when configure CMake for SDL2, after re-build SDL2 I get both *.a and *.lib files under D:/lib/SDL2_mingw directory.
And then CMake can find the package. However, when compile my project, new error occurs:
mingw32-make.exe[3]: *** No rule to make target 'D:/lib/SDL2_mingw/lib/x64/SDL2.lib', needed by 'sdl_test.exe'. Stop.
mingw32-make.exe[3]: *** Waiting for unfinished jobs....
[ 50%] Building CXX object CMakeFiles/sdl_test.dir/main.cpp.obj
mingw32-make.exe[2]: *** [CMakeFiles\Makefile2:82: CMakeFiles/sdl_test.dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFiles\Makefile2:89: CMakeFiles/sdl_test.dir/rule] Error 2
mingw32-make.exe: *** [Makefile:123: sdl_test] Error 2

Related

Problem with building c++ project with opencv and dlib libraries via cmake

I use c++ project with opencv and dlib libraries. I have CMakeLists.txt:
cmake_minimum_required(VERSION 3.20.0)
project(project1)
find_package(OpenCV REQUIRED)
include(FetchContent)
FetchContent_Declare(dlib
GIT_REPOSITORY https://github.com/davisking/dlib.git
GIT_TAG v19.18
)
FetchContent_MakeAvailable(dlib)
add_executable(project1 main.cpp)
target_link_libraries(project1 ${OpenCV_LIBS} dlib::dlib)
target_compile_options(project1 PUBLIC "$<$<CONFIG:RELEASE>:${MY_RELEASE_OPTIONS}>")
I write in cygwin cmake .. -G "Unix Makefiles". I use "Unix Makefiles" to create makefile that use to command "make". After this I write make, but as a result I get error:
collect2: error: ld execution ended with return code 1
make[2]: *** [CMakeFiles/project1.dir/build.make:123: project1.exe] Error 1
make[1]: *** [CMakeFiles/Makefile2:115: CMakeFiles/project1.dir/all] Error 2
make: *** [Makefile:91: all] Error 2
What I need to do to fix this problem?

Build a project depending on the Swiften XMPP library with CMake

I am currently trying to setup my C++ project using CMake, I successfully included dependencies like OpenMPI, but when going to Swiften, this became complex. This is not delivered in the form a package, but as shown in the git repository linked up there. Building that wasn't an issue, but I searched quite a lot of time, without finding any way to include libraries that don't provide a .so, and no main headers.
Here's my current CMakeLists.txt, made for testing only.
cmake_minimum_required (VERSION 3.0.0)
#set the executable
project (mtest)
set(EXECUTABLE_OUTPUT_PATH .)
add_executable (mtest main.cpp)
#defines libs
find_package(MPI REQUIRED)
set(CMAKE_CXX_COMPILE_FLAGS ${CMAKE_CXX_COMPILE_FLAGS} ${MPI_COMPILE_FLAGS})
set(CMAKE_CXX_LINK_FLAGS ${CMAKE_CXX_LINK_FLAGS} ${MPI_LINK_FLAGS})
include_directories(SYSTEM ${MPI_INCLUDE_PATH})
include_directories(SYSTEM Swiften)
target_link_libraries(mtest ${MPI_LIBRARIES})
add_library(swiften SHARED IMPORTED)
set_target_properties(swiften PROPERTIES
IMPORTED_LOCATION "${CMAKE_SOURCE_DIR}/Swiften/libSwiften.a"
INTERFACE_INCLUDE_DIRECTORIES "${CMAKE_SOURCE_DIR}/Swiften"
)
target_link_libraries(mtest swiften)
And when trying to run make, here's the error :
-- Configuring done
-- Generating done
-- Build files have been written to: /home/user/Documents/CTests/build
[ 50%] Building CXX object CMakeFiles/mtest.dir/main.cpp.o
/home/user/Documents/CTests/main.cpp:12:10: fatal error: Swiften/Client/Client.h: No such file or directory
#include <Swiften/Client/Client.h>
^~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
CMakeFiles/mtest.dir/build.make:62: recipe for target 'CMakeFiles/mtest.dir/main.cpp.o' failed
make[2]: *** [CMakeFiles/mtest.dir/main.cpp.o] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/mtest.dir/all' failed
make[1]: *** [CMakeFiles/mtest.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2
By the way, if the directory structure may help you, here's a tree output : https://pastebin.com/giPb9229

How to integrate QtMultimedia component into a project using CMake?

I'm trying to write a GUI appication (under Ubuntu 18.04) using Qt5 and CMake. I failed to integrate the Qt multimedia module into my project.
My CMakeLists.txt:
cmake_minimum_required(VERSION 3.10)
project(Chess)
#set(CMAKE_PREFIX_PATH "/home/mashplant/Qt5.9.6/5.9.6/gcc_64")
#whether I comment it off or not doesn't have an effect on the result
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTORCC ON)
find_package(Qt5 COMPONENTS Widgets Network Multimedia REQUIRED)
set(CMAKE_CXX_STANDARD 11)
include_directories(.)
add_executable(Main Main.cpp
MainWindow.cpp MainWindow.hpp ChessFrame.cpp ChessFrame.hpp resource.qrc)
target_link_libraries(Main Qt5::Widgets Qt5::Network Qt5::Multimedia)
And I get the following warning when I run CMake:
CMake Warning at CMakeLists.txt:18 (add_executable):
Cannot generate a safe runtime search path for target Main because files in
some directories may conflict with libraries in implicit directories:
runtime library [libQt5Widgets.so.5] in /usr/lib/x86_64-linux-gnu may be hidden by files in:
/home/mashplant/Qt5.9.6/5.9.6/gcc_64/lib
runtime library [libQt5Network.so.5] in /usr/lib/x86_64-linux-gnu may be hidden by files in:
/home/mashplant/Qt5.9.6/5.9.6/gcc_64/lib
runtime library [libQt5Gui.so.5] in /usr/lib/x86_64-linux-gnu may be hidden by files in:
/home/mashplant/Qt5.9.6/5.9.6/gcc_64/lib
runtime library [libQt5Core.so.5] in /usr/lib/x86_64-linux-gnu may be hidden by files in:
/home/mashplant/Qt5.9.6/5.9.6/gcc_64/lib
Some of these libraries may not be found correctly.
And when I compile, I get a link error.
/home/mashplant/Qt5.9.6/5.9.6/gcc_64/lib/libQt5Multimedia.so.5.9.6:undefined reference to ‘operator delete(void*, unsigned long)#Qt_5’
collect2: error: ld returned 1 exit status
CMakeFiles/Main.dir/build.make:203: recipe for target 'Main' failed
make[3]: *** [Main] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/Main.dir/all' failed
make[2]: *** [CMakeFiles/Main.dir/all] Error 2
CMakeFiles/Makefile2:79: recipe for target 'CMakeFiles/Main.dir/rule' failed
make[1]: *** [CMakeFiles/Main.dir/rule] Error 2
Makefile:118: recipe for target 'Main' failed
make: *** [Main] Error 2
My best bet is that CMake find different components from different installations of Qt. If that's the case, it should be enough to correct all Qt-related CMake variables to point to correct paths (for example, variables like Qt5Widgets_DIR, Qt5Multimedia_DIR, etc.).

CLion cmake glew error?

I ma trying to setup glew for clion but for some reason everything I have tried after researching the subject seems to not work and produce errors. I have downloaded the source files from their main website http://glew.sourceforge.net/ and are located within my clion project under directory Externals. Note: there is for some reason nothing under the lib folder under glew. my assumption build be that cmake builds it then links it which would be fine but I am very new to cmake and have no idea if this is the case.
CMake compile errors
C:\Users\Matt\AppData\Local\JetBrains\Toolbox\apps\CLion\ch-0\171.4073.41\bin\cmake\bin\cmake.exe -DCMAKE_BUILD_TYPE=Debug -G "CodeBlocks - MinGW Makefiles" C:\Users\Matt\CLionProjects\SkyGames
-- Could NOT find Vulkan (missing: VULKAN_INCLUDE_DIR)
-- Using Win32 for window creation
-- Could NOT find GLEW (missing: GLEW_INCLUDE_DIR GLEW_LIBRARY)
-- Configuring done
CMake Error:
Error evaluating generator expression:
$<TARGET_PDB_FILE:glew>
TARGET_PDB_FILE is not supported by the target linker.
-- Generating done
-- Build files have been written to: C:/Users/Matt/CLionProjects/SkyGames/cmake-build-debug
CMakeList for project
cmake_minimum_required(VERSION 3.7)
project(SkyGames)
set(CMAKE_CXX_STANDARD 14)
add_executable(SkyGames ${SOURCE_FILES})
set(GLFW_BUILD_DOCS OFF CACHE BOOL "" FORCE)
set(GLFW_BUILD_TESTS OFF CACHE BOOL "" FORCE)
set(GLFW_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE)
add_subdirectory(${PROJECT_SOURCE_DIR}/Externals/GLFW3)
###########################
# GLFW
###########################
target_link_libraries(SkyGames glfw)
include_directories(SkyGames ${GLFW_INCLUDE_DIR})
###########################
# GLEW
###########################
add_subdirectory(${PROJECT_SOURCE_DIR}/Externals/glew2s/build/cmake)
find_package(glew REQUIRED)
if (GLEW_FOUND)
include_directories(SkyGames ${GLEW_INCLUDE_DIR})
endif()
##########################
# OPENGL
##########################
find_package(OpenGL REQUIRED)
target_link_libraries(SkyGames ${OPENGL_gl_LIBRARY} ${OPENGL_glu_LIBRARY})
include_directories(SkyGames ${OPENGL_INCLUDE_DIR})
Update:
It appears that the new version of the CMakeLists.txt works fine except when you build the program it errors saying permission denied. it is further highlighted below.
new GLEW portion of CmakeList.txt
###########################
# GLEW
###########################
set(CMAKE_PREFIX_PATH ${PROJECT_SOURCE_DIR}/Externals/glew2s/build/cmake)
set(GLEW_INCLUDE_DIR ${PROJECT_SOURCE_DIR}/Externals/glew2s/include)
set(GLEW_LIBRARY ${PROJECT_SOURCE_DIR}/Externals/glew2s/lib)
find_package(GLEW REQUIRED)
include_directories(${PROJECT_SOURCE_DIR}/Externals/glew2s/include)
##########################
# OPENGL
##########################
find_package(OpenGL REQUIRED)
target_link_libraries(SkyGames GLEW::GLEW ${OPENGL_LIBRARY} ${OPENGL_gl_LIBRARY} ${OPENGL_glu_LIBRARY})
Build Message
C:\Users\Matt\AppData\Local\JetBrains\Toolbox\apps\CLion\ch-0\171.4073.41\bin\cmake\bin\cmake.exe --build C:\Users\Matt\CLionProjects\SkyGames\cmake-build-debug --target SkyGames -- -j 8
[ 71%] Built target glfw
[ 76%] Linking CXX executable SkyGames.exe
c:/mingw/bin/../lib/gcc/mingw32/5.3.0/../../../../mingw32/bin/ld.exe: cannot find ../Externals/glew2s/lib: Permission denied
collect2.exe: error: ld returned 1 exit status
mingw32-make.exe[3]: *** [SkyGames.exe] Error 1
CMakeFiles\SkyGames.dir\build.make:206: recipe for target 'SkyGames.exe' failed
mingw32-make.exe[2]: *** [CMakeFiles/SkyGames.dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFiles/SkyGames.dir/rule] Error 2
CMakeFiles\Makefile2:66: recipe for target 'CMakeFiles/SkyGames.dir/all' failed
mingw32-make.exe: *** [SkyGames] Error 2
CMakeFiles\Makefile2:78: recipe for target 'CMakeFiles/SkyGames.dir/rule' failed
Makefile:161: recipe for target 'SkyGames' failed

Linking external libraries cmake arduino

I'm trying to link a downloaded library called ArduinoJson to my project with CMake, but it seems that whenever i include the file it is missing, it will just ask for the next file its missing. I will end up with a lot of include lines which i think isnt the right way. I cant seem to find the correct syntax to include a libary in CMake
CMakeLists.txt
cmake_minimum_required(VERSION 2.8.11)
SET(CMAKE_CXX_STANDARD 11)
SET(CMAKE_CXX_STANDARD_REQUIRED ON)
SET(CMAKE_CXX_FLAGS "-DTESTING")
include_directories(
/usr/share/arduino/libraries/ArduinoJson/include
/usr/share/arduino/hardware/arduino/cores/arduino
)
set(files
DataHandler.h
DataHandler.cpp
JSON.cpp
JSON.h
HAL_mock.cpp
HAL_mock.h
HAL_Protocol.h
lib/ArduinoJson/ArduinoJson.h
)
# Locate GTest
find_package(GTest REQUIRED)
# Link runTests with what we want to test and the GTest library
add_executable(runTests DataHandlerTests.cpp JSONTests.cpp HALTests.cpp ${files})
target_link_libraries(runTests GTest::Main ArduinoJson)
The error I get
[ 14%] Building CXX object CMakeFiles/runTests.dir/JSONTests.cpp.o
In file included from /usr/share/arduino/libraries/ArduinoJson/include/ArduinoJson/Internals/../Internals/../String.hpp:14:0,
from /usr/share/arduino/libraries/ArduinoJson/include/ArduinoJson/Internals/../Internals/DynamicStringBuilder.hpp:11,
from /usr/share/arduino/libraries/ArduinoJson/include/ArduinoJson/Internals/../Internals/JsonPrintable.hpp:12,
from /usr/share/arduino/libraries/ArduinoJson/include/ArduinoJson/Internals/../JsonVariant.hpp:13,
from /usr/share/arduino/libraries/ArduinoJson/include/ArduinoJson/Internals/../JsonBuffer.hpp:14,
from /usr/share/arduino/libraries/ArduinoJson/include/ArduinoJson/Internals/BlockJsonBuffer.hpp:10,
from /usr/share/arduino/libraries/ArduinoJson/include/ArduinoJson/DynamicJsonBuffer.hpp:10,
from /usr/share/arduino/libraries/ArduinoJson/include/ArduinoJson.hpp:10,
from /usr/share/arduino/libraries/ArduinoJson/include/ArduinoJson.h:8,
from /usr/share/arduino/libraries/ArduinoJson/ArduinoJson.h:8,
from /home/zizyx/Desktop/git-sensorNetwerk/sensor-network/gtest/JSON.h:6,
from /home/zizyx/Desktop/git-sensorNetwerk/sensor-network/gtest/JSONTests.cpp:5:
/usr/share/arduino/hardware/arduino/cores/arduino/WString.h:29:26: fatal error: avr/pgmspace.h: No such file or directory
compilation terminated.
CMakeFiles/runTests.dir/build.make:86: recipe for target 'CMakeFiles/runTests.dir/JSONTests.cpp.o' failed
make[2]: *** [CMakeFiles/runTests.dir/JSONTests.cpp.o] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/runTests.dir/all' failed
make[1]: *** [CMakeFiles/runTests.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2
I would like to add 2 maybe 3 lines which include all the libraries i need for my ArduinoJson library