Issue linking c++ library with cmake - c++

I am trying to link the following library : nngpp
using the following commands
mkdir build
cd build
cmake ..
make
make install
However when testing the demos or using the library in a project with the following in CMakeLists.txt :
...
add_executable(target main.cpp)
target_link_libraries(target nngpp)
I get the following error:
fatal error: 'nngpp/nngpp.h' file not found
#include <nngpp/nngpp.h>
^~~~~~~~~~~~~~~
1 error generated.
make[2]: *** [CMakeFiles/rest.dir/rest/server.o] Error 1
make[1]: *** [CMakeFiles/rest.dir/all] Error 2
make: *** [all] Error 2
note : The library is header-only. but I don't want to copy it in my project.

With
find_package(nngpp)
using the library is straightforward:
add_executable(target main.cpp)
target_link_libraries(target nng::nngpp)
Here nng::nngpp is IMPORTED library, so it cares about include directories for you.

It's hard to know exactly what's wrong without more information. I would suggest running make VERBOSE=1. This will show you the command line that is being executed when trying to compile the file that gives you the error.
Look in the command line for include flags (e.g. -I flags if you're using gcc). Find the directory for nngpp and double-check if your header files are in there, and what the correct path relative to that directory is to reference the header file. Maybe you need to only #include <nngpp.h> instead?

The following worked :
cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
project(comm)
set(CMAKE_CXX_STANDARD 11)
find_package(nng REQUIRED)
find_package(Threads)
find_package(nngpp REQUIRED)
add_executable(server src/tfo-server.cpp)
target_link_libraries(server nng::nng nng::nngpp)
This also worked:
cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
project(comm)
set(CMAKE_CXX_STANDARD 11)
add_executable(server src/tfo-server.cpp)
include_directories(server /usr/local/include)

Related

Why does gcc can't find opencv.hpp file?

I'm quite new to CMake, but I want to build a test .cpp file that includes OpenCV and shows me an image. I have built OpenCV in the path /usr/local and I have here folder with opencv.hpp file - /usr/local/include/opencv4/opencv2/opencv.hpp. Here is my CMakeLists.txt file:
cmake_minimum_required(VERSION 3.0)
project(cpp_proj)
find_package(OpenCV REQUIRED)
add_executable(cpp_proj opencv_cpp.cpp)
include_directories(${OPENCV4_INCLUDES})
target_link_libraries(cpp_proj )
I opened ~./bashrc and added there lines:
export OPENCV4_INCLUDES=/usr/local/include/
export OPENCV4_LIBRARIES=/usr/local/lib/
export PATH=$PATH:$OPENCV4_LIBRARIES
export PATH=$PATH:$OPENCV4_INCLUDES
When I run cmake in bash - everything is ok and even find_package finds OpenCV. But when I'm trying to run make it gives me a error:
pi#raspberrypi:~/Desktop/cpp_proj/build $ cmake ..
-- Configuring done
-- Generating done
-- Build files have been written to: /home/pi/Desktop/cpp_proj/build
pi#raspberrypi:~/Desktop/cpp_proj/build $ make
[ 50%] Building CXX object CMakeFiles/cpp_proj.dir/opencv_cpp.cpp.o
/home/pi/Desktop/cpp_proj/opencv_cpp.cpp:1:10: fatal error: opencv2/opencv.hpp: No such file or directory
#include <opencv2/opencv.hpp>
^~~~~~~~~~~~~~~~~~~~
compilation terminated.
make[2]: *** [CMakeFiles/cpp_proj.dir/build.make:63: CMakeFiles/cpp_proj.dir/opencv_cpp.cpp.o] Error 1
make[1]: *** [CMakeFiles/Makefile2:76: CMakeFiles/cpp_proj.dir/all] Error 2
make: *** [Makefile:84: all] Error 2
I found questions with the same problem, but they didn't help me. What I am doing wrong? What could be the reason of this? Thanks!
In order to use a library you must specify the include directory as well as the libs.
With find_package (in module mode), in your case it should populate the variables OpenCV_INCLUDE_DIRS and OpenCV_LIBS for you to use.
So I recommend to add / alter your code to add the include directory & link the library (such as below)
target_include_directories(${CMAKE_PROJECT_NAME} public ${OpenCV_INCLUDE_DIRS})
target_link_libraries(${CMAKE_PROJECT_NAME} public ${OpenCV_LIBS})
I don't believe you ever need to touch ~./bashrc when using find_package

C++/CMake can't find libxml++

I'm a Java/Go/Python dev trying to get my feet wet with the C++ toolchain and having a hard time forcing CMake to find dependencies. I installed libxml++ on Debian using apt-get install libxml++2.6-dev and it installed it to /usr/include/libxml++2.6/libxml++. This is a problem, because that's not the right path relative to /usr/include--if I try to #include <libxml++/libxml++.h> it can't find it obviously, and if I include stuff as e.g. #include <libxml++2.6/libxml++/whatever.h> then whatever.h will be unable to find other header files searching for the libxml++ path, e.g. #include <libxml++/version.h>. There are pkg-config files that come with the library but I couldn't really figure out what to do with them, and using stuff like find_package(LibXml++) or include_directories(${LibXml++_INCLUDE_DIRS}) didn't work.
Looking at the CMake docs there appear to be several ways to solve every simple problem, and I feel like this must be a very simple problem with a simple solution if this is related to the way apt-get installs things. Is there something I can add to force CMake to look specifically in /usr/include/[something]/libxml++ and still import it properly with relative paths, or do I need to just move stuff every time I install it with apt?
Apologies if this is a duplicate, but "libxml++" is an incredibly difficult library to Google, both because of the "++" punctuation and the fact that it's apparently not used anywhere near as much as regular libxml2. I'm happy to read more basic resources but would rather not slog through 30 years of the evolution of C++ to arrive at the answer.
For reference:
CMakeLists.txt:
cmake_minimum_required(VERSION 3.10)
project(sandbox)
set(CMAKE_CXX_STANDARD 17)
find_package(Boost REQUIRED)
find_package(LibXml2 REQUIRED)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/ ${LIBXML2_INCLUDE_DIR})
add_executable(sandbox main.cpp)
target_link_libraries(sandbox ${LIBXML2_LIBRARIES})
Source file:
#include <iostream>
#include <libxml++2.6/libxml++/libxml++.h>
int main() {
std::cout << "hello" << std::endl;
}
Error msg:
[main] Building folder: cpp-sandbox
[build] Starting build
[proc] Executing command: /usr/bin/cmake --build /workspaces/cpp-sandbox/build --config Debug --target all -- -j 6
[build] Scanning dependencies of target sandbox
[build] [ 50%] Building CXX object CMakeFiles/sandbox.dir/main.cpp.o
[build] In file included from /workspaces/cpp-sandbox/main.cpp:2:
[build] /usr/include/libxml++-2.6/libxml++/libxml++.h:50:10: fatal error: libxml++/exceptions/internal_error.h: No such file or directory
[build] #include <libxml++/exceptions/internal_error.h>
[build] ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[build] compilation terminated.
[build] make[2]: *** [CMakeFiles/sandbox.dir/build.make:63: CMakeFiles/sandbox.dir/main.cpp.o] Error 1
[build] make[1]: *** [CMakeFiles/Makefile2:73: CMakeFiles/sandbox.dir/all] Error 2
[build] make: *** [Makefile:84: all] Error 2
[build] Build finished with exit code 2
LibXml2 and LibXml2++ are different libraries, so find_package() for one of them - LibXml2 - won't help with another.
CMake isn't shipped with "Find" script for LibXml2++, but since you have .pc file for the library, using it is quite simple:
# Search pkg-config utility first
find_package(PkgConfig REQUIRED)
# Then use pkg-config for locate specific package
pkg_check_modules(LIBXMLXX REQUIRED IMPORTED_TARGET libxml++-2.6)
add_executable(sandbox main.cpp)
# Link with the IMPORTED target created by 'pkg_check_modules'
# That target contains both include directories and actual libraries for link.
target_link_libraries(sandbox PkgConfig::LIBXMLXX)
Note, that first parameter for pkg_check_modules call (LIBXMLXX) just denotes the name of the IMPORTED target you want to create. You can specify any name, as you wish.
But the last parameter for pkg_check_modules call (libxml++-2.6) is the name of the package, it should exactly match to the base name of .pc file. Since you have file libxml++-2.6.pc, exactly libxml++-2.6 should be used as the last parameter for pkg_check_modules.
See that question for more details about using pkg-config with CMake.
You may should add this line to your CMakeLists.txt :
include_directories("/usr/include/libxml++2.6/")
( or use any macro to achieve to this result )
then in your main :
#include <iostream>
#include <libxml++/libxml++.h> // <= like this
int main() {
std::cout << "hello" << std::endl;
}
Another way to do :
If it do not fix your problem you can also try to go to the libxml++ page : https://developer.gnome.org/libxml++-tutorial/stable/chapter-introduction.html .
Here you have a link to download libxml++.
Then you just have to follow the instruction in the file call INSTALL, and add the include directory with :
include_directories("<your-path/libxml++2.6>")
and add a new library path for your compilator when it search lib with
link_directories("<your-other-path>/lib")
Hoping it help you
I've managed to solve the apparently same problem (on Ubuntu 20.04 with g++9) by merging the two answers. Some version change may apply
cmake_minimum_required (VERSION 3.10)
project (sandbox)
find_package (LibXml2 REQUIRED)
find_package (PkgConfig REQUIRED)
pkg_check_modules (LIBXMLXX REQUIRED IMPORTED_TARGET libxml++-2.6)
include_directories (${LIBXML2_INCLUDE_DIR})
include_directories ("/usr/include/libxml++2.6/")
add_executable (sandbox main.cpp)
target_link_libraries (sandbox ${LIBXML2_LIBRARIES} PkgConfig::LIBXMLXX)
And with main.cpp source
#include <libxml++/libxml++.h>
int main() { return 0; }

