How do I write the cmake script for clion to include glfw? - linux

cmake_minimum_required(VERSION 3.15)
project(hello)
find_package(PkgConfig REQUIRED)
pkg_search_module(GLFW REQUIRED glfw3)
set(CMAKE_CXX_STANDARD 11)
add_executable(hello main.cpp)
INCLUDE_DIRECTORIES(${GLFW_INCLUDE_DIRS})
TARGET_LINK_LIBRARIES(hello ${GLFW_STATIC_LIBRARIES})
It tells me
CMake Error at /home/user/.local/share/JetBrains/Toolbox/apps/CLion/ch-0/193.5233.144/bin/cmake/linux/share/cmake-3.15/Modules/FindPkgConfig.cmake:696 (message):
None of the required 'glfw3' found
when I try to build it. My glfw folder is located at /usr/local/include/GLFW.

AFAIK, glfw3 is using CMake as the build system,
(src: packages.debian.org/fr/sid/amd64/libglfw3-dev/filelist)
which uses modern CMake, so you don't need GLFW_INCLUDE_DIRS etc...
Inside this file /usr/lib/cmake/glfw3/glfw3Targets.cmake (loaded by /usr/lib/cmake/glfw3/glfw3Config.cmake), you'll see:
...
# Create imported target glfw
add_library(glfw SHARED IMPORTED)
set_target_properties(glfw PROPERTIES
INTERFACE_COMPILE_DEFINITIONS "GLFW_DLL"
INTERFACE_INCLUDE_DIRECTORIES "/usr/include"
)
...
So you can simply use:
find_package(glfw3 REQUIRED)
...
target_link_libraries(Foo glfw)
ps: same as my previous comment

Related

CMake not finding Qt5 when trying to create Qt5 Project in CLion on Linux

I am getting the following CMake error:
CMake Error at CMakeLists.txt:23 (find_package):
By not providing "FindQt5.cmake" in CMAKE_MODULE_PATH this project has
asked CMake to find a package configuration file provided by "Qt5", but
CMake did not find one.
Could not find a package configuration file provided by "Qt5" with any of
the following names:
Qt5Config.cmake
qt5-config.cmake
Add the installation prefix of "Qt5" to CMAKE_PREFIX_PATH or set "Qt5_DIR"
to a directory containing one of the above files. If "Qt5" provides a
separate development package or SDK, be sure it has been installed.
In my CMakeLists.txt, there is this line:
if (NOT CMAKE_PREFIX_PATH)
message(WARNING "CMAKE_PREFIX_PATH is not defined, you may need to set it "
"(-DCMAKE_PREFIX_PATH=\"path/to/Qt/lib/cmake\" or -DCMAKE_PREFIX_PATH=/usr/include/{host}/qt{version}/ on Ubuntu)")
endif ()
So I did what it says and set my PREFIX_PATH to "/usr/include/x86_64-linux-gnu/qt5/".
However, the error I get says that CMake cannot find "qt5-config.cmake", which is located at "/usr/lib/x86_64-linux-gnu/cmake/Qt5" and not "/usr/include/x86_64-linux-gnu/qt5/", so I tried setting the "Qt5_DIR" to that directory (since it says in the error to set it to a dir that contains the above files) but still without success. Any ideas why it cant find the files it needs?
edit:
my CMakeLists.txt:
cmake_minimum_required(VERSION 3.19)
project(untitled1)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_PREFIX_PATH "/usr/lib/x86_64-linux-gnu/cmake/Qt5/")
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)
set(QT_VERSION 5)
set(REQUIRED_LIBS Core Gui Widgets)
set(REQUIRED_LIBS_QUALIFIED Qt5::Core Qt5::Gui Qt5::Widgets)
add_executable(${PROJECT_NAME} main.cpp)
if (NOT CMAKE_PREFIX_PATH)
message(WARNING "CMAKE_PREFIX_PATH is not defined, you may need to set it "
"(-DCMAKE_PREFIX_PATH=\"path/to/Qt/lib/cmake\" or -DCMAKE_PREFIX_PATH=/usr/include/{host}/qt{version}/ on Ubuntu)")
endif ()
find_package(Qt${QT_VERSION} COMPONENTS ${REQUIRED_LIBS} REQUIRED)
target_link_libraries(${PROJECT_NAME} ${REQUIRED_LIBS_QUALIFIED})
note that the file has been auto-generated by CLion, all I did was change the path according to what is specified in the if-Block

cmake error while building OpenCV in native on Android Studio

