Compiling with QT: libGL.so.1 not found - c++

I'm trying to compile a simple Qt program with make after using CMake and I keep getting this error:
/usr/bin/ld: warning: libGL.so.1, needed by /usr/lib/x86_64-linux-gnu/libQt5Gui.so.5.5.1, not found (try using -rpath or -rpath-link)
/usr/lib/x86_64-linux-gnu/libQt5Gui.so.5.5.1: undefined reference to `glGetTexParameterfv'
/usr/lib/x86_64-linux-gnu/libQt5Gui.so.5.5.1: undefined reference to `glHint'
/usr/lib/x86_64-linux-gnu/libQt5Gui.so.5.5.1: undefined reference to `glClearColor'
/usr/lib/x86_64-linux-gnu/libQt5Gui.so.5.5.1: undefined reference to `glFlush'
...
collect2: error: ld returned 1 exit status
I have re-installed libgl1-mesa-dev via sudo apt install --reinstall libgl1-mesa-dev, made sure I have libGL.so.1 installed in my /usr/lib/x86_64-linux-gnu/mesa directory and I have run sudo ldconfig to no avail.
I'm not sure what is causing this and I haven't seen this problem anywhere else. It seems to be a dependency I'm ignoring, however I installed Qt using sudo apt install qt5-default so I would think the dependencies would be handled.
Here's my CMakeLists.txt:
cmake_minimum_required(VERSION 2.6)
project(SimpleQt)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_AUTOMOC ON)
find_package(Qt5Widgets CONFIG REQUIRED)
include_directories(include)
add_executable(SimpleQt src/main.cpp)
target_link_libraries(growth Qt5::Widgets)

I figured out a way to make it work thanks to Ripi2 in the comments. Apparently I had to link OpenGL with my project. Here's my updated CMakeLists.txt:
cmake_minimum_required(VERSION 2.6)
project(SimpleQt)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_AUTOMOC ON)
find_package(OpenGL Required) # Added this line
find_package(Qt5Widgets CONFIG REQUIRED)
include_directories(include ${OPENGL_INCLUDE_DIRS}) # Updated this line
add_executable(SimpleQt src/main.cpp)
target_link_libraries(growth Qt5::Widgets ${OPENGL_LIBRARIES}) # Updated this line
I'm leaving this unanswered though because this is definitely not how it should be. All the examples on Qt's site do not show OpenGL as a link requirement, so I feel there must be a better way.

Related

undefined reference to boost::system::generic_category() cmake

I have an undefined reference while trying to compile my Qt5 project.
I am usually pretty confident with using CMake but this time I have a strange error that I can't figure out:
undefined reference to boost::system::generic_category()
/usr/local/include/boost/system/error_code.hpp:223: undefined reference to 'boost::system::generic_category()'
My configurations are:
gcc (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0
Qt5.15
cmake version 3.16.3
After typing whereis boost the outcome wasboost: /usr/include/boost and after applying the great power of dpkg -s libboost-dev | grep 'Version' :) the version is Version: 1.71.0.0ubuntu2
I don't understand what is happening, below an example of how my CMakeLists.txt is structured:
cmake_minimum_required (VERSION 3.1)
project(projectA)
set (OpenCV_DIR /home/to/opencv/build)
find_package( OpenCV REQUIRED )
find_package( Boost COMPONENTS system thread filesystem REQUIRED)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(CMAKE_INCLUDE_CURRENT_DIR ON)
INCLUDE_DIRECTORIES(${Boost_INCLUDE_DIR})
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
find_package(Qt5Widgets)
find_package(Qt5PrintSupport)
#make them into headers
qt5_wrap_ui (UI_HDRS ${UI})
add_executable(projectA main/main.cpp ui/qdarkstyle/style.qrc ${SRCS} ${UI_HDRS} ${UI_SRCS})
target_link_libraries (projectA Qt5::Widgets ${Boost_LIBRARIES} ${OpenCV_LIBS} Qt5::PrintSupport)
add_library(projectA_lib SHARED ${SRCS} ${UI_HDRS})
target_include_directories (projectA_lib PUBLIC "src/" "ui/")
link_directories(${Boost_LIBRARY_DIRS})
target_link_libraries (projectA_lib Qt5::Widgets ${Boost_LIBRARIES} ${OpenCV_LIBS})
I have searched and applied solutions I saw on all possible sources I was able to find such as:
This source but that didn't work.
Also from here it seems that this solution shall be applied:
set(Boost_USE_STATIC_LIBS ON)
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME OFF)
find_package(Boost REQUIRED COMPONENTS system)
# the call to include_directories is now useless:
# the Boost::system imported target used below
# embeds the include directories
project(APP C CXX)
add_executable(APP src.cpp)
target_link_libraries(APP Boost::system)
However that also didn't do any specific benefits to finding the solution.
Other posts I consulted were this, this one but no answer was provided.
This post was useful but that also didn't provide any advice that didn't already know.
Thanks for pointing to the right direction and trying to find a solution.