fatal error: allegro5\allegro.h: No such file or directory yet found on cmake

So I did a game on Visual Studio long ago. 2 years ago I uploaded the sources to github NEGU93/ForbiddenDesert just to have it there. Now I changed from windows to linux and I wanted to go back and compile this game for linux.
I remember I used allegro for the GUI so I installed it following this steps Installing Allegro 5. When I coded the game I remember downloading an allegro prepared for Visual Studio directly so I didn't have any problems.
I created a CMakeLists.txt (never did that before, so I am quite new in that area):
# Specify the minimum version for CMake
cmake_minimum_required(VERSION 2.8)
# projects name
project(ForbiddenDesert)
set(CMAKE_CXX_STANDARD 11) # enable C++11 standard
# Set the output folder where your program will be created
set(CMAKE_BINARY_DIR ${CMAKE_SOURCE_DIR}/bin)
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR})
set(LIBRARY_OUTPUT_PATH ${CMAKE_BINARY_DIR})
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/")
# Finds Allegro using pkgconfig, so it must be configured correctly
find_package(Allegro5 REQUIRED)
# Set include and lib dirs.
include_directories(${ALLEGRO_INCLUDE_DIR})
set(FD_LIBS ${LIBS} ${OBJC_LIBRARIES} ${ALLEGRO_LIBRARIES})
# The following folder will be included
# include_directories(${PROJECT_SOURCE_DIR}/src)
# include_directories(${PROJECT_SOURCE_DIR}/include)
include_directories("${PROJECT_SOURCE_DIR}")
file(GLOB all_SRCS
"*.h"
"*.cpp"
)
add_executable(forbidden ${all_SRCS})
target_link_libraries(forbidden ${FD_LIBS})
I am using a file named FindAllegro5.cmake inside a folder named cmake/. The file is a copy of eruta/FindAllegro5.cmake.
When running cmake . I get the following:
-- Found PkgConfig: /usr/bin/pkg-config (found version "0.29.1")
-- Checking for module 'allegro-5'
-- Found allegro-5, version 5.2.4
-- Found Allegro5: /usr/lib/liballegro.so;/usr/lib/liballegro_image.so;/usr/lib/liballegro_font.so;/usr/lib/liballegro_primitives.so;/usr/lib/liballegro_ttf.so;/usr/lib/liballegro_audio.so;/usr/lib/liballegro_dialog.so;/usr/lib/liballegro_memfile.so;/usr/lib/liballegro_acodec.so;/usr/lib/liballegro_color.so;/usr/lib/liballegro_main.so;/usr/lib/liballegro_physfs.so
So far so good. Yet, when I run make
In file included from /home/ubuntu/Documents/GitHub/ForbiddenDesert/Button.h:5:0,
from /home/ubuntu/Documents/GitHub/ForbiddenDesert/ArcheologistButton.h:4,
from /home/ubuntu/Documents/GitHub/ForbiddenDesert/ArcheologistButton.cpp:1:
/home/ubuntu/Documents/GitHub/ForbiddenDesert/allegro.h:5:10: fatal error: allegro5\allegro.h: No such file or directory
#include <allegro5\allegro.h>
^~~~~~~~~~~~~~~~~~~~
compilation terminated.
CMakeFiles/forbidden.dir/build.make:62: recipe for target 'CMakeFiles/forbidden.dir/ArcheologistButton.cpp.o' failed
make[2]: *** [CMakeFiles/forbidden.dir/ArcheologistButton.cpp.o] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/forbidden.dir/all' failed
make[1]: *** [CMakeFiles/forbidden.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2
I've seen that the FindAllegro5.cmake has:
# Include dir
find_path(Allegro5_INCLUDE_DIR
NAMES allegro5/allegro5.h
PATHS ${Allegro5_PKGCONF_INCLUDE_DIRS}
)
So I changed the include to #include <allegro5/allegro5.h> but still no solution.
I haven't been able to find the solution (I've found plenty of information about the "fatal error: allegro5\allegro.h: No such file or directory" but haven't found one that applies to my case).
So I installed allegro with the following: Quickstart Allegro and then I created the hello.c file and compiled it as it says there (gcc hello.c -o hello $(pkg-config allegro-5 allegro_font-5 --libs --cflags)) and it worked. So the problem will be how to add those flags to the cmakelists.txt
So I tried:
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} $(pkg-config allegro-5 allegro_font-5 allegro_primitives-5 allegro_ttf-5 allegro_image-5 allegro_audio-5 allegro_acodec-5 --libs --cflags)")
But still didn't work.
Running find /usr/ -name "allegro*"
I found the header allegro5.h on /usr/include/allegro5/allegro5.h and some other files on /usr/lib/pkgconfig/.
So I did:
INCLUDE_DIRECTORIES( /usr/include/allegro5 )
LINK_DIRECTORIES( /usr/lib/pkgconfig )
TARGET_LINK_LIBRARIES(game liballegro.a )
Still not working.
I tried adding the libraries directly from the build I compiled from the source code on github (following the info I found here Allegro and CMake) and still didn't work:
#Include Allegro
include_directories(/home/ubuntu/Documents/GitHub/allegro5/build/include)
include_directories(/home/ubuntu/Documents/GitHub/allegro5/build/lib/Headers)
link_directories(/home/ubuntu/Documents/GitHub/allegro5/build/lib)
#connect all the libraries you need
set(game_LIBS liballegro.so liballegro_dialog.so liballegro_image.so)
target_link_libraries(game ${game_LIBS})
Under Ubuntu I would just install allegro with apt-get:
apt-get install liballegro5-dev
Then the includes would be at the right place (/usr/include/allegro5/...)
You say you installed allegro, but I'm not too sure whether it would be the same as from a package. If it was from a build, it is often that the default installation directory is /usr/local instead of /usr. So the includes would be in /usr/local/include/allegro5/...
That being said, your include() command in cmake is correct:
include_directories(${ALLEGRO_INCLUDE_DIR})
However, your libraries handling is completely wrong. You never have to change LD_FLAGS. (At least, in the last 6 or 7 years I've used cmake, I never had a need.) Instead, you want to use the target_link_libraries(). Something like this:
target_link_libraries(${PROJECT_NAME} ${ALLEGRO_LIBRARIES})
It will work within (as in after) a project(...).
If you want to verify that the variables are correctly named, you may use the message() command. Some libraries use names such as NAME_LIBRARY or NAME_INCLUDE_PATH. They should follow the proper naming convention, but nothing forces them to, so you often find some funkiness there.
The following will print a message out:
message("allegro libraries = " ${ALLEGRO_LIBRARIES})
If the variable name is not correct (or the library was not found), you will see nothing. If you got it right, it will show you the variable libraries. I would imagine that is right, but the LD_FLAGS was probably what you got wrong.

Include external libraries into CLion project through CMake

I struggle to get GLFW Windows pre-compiled binaries working within my CLion Project. Those libraries are placed in a external directory. I do not want them to be in my project library but should (of course) be shipped when releasing the application. I am new to C++ but I thought to accomplish this might be as easy as it is in Java (Intellij Idea -> dependencies -> ...).
GLFW Windows pre-compiled binaries
I use MinGW 5.0 and CMake 3.10.2;
My CMakeLists.txt:
cmake_minimum_required(VERSION 3.10)
project(Hatsudouki_core)
set(CMAKE_CXX_STANDARD 17)
link_directories(F:\\C++\\ExternalLibraries\\GLFW\\lib-mingw-w64)
include_directories(F:\\C++\\ExternalLibraries\\GLFW\\include)
add_executable(Hatsudouki_core main.cpp)
target_link_libraries(Hatsudouki_core glfw3)
Main.cpp
#include <iostream>
#include <GLFW/glfw3.h>
int main() {
if (!glfwInit())
std::cout << "error!" << std::endl;
else
std::cout << "success!" << std::endl;
return 0;
}
Build output
"F:\C++\CLion 2018.1\bin\cmake\bin\cmake.exe" --build C:\Users\simon\CLionProjects\Hatsudouki-core\cmake-build-debug --target Hatsudouki_core -- -j 4
[ 50%] Linking CXX executable Hatsudouki_core.exe
CMakeFiles\Hatsudouki_core.dir/objects.a(main.cpp.obj): In function `main':
C:/Users/simon/CLionProjects/Hatsudouki-core/main.cpp:5: undefined reference to `glfwInit'
collect2.exe: error: ld returned 1 exit status
mingw32-make.exe[3]: *** [Hatsudouki_core.exe] Error 1
CMakeFiles\Hatsudouki_core.dir\build.make:96: recipe for target 'Hatsudouki_core.exe' failed
CMakeFiles\Makefile2:66: recipe for target 'CMakeFiles/Hatsudouki_core.dir/all' failed
mingw32-make.exe[2]: *** [CMakeFiles/Hatsudouki_core.dir/all] Error 2
CMakeFiles\Makefile2:78: recipe for target 'CMakeFiles/Hatsudouki_core.dir/rule' failed
mingw32-make.exe[1]: *** [CMakeFiles/Hatsudouki_core.dir/rule] Error 2
Makefile:117: recipe for target 'Hatsudouki_core' failed
mingw32-make.exe: *** [Hatsudouki_core] Error 2
I tried following solutions mentioned here:
- GLFW doc and GLFW doc2 (find package does not work, no CMake file)
- Github issue report related to Github issue report 2 which then leads to the solution to put FindGLFW.cmake into some directory? Tried to put it here GLFW\FindGLFW.cmake but does not work- Linking did not work as well as mentioned here: Stackoverflow
Image GLFW directory: GLFW Windows pre-compiled binaries
I think I just do not understand how CMake, external Libraries and C++ work together to accomplish this fairly easy task. I believe comparison to Java would help (used to work with gradle)
EDIT 1
As suggested I added following:
I put the Findglfw3.cmake into PROJECT/cmake/Modules/:
# Copyright (c) 2015 Andrew Kelley
# This file is MIT licensed.
# See http://opensource.org/licenses/MIT
# GLFW_FOUND
# GLFW_INCLUDE_DIR
# GLFW_LIBRARY
find_path(GLFW_INCLUDE_DIR NAMES F:\\C++\\ExternalLibraries\\GLFW\\include\\GLFW\\glfw3.h)
find_library(GLFW_LIBRARY NAMES glfw glfw3)
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(GLFW DEFAULT_MSG GLFW_LIBRARY GLFW_INCLUDE_DIR)
mark_as_advanced(GLFW_INCLUDE_DIR GLFW_LIBRARY)
And added following lines into my CMakeLists.txt:
find_package(glfw3 REQUIRED)
include_directories(${glfw3_INCLUDE_DIRS})
set(LIBS ${LIBS} ${glfw3_LIBRARIES})
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/Modules/")
target_link_libraries(hatsudouki_core ${LIBS})
I also tried in the Findglfw3.cmake:
find_path(GLFW_INCLUDE_DIR NAMES GLFW/glfw3.h)
which is the same in the original file. Both do not work; error:
"F:\C++\CLion 2018.1\bin\cmake\bin\cmake.exe" --build C:\Users\simon\CLionProjects\Hatsudouki-core\cmake-build-debug --target Hatsudouki_core -- -j 4
CMake Error at CMakeLists.txt:6 (find_package):
-- Configuring incomplete, errors occurred!
By not providing "Findglfw3.cmake" in CMAKE_MODULE_PATH this project has
See also "C:/Users/simon/CLionProjects/Hatsudouki-core/cmake-build-debug/CMakeFiles/CMakeOutput.log".
asked CMake to find a package configuration file provided by "glfw3", but
CMake did not find one.
Makefile:175: recipe for target 'cmake_check_build_system' failed
Could not find a package configuration file provided by "glfw3" with any of
the following names:
glfw3Config.cmake
glfw3-config.cmake
Add the installation prefix of "glfw3" to CMAKE_PREFIX_PATH or set
"glfw3_DIR" to a directory containing one of the above files. If "glfw3"
provides a separate development package or SDK, be sure it has been
installed.
mingw32-make.exe: *** [cmake_check_build_system] Error 1
As explained here
Make Findglfw3.cmake file in PROJECT/cmake/Modules/ which looks like
# GLFW_FOUND
# GLFW_INCLUDE_DIR
# GLFW_LIBRARY
set(FIND_GLFW_PATHS "F:\\C++\\ExternalLibraries\\GLFW")
find_path(GLFW_INCLUDE_DIR NAMES GLFW/glfw3 GLFW/glfw3.h PATH_SUFFIXES include PATHS ${FIND_GLFW_PATHS})
find_library(GLFW_LIBRARY NAMES glfw3 glfw3.a libglfw3 libglfw3.a PATH_SUFFIXES lib-mingw PATHS ${FIND_GLFW_PATHS})
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(GLFW DEFAULT_MSG GLFW_LIBRARY GLFW_INCLUDE_DIR)
mark_as_advanced(GLFW_INCLUDE_DIR GLFW_LIBRARY)
Define Module Path in CMakeLists.txt
#Define module path
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake/Modules")
Also Link include directory and library with project in CMakeLists.txt
#Define static GLFW libraries and header files
find_package(glfw3 REQUIRED)
include_directories(${GLFW_INCLUDE_DIR})
...
target_link_libraries(Hatsudouki_core ${GLFW_LIBRARY})
At first you need to install glfw package. If you are using MSYS2 then it can be installed using pacman -S mingw-w64-x86_64-glfw on 64-bit windows and pacman -S mingw-w64-i686-glfw on 32-bit windows. And you need to use find_package to let the cmake locate the glfw library automatically instead of manually locating them. If you are not using MSYS2 then it might be little difficult. Check out this code.
cmake_minimum_required(VERSION 3.10)
project(Hatsudouki_core)
set(CMAKE_CXX_STANDARD 17)
add_executable(Hatsudouki_core main.cpp)
find_package(glfw3 REQUIRED) # Find the GLFW library
target_link_libraries(Hatsudouki_core PRIVATE glfw) # Finally, link to them.
I suggest you to use MSYS2 If you need pre-compiled libraries. It has a very good package manager called as pacman.

C++ / compilation of a program fatal error: QtGui/qwidget.h: No such file or directory

I've downloaded a linux application which required qt4.
I have it already on my computer.
I have this following error when I want to compile the program using make in the terminal.
[ 6%] Building CXX object CMakeFiles/util_convert.dir/moc_planeviewer.cxx.o
In file included from /usr/include/qt4/QtGui/QMainWindow:1:0,
from /home/Desktop/plane/src/planeviewer.h:3,
from /home/Desktop/plane/src/moc_planeviewer.cxx:9:
/usr/include/qt4/QtGui/qmainwindow.h:45:27: fatal error: QtGui/qwidget.h: No such file or directory
#include <QtGui/qwidget.h>
^
compilation terminated.
make[2]: *** [CMakeFiles/util_convert.dir/moc_planviewer.cxx.o] Error 1
make[1]: *** [CMakeFiles/util_convert.dir/all] Error 2
make: *** [all] Error 2
I don't understand because I checked already everything.
I have the following input for those different commands line:
qmake --version
QMake version 2.01a
Using Qt version 4.8.6 in /usr/lib/x86_64-linux-gnu
locate qwidget.h
/opt/qt/5.4/android_armv7/include/QtWidgets/qwidget.h
/opt/qt/5.4/gcc_64/include/QtWidgets/qwidget.h
/usr/include/qt4/Qt/qwidget.h
/usr/include/qt4/QtGui/qwidget.h
So the file already exists.
The CmakeList in case
find_package(Boost 1.58 REQUIRED COMPONENTS serialization program_options python)
find_package(PythonLibs 2.6 REQUIRED)
set(QT_USE_QTSVG TRUE)
set(QT_USE_QTXML TRUE)
set(QT_USE_QTOPENGL TRUE)
find_package(OpenGL)
find_package( Qt4 REQUIRED )
INCLUDE_DIRECTORIES("${Boost_INCLUDE_DIRS}")
INCLUDE_DIRECTORIES("${PYTHON_INCLUDE_PATH}")
INCLUDE_DIRECTORIES(${QT_QTCORE_INCLUDE_DIR} ${QT_QTGUI_INCLUDE_DIR} ${QT_QTSVG_INCLUDE_DIR}
${QT_QTXML_INCLUDE_DIR} ${QT_QTOPENGL_INCLUDE_DIR} ${OPENGL_INCLUDE_DIRS})
QT4_WRAP_UI(util_convert_FORMS planeviewer.ui)
QT4_WRAP_CPP(util_convert_MOCS planeviewer.h planewidget.h)
ADD_EXECUTABLE(util_convert ${util_convert_FORMS} ${util_convert_MOCS} plane.cpp util_convert.cpp planeviewer.cpp planewidget.cpp objects.cpp coordinates.cpp utils.cpp)
TARGET_LINK_LIBRARIES(util_convert ${Boost_LIBRARIES} ${QT_QTCORE_LIBRARY} ${QT_QTGUI_LIBRARY} ${QT_QTSVG_LIBRARY}
${QT_QTXML_LIBRARY} ${QT_QTOPENGL_LIBRARY} ${OPENGL_LIBRARIES})
How I compile:
Go to src/ then do cmake . and finally make.
The error happened at the make step.
Before posting this, I already searched on the internet but didn't find a solution yet.
Can someone helps me ?
The problem is in INCLUDE_DIRECTORIES. I think that in your example QT_QTGUI_INCLUDE_DIR variable has value /usr/include/qt4/QtGui, but you need add /usr/include/qt4 to INCLUDE_DIRECTORIES, because in code include statement useses path <QtGui/qwidget.h>:
INCLUDE_DIRECTORIES("/usr/include/qt4")
Thanks to #gomons, I was able to compile it.
I added in the CmakeList the following line:
INCLUDE_DIRECTORIES("/usr/include/qt4/")