I am trying to compile OpenCV in native on Android Studio. I am trying to follow : https://sriraghu.com/2017/03/11/opencv-in-android-an-introduction-part-1/
I was succesfully able to do it by following these steps on Windows. However on Ubuntu, its not working. The error is :
Execution failed for task ‘:app:externalNativeBuildDebug’.
Build command failed. Error while executing process /home/user/Android/Sdk/cmake/3.6.4111459/bin/cmake with arguments {–build
/home/user/AndroidStudioProjects/project_folders/app/.externalNativeBuild/cmake/debug/arm64-v8a
–target native-lib}
ninja: error: ‘../../../../jniLibs/src/main/jniLibs/arm64-v8a/libopencv_java3.so’,
needed by
‘../../../../build/intermediates/cmake/debug/obj/arm64-v8a/libnative-lib.so’,
missing and no known rule to make it
The CMakeLists.txt file is:
cmake_minimum_required(VERSION 3.4.1)
set(pathToOpenCV /media/user/Stuff/Softwares/OpenCV-android-sdk/sdk/native)
include_directories(${pathToOpenCV}/jni/include)
add_library( # Sets the name of the library.
native-lib
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
src/main/cpp/native-lib.cpp )
# Searches for a specified prebuilt library and stores the path as a
# variable. Because CMake includes system libraries in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.
add_library(lib_opencv SHARED IMPORTED)
set_target_properties(lib_opencv PROPERTIES IMPORTED_LOCATIONS /home/user/cmu/android-apps/OpenCV_app_June23/OpenCV_Android/app/src/main/jniLibs/arm64-v8a/libopencv_java3.so)
find_library( # Sets the name of the path variable.
log-lib
# Specifies the name of the NDK library that
# you want CMake to locate.
log )
# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
# build script, prebuilt third-party libraries, or system libraries.
target_link_libraries( # Specifies the target library.
native-lib
# Links the target library to the log library
# included in the NDK.
${log-lib} lib_opencv)
In native-lib.cpp, I am able to add #include without any error prompts in Android Studio.
My previous searches suggest that there is something wrong with the paths. I have checked that the paths are correct, or is there a different way to put the path in Ubuntu?

CMake linking glfw3 lib error

i'm working with CLion, and I'm writing a program using the glfw3 lib.(http://www.glfw.org/docs/latest/)
I installed and did everything correctly for the lib i have the .a and .h files in:
/usr/local/lib/libglfw3.a
/usr/local/include/.h(all files)
I'm trying to use the library now, but i'm getting the linker error:
undefined reference to 'glViewport' etc. etc. all the functions i'm using
I added the lib path to the make file, I can't understand what i'm doing wrong, my CMakeLists.txt looks like this:
cmake_minimum_required(VERSION 3.6)
project(testing)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES main.cpp examples.h)
add_executable(testing ${SOURCE_FILES})
target_link_libraries(testing /usr/local/lib/libglfw3.a)
target_link_libraries(testing /usr/local/lib/libTest.a)
target_link_libraries(testing pthread)
any help will be appreciated.
Thanks!
You should not hard code absolute paths into your CMake files. This renders CMake useless.
In the documentation of GLFW on how to link against it, there it is explicitly written:
find_package(glfw3 3.2 REQUIRED)
find_package(OpenGL REQUIRED)
target_include_directories(myapp PUBLIC ${OPENGL_INCLUDE_DIR})
target_link_libraries(myapp
glfw
${OPENGL_gl_LIBRARY})
Moreover, you can replace
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
with
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
and CMake will figure out the correct compiler flag automatically depending on the compiler currently in use.

How to list target_link_libraries available on a system?

