I am trying to compile a code snippet with Eigen using CMake, but CMake seems to have trouble finding the correct path, even if the source code is there.
This is my CMakeLists.txt file.
cmake_minimum_required(VERSION 3.14.2)
project(test)
# set default build type
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Release)
endif()
find_package(OpenCV 3 REQUIRED)
find_package (Eigen3 3.3 REQUIRED NO_MODULE)
include_directories(
include
${OpenCV_INCLUDE_DIRS}
${EIGEN3_INCLUDE_DIR}
)
add_executable(playground playground.cpp)
target_link_libraries(playground ${OpenCV_LIBS} Eigen3::Eigen)
This is how I import headers in playground.cpp:
#include <iostream>
#include <vector>
#include <map>
#include <math.h>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <string>
#include <Eigen>
My Eigen is installed under /usr/include/eigen3, here is the directory structure:
yuqiong#yuqiong-G7-7588:/usr/include/eigen3$ ls
Eigen signature_of_eigen3_matrix_library unsupported
So the correct path to Eigen library is /usr/include/eigen3/Eigen.
However, when I run cmake .. and then make for the aforementioned CMakeLists.txt file, with the -LH flag, CMake complains it can't find Eigen. This is the path it thinks where Eigen is:
yuqiong#yuqiong-G7-7588:/media/yuqiong/DATA/vignetting/catkin_ws/src/vig2/src/build$ cmake -LH ..
-- Configuring done
-- Generating done
-- Build files have been written to: /media/yuqiong/DATA/vignetting/catkin_ws/src/vig2/src/build
-- Cache values
// Choose the type of build, options are: None Debug Release RelWithDebInfo MinSizeRel ...
CMAKE_BUILD_TYPE:STRING=
// Install path prefix, prepended onto install directories.
CMAKE_INSTALL_PREFIX:PATH=/usr/local
// The directory containing a CMake configuration file for Eigen3.
Eigen3_DIR:PATH=/usr/local/share/eigen3/cmake
// The directory containing a CMake configuration file for OpenCV.
OpenCV_DIR:PATH=/opt/ros/kinetic/share/OpenCV-3.3.1-dev
This is the structure of the folder where it think Eigen is:
yuqiong#yuqiong-G7-7588:/usr/local/share/eigen3$ tree
.
└── cmake
├── Eigen3Config.cmake
├── Eigen3ConfigVersion.cmake
├── Eigen3Targets.cmake
└── UseEigen3.cmake
I am confused why CMake has such behavior? How can I let it find the correct path of Eigen in this case?
All this environment set-ups are so tricky to get it right...
Thanks for the comments above. I think the issue might be the Eigen team changed their directory structure somehow between different versions, and the old include path does not work anymore.
The solution for me was to change #include <Eigen/Dense> to #include <eigen3/Eigen/Dense>. After that g++ and CMake can find the path.
I did install Eigen following this official doc, which was why I was surprised the installation is still problematic.
This answer solved my problem.
This answer also solves the problem.
On another note, my CMake file was a bit messed up, with both include_directories and target_link_libraries.
Related
I have the following main.cpp, very simple script, trying to re-produce the problem and isolate to it's most basic.
#include<iostream>
#include<fmt/core.h>
// #include "json/json.hpp"
// #include <json/json.hpp>
// #include <nlohmann/json.hpp>
// #include "json.hpp"
// #include "nlohmann/json.hpp"
int main(){
fmt::print("Hello, world!\n");
return 0;
}
The commented out include statements are all of the paths I have tried to get this into my program.
My CMakeLists.txt file looks like this
cmake_minimum_required(VERSION 3.24)
project(main)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_FLAGS "-std=c++17")
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/build)
# include(FetchContent)
# FetchContent_Declare(
# json
# GIT_REPOSITORY https://github.com/nlohmann/json
# )
# FetchContent_MakeAvailable(json)
include(FetchContent)
FetchContent_Declare(fmt
GIT_REPOSITORY https://github.com/fmtlib/fmt
GIT_TAG 9.0.0
)
FetchContent_MakeAvailable(fmt)
add_executable(main main.cpp)
# target_link_libraries(main json)
target_link_libraries(main fmt)
I've commented out the json part since it's not working obviously, I can also json-src and json-build in my _deps folder so I know for a fact that it is being downloaded into my local machine.
After running cmake CMakeLists.txt and make and then running the executable I get the error in the title 'nlohmann/json.hpp' file not found
Any help would be appreciated.
Tried to reproduce the example with another popular C++ package which worked fine. Tried changing the include statements to see if maybe I had something wrong since I don't fully understand all the nuance with CMake and am quite new to it.
You can either specifically use the interface target already included in the nlohmann library, which will automatically populate the correct include path for you, with:
target_link_libraries(main nlohmann_json::nlohmann_json)
Or you would need to specifically include the include path yourself:
include_directories(${json_SOURCE_DIR}/include)
With either way, you will be able to use #include <nlohmann/json.hpp> in your source.
I am trying to create a simple example using the boost library. I can successfully use CMake for the initial setup and it finds boost.
using the following code in CMakeLists.txt:
cmake_minimum_required(VERSION 3.18)
project(edge_detector)
find_package(Boost REQUIRED)
include_directories(${Boost_INCLUDE_DIRS})
add_executable(edge_detector main.cpp)
target_include_directories(edge_detector PUBLIC ${Boost_INCLUDE_DIRS})
target_link_libraries(edge_detector ${Boost_LIBRARIES})
However when I try to build the project using make or CMake --build . boost is not found and I am met with this error:
I am not sure what I am missing, I would be grateful for any help
Your include directive must include a file not a directory.
Replace
#include <boost/algorithm/string>
with
#include <boost/algorithm/string.hpp>
I am trying to write an OpenGL application with GLFW. The file structure is as follows
-USG
-build
-include
-glfw-3.3
-src
-CMakeLists.txt
-main.cpp
-CMakeLists.txt
The CMake in the USG file is as follows.
cmake_minimum_required(VERSION 3.0)
project(USG)
#--------------------------------------------------------------------
# Set GLFW variables
#--------------------------------------------------------------------
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 subdirectories
#--------------------------------------------------------------------
add_subdirectory(include/glfw-3.3)
add_subdirectory(src)
And the CMakeLists in the src file is as follows
add_executable(usg main.cpp)
set(OpenGL_GL_PREFERENCE GLVND)
find_package(OpenGL REQUIRED)
if (OPENGL_FOUND)
target_include_directories(usg PUBLIC ${OPENGL_INCLUDE_DIR})
target_link_libraries(usg ${OPENGL_gl_LIBRARY})
endif()
target_link_libraries(usg glfw)
I run cmake ../ followed by make in the build folder. Running cmake runs as expected. Running make builds glfw fine but when it gets to main.cpp it fails at #include <GLFW\glfw3.h> telling me GLFW\glfw3.h: No such file or directory. It seems to me like the cmake isn't properly linking the header file to the main.cpp file, but I don't know enough about cmake to get that to work. I've tried looking through the examples provided in the glfw-3.3 example folder but I couldn't make enough sense of it to solve my problem. I've tried drawing what knowledge I could from the many similar problems on stack overflow but none of them could help.
I'm following this tutorial here. I want the OpenGL application to be relatively portable, which is why I'm compiling glfw from source rather than using a binary. I'm building on Linux, if that matters.
Try #include <GLFW/glfw3.h> instead of #include <GLFW\glfw3.h>
Backslashes in include paths are a bad (Windows)-practice and do not work with GCC.
https://gcc.gnu.org/onlinedocs/cpp/Include-Syntax.html
I'm working on a Cmake based C++ project in QtCreator. I am trying to use mlpack in my project, but as soon as I include the line #include <mlpack/core.hpp> to my only source file, I get an error pertaining to some libxml files that are included by mlpack/core.hpp:
In file included from /usr/local/include/mlpack/core/util/save_restore_utility.hpp:26:0,
from /usr/local/include/mlpack/core.hpp:171,
from /home/revinci/code/workspaces/qt_ws/Semantic_Perception/src/features_slic.cpp:18:
/usr/include/libxml2/libxml/parser.h:15:31: fatal error: libxml/xmlversion.h: No such file or directory
#include <libxml/xmlversion.h>
Now, I went into /usr/include/libxml2/libxml/ and found parser.h with the line #include <libxml/xmlversion.h> in it.
So, I saw that xmlversion.h and parser.h are in the same folder and tried a hack: I changed the #include <libxml/xmlversion.h> in parser.h to #include "xmlversion.h" only to get the following error:
In file included from /usr/include/libxml2/libxml/parser.h:15:0,
from /usr/local/include/mlpack/core/util/save_restore_utility.hpp:26,
from /usr/local/include/mlpack/core.hpp:171,
from /home/revinci/code/workspaces/qt_ws/Semantic_Perception/src/features_slic.cpp:18:
/usr/include/libxml2/libxml/xmlversion.h:13:31: fatal error: libxml/xmlexports.h: No such file or directory
#include <libxml/xmlexports.h>
Which is basically telling me that it can't find xmlexports.h (included by xmlversion.h). More importantly, xmlexports.h is in the same directory as xmlversion.h and parser.h!
I tried the solution mentioned here and installed libxml2-dev (again) and libxslt1-dev, but my problem wasn't solved.
I think this may have something to do with specifying my include paths correctly. I've tried to add /usr/include/libxml2 to the various path environment variables (PATH, INCLUDE_PATH and CPATH) that are present in my build environment in QtCreator, but to no avail. My CMakeLists.txt looks like this:
project(Semantic_Perception)
cmake_minimum_required(VERSION 2.8)
#Vigra Headers
include_directories(
include
)
file(GLOB_RECURSE VigraImpex include/impex/)
add_library(VigraImpex ${VigraImpex})
#LibXml2 Headers
find_package(LibXml2 REQUIRED)
#Armadillo Headedrs
find_package(Armadillo REQUIRED)
include_directories(${ARMADILLO_INCLUDE_DIRS})
#Boost Headers
find_package(Boost 1.54.0 REQUIRED)
add_executable(features_slic src/features_slic.cpp)
target_link_libraries(features_slic
VigraImpex
${ARMADILLO_LIBRARIES}
)
BTW: LibXml2, Armadillo and Boost are all dependencies of the library I am trying to use - mlpack. The command find_pakcage(mlpack) won't work because there is no Findmlpack.cmake file on my system anywhere, and I couldn't find one on the internet either.
I hope someone can help me.
I have a simple CMakeLists.txt in order to build my project on Ubuntu. I'm using CMake 2.8.1 and at the moment this is the code:
cmake_minimum_required(VERSION 2.4.6)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} /home/user/workspace)
include($ENV{ROS_ROOT}/core/rosbuild/rosbuild.cmake)
# Set the build type. Options are:
# Coverage : w/ debug symbols, w/o optimization, w/ code-coverage
# Debug : w/ debug symbols, w/o optimization
# Release : w/o debug symbols, w/ optimization
# RelWithDebInfo : w/ debug symbols, w/ optimization
# MinSizeRel : w/o debug symbols, w/ optimization, stripped binaries
#set(ROS_BUILD_TYPE RelWithDebInfo)
rosbuild_init()
find_package(OpenCV 2)
#set the default path for built executables to the "bin" directory
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)
#set the default path for built libraries to the "lib" directory
set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib)
#uncomment if you have defined messages
#rosbuild_genmsg()
#uncomment if you have defined services
#rosbuild_gensrv()
#common commands for building c++ executables and libraries
#rosbuild_add_library(${PROJECT_NAME} src/example.cpp)
#target_link_libraries(${PROJECT_NAME} another_library)
#rosbuild_add_boost_directories()
#rosbuild_link_boost(${PROJECT_NAME} thread)
#rosbuild_add_executable(example examples/example.cpp)
#target_link_libraries(example ${PROJECT_NAME})
include_directories(${CMAKE_SOURCE_DIR}/include
${OpenCV_INCLUDE_DIRS})
rosbuild_add_executable (RosPub src/paste.cpp)
target_link_libraries (RosPub openni_driver usb-1.0 ${OpenCV_LIBS})
I need to add opencv libraries on my project. I have added them but i can't still get my code to work. its keeps posting me this error:
‘struct MyOpenNIExample::ImgContext’ has no member named ‘image’
there is a few of them.
after i added find_package(OpenCV REQUIRED to the CMakeLists.txt,
i get this error
Adjust CMAKE_MODULE_PATH to find FindOpenCV.cmake or set OpenCV_DIR to the
directory containing a CMake configuration file for OpenCV. The file will
have one of the following names:
OpenCVConfig.cmake
opencv-config.cmake
what shld i do? I am using Apple and using Ubuntu 10.04.
Since i need
#include "opencv2\opencv.hpp"
#include "opencv2\highgui\highgui.hpp"
#include "opencv2\imgproc\imgproc.hpp"
i added
find_package(OpenCV 2),
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} /home/user/workspace) and
include_directories(${CMAKE_SOURCE_DIR}/include
${OpenCV_INCLUDE_DIRS})
target_link_libraries (RosPub openni_driver usb-1.0 ${OpenCV_LIBS})
#include "opencv2\opencv.hpp"
#include "opencv2\highgui\highgui.hpp"
#include "opencv2\imgproc\imgproc.hpp"
the include files above are included in the vision_opencv in ROS. so to include it, add the opencv dependency in the manifest file.
That would help.
Get FindOpenCV.cmake from above link and put it anywhere on your computer.
After cmake_minimum_required add line set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} [path to folder where you put FindOpenCV.cmake])
Add find_package(OpenCV) to your CMakeLists.txt
On this step you can check for OpenCV_FOUND and other OpenCV variables in your CMakeLists.txt