How to add OpenXLSX to my project in c++?

I have a project and I want to use OpenXLSX as excel library. However, I can't add this to my project.
I move all OpenXLSX files into my project folder, it didn't work. Also moved them to my Desktop, it didn't work either.
Here is my CMakeList.txt file:
#cmake_minimum_required(VERSION 3.5)
#project(PCL_Visualizer LANGUAGES CXX)
#set(CMAKE_CXX_STANDARD 11)
#set(CMAKE_CXX_STANDARD_REQUIRED ON)
#add_executable(PCL_Visualizer main.cpp)
cmake_minimum_required(VERSION 3.5)
project(mainwindow)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
# init_qt: Let's do the CMake job for us
set(CMAKE_AUTOMOC ON) # For meta object compiler
set(CMAKE_AUTORCC ON) # Resource files
set(CMAKE_AUTOUIC ON) # UI files
# Find includes in corresponding build directories
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "/home/fatih/Desktop")
# Find the QtWidgets library
find_package(Qt5 REQUIRED Widgets)
find_package(VTK REQUIRED)
find_package(PCL 1.7.1 REQUIRED)
find_package(OpenXLSX REQUIRED)
# Fix a compilation bug under ubuntu 16.04 (Xenial)
#list(REMOVE_ITEM PCL_LIBRARIES "vtkproj4")
include_directories(${PCL_INCLUDE_DIRS})
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/OpenXLSX-master)
#include_directories(OpenXLSX-master/library/headers)
#include_directories(OpenXLSX-master/library/external/pugixml)
#include_directories(OpenXLSX-master/library/external/nowide/nowide)
#include_directories(OpenXLSX-master/library/external/zippy)
add_definitions(${PCL_DEFINITIONS})
set(project_SOURCES main.cpp mainwindow.cpp)
add_executable(${PROJECT_NAME} ${project_SOURCES} icons.qrc)
target_link_libraries(${PROJECT_NAME} ${PCL_LIBRARIES} Qt5::Widgets)
Everytime I try to cmake .. and make, i get this error.
Could not find a package configuration file provided by "OpenXLSX" with any
of the following names:
OpenXLSXConfig.cmake
openxlsx-config.cmake
Add the installation prefix of "OpenXLSX" to CMAKE_PREFIX_PATH or set
"OpenXLSX_DIR" to a directory containing one of the above files. If
"OpenXLSX" provides a separate development package or SDK, be sure it has
been installed.
I just add these commands to my CMakeList.txt and problem solved partially.
include_directories(/home/fatih/Desktop/OpenXLSX-master/library)
include_directories(/home/fatih/Desktop/OpenXLSX-master/build/library)
include_directories(/home/fatih/Desktop/OpenXLSX-master/library/headers)
link_directories(/home/fatih/Desktop/OpenXLSX-master/build/library)
link_directories(/home/fatih/Desktop/OpenXLSX-master/build/library/headers)
link_directories(/home/fatih/Desktop/OpenXLSX-master/library/headers)
target_link_libraries(${PROJECT_NAME} OpenXLSX VERSION 0.2.0 LANGUAGES CXX)
However, now I get different type of error.
/usr/bin/ld: cannot find -lOpenXLSX
/usr/bin/ld: cannot find -lVERSION
/usr/bin/ld: cannot find -l0.2.0
/usr/bin/ld: cannot find -lLANGUAGES
/usr/bin/ld: cannot find -lCXX
/usr/bin/ld: cannot find -lOpenXLSX
/usr/bin/ld: cannot find -lVERSION
/usr/bin/ld: cannot find -l0.2.0
/usr/bin/ld: cannot find -lLANGUAGES
/usr/bin/ld: cannot find -lCXX
Edit: I solved that problem too. If someone gets an error like this, I changed the target_link_library comment to this:
target_link_libraries(${PROJECT_NAME} /home/fatih/Desktop/OpenXLSX-master/build/output/libOpenXLSX-shared.so)