I'm trying to link with the Curl library in my CMakeLists.txt and I know that I should require the package add executable and link the library but I don't know how the variables names are called and where should I look for them, do you know where should I be looking for? Is there a way to list all the libraries in the system that I could be able to add in my project?
Here it's my CMakeLists (silly) attempt:
cmake_minimum_required(VERSION 3.3)
project(RadekClientRecon)
find_package(OpenCV REQUIRED)
find_package(CURL REQUIRED)
link_directories(/usr/lib/)
set(SOURCE_FILES main.cpp HttpManager.cpp)
add_executable(RadekClientRecon ${SOURCE_FILES})
target_link_libraries(RadekClientRecon ${OpenCV_LIBS} ${CURL_LIBRARIES})
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
I've searched all "curl" named files in my computer if helps, I'm also using CLion if there's an automated way.
rinaldi#rinaldi-home:~/Projects/radekrecon$ sudo find / -name "*curl*" 2> /dev/null
/home/rinaldi/.atom/packages/atom-beautify/node_modules/jscs/lib/rules/disallow-curly-braces.js
/home/rinaldi/.atom/packages/atom-beautify/node_modules/jscs/lib/rules/require-curly-braces.js
/var/lib/dpkg/info/libcurl3:amd64.list
/var/lib/dpkg/info/python3-pycurl.md5sums
/var/lib/dpkg/info/libcurl3-gnutls:amd64.shlibs
/var/lib/dpkg/info/curl.md5sums
/var/lib/dpkg/info/libcurl3:amd64.postrm
/var/lib/dpkg/info/libcurl3:amd64.md5sums
/var/lib/dpkg/info/python3-pycurl.list
/var/lib/dpkg/info/libcurl3-gnutls:amd64.symbols
/var/lib/dpkg/info/libcurl3-gnutls:amd64.postrm
/var/lib/dpkg/info/libcurl3:amd64.symbols
/var/lib/dpkg/info/libcurl3-gnutls:amd64.md5sums
/var/lib/dpkg/info/libcurl3:amd64.postinst
/var/lib/dpkg/info/libcurl3:amd64.shlibs
/var/lib/dpkg/info/python3-pycurl.postinst
/var/lib/dpkg/info/curl.list
/var/lib/dpkg/info/python3-pycurl.prerm
/var/lib/dpkg/info/libcurl3-gnutls:amd64.postinst
/var/lib/dpkg/info/libcurl3-gnutls:amd64.list
/opt/clion-1.2.4/bin/cmake/doc/cmake-3.3/cmcurl
/opt/clion-1.2.4/bin/gdb/lib/python2.7/macurl2path.py
/usr/lib/python2.7/macurl2path.py
/usr/lib/python3.5/macurl2path.py
/usr/lib/x86_64-linux-gnu/libcurl-gnutls.so.4
/usr/lib/x86_64-linux-gnu/libcurl.so.4
/usr/lib/x86_64-linux-gnu/libcurl-gnutls.so.3
/usr/lib/x86_64-linux-gnu/libcurl.so.3
/usr/lib/x86_64-linux-gnu/libcurl-gnutls.so.4.3.0
/usr/lib/x86_64-linux-gnu/gstreamer-0.10/libgstcurl.so
/usr/lib/x86_64-linux-gnu/libcurl.so.4.3.0
/usr/lib/python3/dist-packages/pycurl.cpython-35m-x86_64-linux-gnu.so
/usr/lib/python3/dist-packages/curl
/usr/lib/python3/dist-packages/pycurl-7.19.5.1.egg-info
/usr/lib/python3/dist-packages/pycurl.cpython-34m-x86_64-linux-gnu.so
/usr/lib/python3.4/macurl2path.py
/usr/lib/gnupg/gpgkeys_curl
/usr/bin/curl
/usr/share/man/man1/curl.1.gz
/usr/share/lintian/overrides/libcurl3
/usr/share/lintian/overrides/libcurl3-gnutls
/usr/share/doc/libcurl3
/usr/share/doc/python3-pycurl
/usr/share/doc/libcurl3-gnutls
/usr/share/doc/curl
/usr/share/bash-completion/completions/curl
Apparently I needed to install some packages other than curl.
* libcurl4-gnutls-dev
* libcurl4-nss-dev
* libcurl4-openssl-dev
Those variables are named after their modules that can be found here.
My CMakeList.txt now it's creating the proper make file and looks like this:
cmake_minimum_required(VERSION 3.3)
project(RadekClientRecon)
find_package(OpenCV REQUIRED)
find_package(CURL REQUIRED)
link_directories(/usr/lib/)
link_directories(CURL_INCLUDE_DIRS)
set(SOURCE_FILES main.cpp HttpManager.cpp)
add_executable(RadekClientRecon ${SOURCE_FILES})
target_link_libraries(RadekClientRecon ${OpenCV_LIBS} ${CURL_LIBRARIES})
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
After verifying the installation of curl, this is my CMakeLists.txt, which is running well on ubuntu 17.10
cmake_minimum_required(VERSION 2.8)
set(CURL_MIN_VERSION "7.55.1")
project(Downloader)
find_package(CURL REQUIRED)
link_directories(/usr/lib/x86_64-linux-gnu/)
add_executable(Downloader HTTPDownloaderExample.cpp HTTPDownloader.cpp)
target_link_libraries(Downloader curl)

Incorrect Dynamic Libraries linking after explicit set in Cmake

I've got a very frustrating issue a the moment. I'm attempting to build an executable that uses ROS and a compiled Matlab .so file. Due to what I believe is a boost conflict, I'm trying to build this exe using an earlier version of boost. Now, I've set up the cmakelist file, ran catkin_make (as its a ros project) and everything compiles, and the correct boost version is seen.
However, when running a ldd on the compiled exe, it still seems to be linking to the more recent version. Its rather annoying. I've posted the cmakelists file, and the ldd outcome. My system is Ubuntu 14.04 with ROS Indigo.
Any help would be really good, as I'm pulling my hair out trying to figure out what is wrong!
Many thanks in advance!
##CMAKE FILE
cmake_minimum_required(VERSION 2.8.3)
project(testmatlabdll)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wfatal-errors -O4 -march=native")
add_definitions(-std=c++11)
find_package(catkin REQUIRED)
find_package(PCL 1.7 REQUIRED)
link_directories(${PCL_LIBRARY_DIRS})
include_directories(${PCL_INCLUDE_DIRS})
add_definitions(${PCL_DEFINITIONS})
set(Boost_NO_SYSTEM_PATHS ON)
set(BOOST_ROOT /home/devbot/Downloads/boost_1_50_0)
set(BOOST_DIR /home/devbot/Downloads/boost_1_50_0)
set(Boost_INCLUDE_DIR /home/devbot/Downloads/boost_1_50_0)
set(BOOST_INCLUDEDIR /home/devbot/Downloads/boost_1_50_0)
set(Boost_USE_STATIC_LIBS OFF)
set(Boost_USE_MULTITHREADED ON)
message(STATUS "BOOST_ROOT = ${BOOST_ROOT}")
message(STATUS "Boost_VERSION = ${Boost_VERSION}")
message(STATUS "Boost_LIB_VERSION = ${Boost_LIB_VERSION}")
message(STATUS "Boost_MAJOR_VERSION = ${Boost_MAJOR_VERSION}")
message(STATUS "Boost_MINOR_VERSION = ${Boost_MINOR_VERSION}")
message(STATUS "Boost_LIBRARIES = ${Boost_LIBRARIES}")
message(STATUS "Boost_INCLUDE_DIRS = ${Boost_INCLUDE_DIRS}")
message(STATUS "Boost_LIBRARY_DIRS = ${Boost_LIBRARY_DIRS}")
find_package(Boost COMPONENTS REQUIRED
system filesystem thread date_time iostreams serialization chrono)
find_package(catkin REQUIRED COMPONENTS roscpp rospy std_msgs message_generation image_transport cv_bridge)
find_package(OpenCV REQUIRED)
catkin_package(CATKIN_DEPENDS roscpp
std_msgs
pcl_ros
sensor_msgs
cv_bridge
message_runtime
LIBRARIES ${PROJECT_NAME}
DEPENDS system_lib)
include_directories(include ${catkin_INCLUDE_DIRS})
include_directories(${OpenCV_INCLUDE_DIRS})
include_directories(/usr/local/MATLAB/R2013b/extern/include/)
include_directories(/opt/matlab/extern/include)
include_directories(/home/devbot/catkin_ws/src/testmatlabdll/src)
link_directories(/home/devbot/catkin_ws/src/testmatlabdll/src)
add_library(mwmc SHARED IMPORTED)
set_property(TARGET mwmc PROPERTY IMPORTED_LOCATION /usr/local/MATLAB/MATLAB_Compiler_Runtime/v82/runtime/glnxa64/libmwmclmcrrt.so)
add_executable(testMatlab src/testMatlab.cpp)
target_link_libraries(testMatlab ${Boost_LIBRARIES} ${catkin_LIBRARIES} ${PCL_LIBRARIES} ${OpenCV_LIBRARIES})
target_link_libraries(testMatlab roughlib mwmc)
OUTPUT FROM LDD
ldd testMatlab | grep boost
libboost_system.so.1.50.0 => /home/devbot/Downloads/boost_1_50_0/stage/lib/libboost_system.so.1.50.0 (0x00007f7456f8a000)
libboost_system.so.1.54.0 => /usr/lib/x86_64-linux-gnu/libboost_system.so.1.54.0 (0x00007f7454662000)
libboost_thread.so.1.54.0 => /usr/lib/x86_64-linux-gnu/libboost_thread.so.1.54.0 (0x00007f745444c000)
libboost_filesystem.so.1.54.0 => /usr/lib/x86_64-linux-gnu/libboost_filesystem.so.1.54.0 (0x00007f7454018000)
libboost_regex.so.1.54.0 => /usr/lib/x86_64-linux-gnu/libboost_regex.so.1.54.0 (0x00007f744d5d8000)

Resources