Build fails with OpenCV on cmake

I'm trying to compile a project using OpenCV 2.4 on ubuntu 16.04. I installed opencv using apt-get:
sudo apt-get install libopencv-dev
sudo apt-get install libopencv-nonfree-dev
However, when compiling the project I'm getting the following error message:
undefined reference to `cv::imread(std::string const&, int)'
My CMakeLists.txt looks like this:
cmake_minimum_required(VERSION 3.5)
project(Ex)
find_package(OpenCV REQUIRED )
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c99")
message(STATUS "CMAKE_CXX_FLAGS=${CMAKE_CXX_FLAGS}}")
message(STATUS "OpenCV_LIBS=${OpenCV_LIBS}")
set(SOURCE_FILES main.cpp)
add_executable(Ex ${SOURCE_FILES})
target_link_libraries(Ex ${OpenCV_LIBS})
set_property(TARGET Ex PROPERTY C_STANDARD 99)
Does anyone know how to solve this?
Edit
The output of the second message is
OpenCV_LIBS=opencv_videostab;opencv_video;opencv_ts;opencv_superres;opencv_stitching;opencv_photo;opencv_ocl;opencv_objdetect;opencv_nonfree;opencv_ml;opencv_legacy;opencv_imgproc;opencv_highgui;opencv_gpu;opencv_flann;opencv_features2d;opencv_core;opencv_contrib;opencv_calib3d

CMake doesn't work with Google Protobuf

Unable to link protobuf library using CMake. My CMakeLists is
cmake_minimum_required(VERSION 3.6)
project(addressbook)
set(CMAKE_CXX_STANDARD 11)
set(PROJECT_NAME addressbook)
ADD_SUBDIRECTORY(proto)
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_BINARY_DIR})
ADD_EXECUTABLE(main main.cpp)
TARGET_LINK_LIBRARIES(main proto ${PROTOBUF_LIBRARY})
and in proto subdirectory there is another CMakeLists.txt (that way it is done in github repo https://github.com/shaochuan/cmake-protobuf-example)
INCLUDE(FindProtobuf)
FIND_PACKAGE(Protobuf REQUIRED)
INCLUDE_DIRECTORIES(${PROTOBUF_INCLUDE_DIR})
PROTOBUF_GENERATE_CPP(PROTO_SRC PROTO_HEADER message.proto)
ADD_LIBRARY(proto ${PROTO_HEADER} ${PROTO_SRC})
But my IDE still outputs buch of lines like
CMakeFiles/main.dir/main.cpp.o: In function main':
/home/camille/ClionProjects/protobuf/main.cpp:42: undefined reference
togoogle::protobuf::internal::VerifyVersion(int, int, char const*)'
/home/camille/ClionProjects/protobuf/main.cpp:49: undefined reference
to tutorial::AddressBook::AddressBook()'
/home/camille/ClionProjects/protobuf/main.cpp:54: undefined reference
togoogle::protobuf::Message::ParseFromIstream(std::istream*)'
Where is my mistake? How do I make it work?
Your program fails to link because ${PROTOBUF_LIBRARY} is empty in the scope of your top-level CMakeLists.txt. This happens because calling add_subdirectory creates a child scope, and the Protobuf_XXX variables set by find_package(Protobuf REQUIRED) are only in that child scope.
A good way to fix this is to add the following to proto/CMakeLists.txt:
target_link_libraries(proto INTERFACE ${Protobuf_LIBRARIES})
This instructs targets that link to proto to also link to ${Protobuf_LIBRARIES}. Now you can simplify target_link_libraries in your top-level CMakeLists.txt:
target_link_libraries(addressbook proto)
On a side note, you could also use e.g.
target_link_libraries(${PROJECT_NAME} INTERFACE ... )
${PROJECT_NAME} resolves to whatever you have set in the project(...) statement in that CMakeLists.txt file.
Finally, note that this links to Protobuf_LIBRARIES instead of PROTOBUF_LIBRARY. Protobuf_LIBRARIES includes both the Protocol Buffers libraries and the dependent Pthreads library.
Watch out for variable name case: With CMake 3.6 and later, the FindProtobuf module input and output variables were all renamed from PROTOBUF_ to Protobuf_ (see release notes), so using Protobuf_ works with CMake 3.6, but fails with undefined reference with an earlier version.
To be on the safe side, either use the old style
target_link_libraries(${PROJECT_NAME} INTERFACE ${PROTOBUF_LIBRARIES}))
or force everyone to use at least CMake 3.6
cmake_minimum_required(VERSION 3.6)
Also, there is a resolved bug report in the Kitware cmake issue tracker with more information on how to diagnose such issues.
The variable you need to pass to target_link_libraries is Protobuf_LIBRARIES. See documentation.
CMakeLists.txt:
cmake_minimum_required(VERSION 3.12)
project(protobuf)
SET(CMAKE_CXX_FLAGS "-g -Wall -Werror -std=c++11")
set(CMAKE_CXX_STANDARD 11)
INCLUDE(FindProtobuf)
FIND_PACKAGE(Protobuf REQUIRED)
INCLUDE_DIRECTORIES(${PROTOBUF_INCLUDE_DIR})
PROTOBUF_GENERATE_CPP(PROTO_SRC PROTO_HEADER addressbook.proto)
ADD_LIBRARY(proto2 ${PROTO_HEADER} ${PROTO_SRC})
TARGET_LINK_LIBRARIES(proto2)
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_BINARY_DIR})
add_executable(protobuf main.cpp)
TARGET_LINK_LIBRARIES(protobuf proto2 ${PROTOBUF_LIBRARY})

CMake not linking SDL library? [duplicate]

I'm looking for the simplest way to compile a c++ program using SDL2 and SDL_image with cmake.
Here is my best attempt, after hours of searching:
CMakeLists.txt
project(shooter-cmake2)
cmake_minimum_required(VERSION 2.8)
set(SOURCES
shooter.cpp
classes.cpp
utils.cpp
)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x")
add_executable(${PROJECT_NAME} ${SOURCES})
INCLUDE(FindPkgConfig)
PKG_SEARCH_MODULE(SDL2 REQUIRED sdl2)
PKG_SEARCH_MODULE(SDL2_image REQUIRED sdl2_image)
INCLUDE_DIRECTORIES(${SDL2_INCLUDE_DIRS} ${SDL2IMAGE_INCLUDE_DIR})
TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${SDL2_LIBRARIES} ${SDL2IMAGE_LIBRARY})
I get these errors:
In function `loadTexture(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, SDL_Renderer*)':
undefined reference to `IMG_LoadTexture'
collect2: ld returned 1 exit status
Here is the function call:
#include "SDL.h"
#include "SDL_image.h"
SDL_Texture* loadTexture(const std::string &file, SDL_Renderer *ren){
SDL_Texture *texture = IMG_LoadTexture(ren, file.c_str());
texture != nullptr or die("LoadTexture");
return texture;
}
I think that the following will work, as it finds the libraries on my ubuntu system and the example function you provided can link:
project(shooter-cmake2)
cmake_minimum_required(VERSION 2.8)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x")
add_executable(${PROJECT_NAME} src/test.cpp)
INCLUDE(FindPkgConfig)
PKG_SEARCH_MODULE(SDL2 REQUIRED sdl2)
PKG_SEARCH_MODULE(SDL2IMAGE REQUIRED SDL2_image>=2.0.0)
INCLUDE_DIRECTORIES(${SDL2_INCLUDE_DIRS} ${SDL2IMAGE_INCLUDE_DIRS})
TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${SDL2_LIBRARIES} ${SDL2IMAGE_LIBRARIES})
If cmake is executed with --debug-output it outputs:
-- Found PkgConfig: /usr/bin/pkg-config (found version "0.26")
Called from: [2] /usr/share/cmake-2.8/Modules/FindPkgConfig.cmake
[1] $USER/stack-overflow/cmake-sdl2-image/CMakeLists.txt
-- checking for one of the modules 'sdl2'
Called from: [1] $USER/stack-overflow/cmake-sdl2-image/CMakeLists.txt
-- checking for one of the modules 'SDL2_image>=2.0.0'
Called from: [1] $USER/stack-overflow/cmake-sdl2-image/CMakeLists.txt
This made me check the contents of
/usr/lib/x86_64-linux-gnu/pkgconfig/sdl2.pc
/usr/lib/x86_64-linux-gnu/pkgconfig/SDL2_image.pc
I noticed that SDL2_image.pc contains
Name: SDL2_image
which I assumed should match the third parameter to PKG_SEARCH_MODULE for this library.
There are two blog posts about this here:
Using SDL2 with CMake
Using SDL2_image with CMake
Basically you need a FindSDL2.cmake and FindSDL2_image.cmake module. They can be based of the ones that work for SDL 1.2 which are included in CMake already. Using these Find modules will also work on Windows.
If you are on Linux and only need SDL2 you don't even need the FindSDL2.cmake as the following already works:
cmake_minimum_required(VERSION 3.7)
project(SDL2Test)
find_package(SDL2 REQUIRED)
include_directories(${SDL2_INCLUDE_DIRS})
add_executable(SDL2Test Main.cpp)
target_link_libraries(SDL2Test ${SDL2_LIBRARIES})
I was having trouble with these answers, I think cmake changed the way to import targets. Following #trenki blog post I needed to change my CMakeLists.txt to:
project(SDL2Test)
find_package(SDL2 REQUIRED COMPONENTS SDL2::SDL2)
add_executable(SDL2Test main.cpp)
target_link_libraries(SDL2Test SDL2::SDL2)
Currently this works out of the box on Arch Linux.
I introduced a modern and portable approach for linking to the SDL2, SDL2_image. These SDL2 CMake modules let you build an SDL2 & SDL2_image project as follows :
list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake/sdl2)
find_package(SDL2 REQUIRED)
find_package(SDL2_image REQUIRED)
target_link_libraries(${PROJECT_NAME} SDL2::Main SDL2::Image)
You should just clone the repo in your project:
git clone https://github.com/aminosbh/sdl2-cmake-modules cmake/sdl2
Note: If CMake didn't find the SDL2/SDL2_image libraries (in Windows), we can specify the CMake options SDL2_PATH and SDL2_IMAGE_PATH as follows:
cmake .. -DSDL2_PATH="/path/to/sdl2" -DSDL2_IMAGE_PATH="/path/to/sdl2-image"
It supports also other related libraries : SDL2_ttf, SDL2_net, SDL2_mixer and SDL2_gfx. For more details, please read the README.md file.
You can find a list of examples/samples and projects that uses these modules here : https://github.com/aminosbh/sdl-samples-and-projects
Since 2.6 version, SDL2_image installation is shipped with CMake config script SDL2_imageConfig.cmake/SDL2_image-config.cmake.
So find_package(SDL2_image) works without any additional FindSDL2_image.cmake module, and creates IMPORTED target SDL2_image::SDL2_image:
find_package(SDL2_image REQUIRED)
target_link_libraries(<executable-target> SDL2_image::SDL2_image)
Note, that variables like SDL2_IMAGE_LIBRARIES or SDL2_IMAGE_INCLUDE_DIRS are NOT set in this case, so using them is meaningless.
The following commands works fine for me:
set(SDL_INCLUDE_DIR "/usr/include/SDL2")
set(SDL_LIBRARY "SDL2")
include(FindSDL)
if(SDL_FOUND)
message(STATUS "SDL FOUND")
endif()