I am very new to cmake, but I am using it on Visual Studio to develop a program that has to run on linux. I need to include the following in this manner:
#include <xscontroller/xscontrol_def.h>
#include <xscontroller/xsdevice_def.h>
#include <xscontroller/xsscanner.h>
#include <xstypes/xsoutputconfigurationarray.h>
#include <xstypes/xsdatapacket.h>
#include <xstypes/xstime.h>
#include <xscommon/xsens_mutex.h>
However, the files are only recognize by visual studio when I do the following:
#include "xscontroller/xscontrol_def.h"
#include "xscontroller/xsdevice_def.h"
#include "xscontroller/xsscanner.h"
#include "xstypes/xsoutputconfigurationarray.h"
#include "xstypes/xsdatapacket.h"
#include "xstypes/xstime.h"
#include "xscommon/xsens_mutex.h"
The structure of my project in VS is fairly simple:
ANT
-out
-xscommon
-xscontroller
-xstypes
-ANT.cpp
-CMakeLists.txt
.
.
.
The includes I need are in the three xs folder, and I believe they have to be referenced with <> both in visual studio and when the code is compiled onto linux, as the references within each header are done in <> form, which is what causes this error:
xscallbackplainc.h:68:10: fatal error: xstypes/pstdint.h: No such file or directory
#include <xstypes/pstdint.h>
^~~~~~~~~~~~~~~~~~~
at compilation.
Concisely, I really just need to know what command (whether it be in CMakeLists.txt or somewhere else) will allow this kind of referencing within the project and the compiled project over ssh on linux. I am aware of the difference between #include "" and #include <>, I am however new to cmake, and have looked everywhere and cannot find an answer.
The simplest way to achieve this is using include_directories command. Simply add the following to your ANT/CMakeLists.txt:
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
Though I would highly recommend using target_include_directories() instead. The difference between the two is that target_include_directories() specifies include directories just for one target[1].
[1]. A target is anything specified via add_executable() or add_library():
cmake_minimum_required(VERSION 3.12)
project(ANT)
add_executable(ANT ANT.cpp) #other source files as necessary
#format of target_include_directories:
# target_include_directories(target_name PUBLIC|PRIVATE PATH_TO_DIR)
target_include_directories(ANT PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
I have posted the question about linking, hopefully it makes sense. Should be clear I don't know what I'm doing.
Related
In Qt 6.4.0, we can use such code to include qt components:
#include <QtCore/qchar.h>
#include <QtCore/qbytearray.h>
#include <QtCore/qbytearrayview.h>
#include <QtWidgets/qtwidgetsglobal.h>
But I found that the real paths of those .h file are NOT under such folder like QtCore, QtWidgets etc. , actually most of them are under such directory:
/Users/tony/Qt/6.4.0/macos/lib/QtXXX.framework/Headers/qtxxx.h
I'm wondering that since QtCore is not the real path but Headers, Shouldn't we write #include "Headers/qtxxx.h" ? how can #include <QtCore/qchar.h> such path works?
Using the -I argument
You have to tell to your compiler which directory you want to include to find the headers properly. For example in clang and gcc you can use this directory as -I argument, like this:
clang++ yourprogram.cpp -I /Users/tony/Qt/6.4.0/macos/lib/QtXXX.framework/Headers/
And in your source code you will write includes at this way:
#include "QtCore/qchar.h"
#include "QtCore/qbytearray.h"
#include "QtCore/qbytearrayview.h"
#include "QtWidgets/qtwidgetsglobal.h"
Note it uses " " instead < >, this means you are finding the included files relative to your compile options instead of your system.
Although this works, it's recommended to use relative paths of your project to point headers directories.
It's been solved: This phenomenon only shows on MacOS since MacOS uses "framework" to organize a set of lib and headers, which can be parsed by the clang-module name to the realpath.
I am trying to set up my environment with C++ and Python.
I need pybind11.
In some files of pybind11 there is the line #include <Python.h> included.
However it tells me
#include errors detected. Please update your includePath. Squiggles are disabled for this translation unit (C:\Users\d91675\Documents\GitHub\Test_PyBind\PyBind_Add.cpp). C/C++(1696)
cannot open source file "Python.h" C/C++(1696)
I would be happy if you could help me to understand the include functionality.
In places like #include <iostream> I guess this is directly included in the C++ installation.
I can also add modules myself with relative directory like #include "subfolder/module.cpp".
Since I cannot find Python.h within the pybind11 library, I would guess, it is not available.
Do I need to create it with CMake?
e.g. as described here: https://pybind11.readthedocs.io/en/stable/basics.html
Or what could be a reason this Python.h reference is missing?
I am really happy about an explanation for dummies.
Best regards,
Till
I downloaded the latest version of Boost libraries 1_60_0 and I tried to use it but I quickly ran into troubles.
boost::unordered_map<int, int> map;
This piece of code says "namespace boost has no member unordered_map". I checkd the file, it is there though. The same happened for basically everything I tried to acces from the boost namespace.
Header includes are as follows:
#include <D:/IP/boost_1_60_0/boost/graph/adjacency_list.hpp>
#include <D:/IP/boost_1_60_0/boost/graph/graph_traits.hpp>
#include <D:/IP/boost_1_60_0/boost/graph/connected_components.hpp>
#include <D:/IP/boost_1_60_0/boost/unordered_map.hpp>
#include <D:/IP/boost_1_60_0/boost/graph/floyd_warshall_shortest.hpp>
#include <D:/IP/boost_1_60_0/boost/numeric/ublas/matrix.hpp>
#include <D:/IP/boost_1_60_0/boost/numeric/ublas/io.hpp>
I'm guessing I should include something more, but no clue what. Any tips?
unordered_map.hpp includes other boost header files this way:
#include <boost/config.hpp>
Which means that the boost folder has to be set as an additional include directory for this to work.
I'm assuming you're compiling on MSVC, if so, right click your project : properties -> C/C++ -> General and add the folder D:/IP/boost_1_60_0/ as Additional Include Directory.
The docs also answers this question for you.
I am trying to run this code
http://dlib.net/dlib/statistics/cca.h.html
As you can notice, it contains many include, which I copy them.
But inside each include there are a lot of includes like this :
#include "../matrix.h"
It contains
#include "matrix/matrix.h"
#include "matrix/matrix_utilities.h"
#include "matrix/matrix_subexp.h"
#include "matrix/matrix_math_functions.h"
#include "matrix/matrix_assign.h"
#include "matrix/matrix_la.h"
#include "matrix/symmetric_matrix_cache.h"
#include "matrix/matrix_conv.h"
#include "matrix/matrix_read_from_istream.h"
#include "matrix/matrix_fft.h"
#include "matrix/matrix_generic_image.h"
Is there any method to just include the main class? For example give the directory or the link of the classes?
I bet the problem lies with your include directories.
I assume you downloaded the full zip file from http://dlib.net/ (latest version seems to be 18.18). Inside that .zip you have a bunch of folders: examples, tools, dlib. In dlib folder you have all the header files.
You should add the path to wherever you extracted the .zip contents to the "Additional Include Directories" property of your project:
Then just use dlib in your own code as showed in the examples, for instance 3d_point_cloud_ex.cpp:
#include <dlib/gui_widgets.h>
#include <dlib/image_transforms.h>
Initially there is no need to create extra header files, all seems to be provided with a nice folder structure. I encourage you to read dlib's documentation before start using it.
You might also want to check this answer to another question to help you build a handy project folder structure.
I want to compile this example in vtk, which includes the following include files:
#include <vtkSmartPointer.h>
#include <vtkObjectFactory.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkActor.h>
// headers needed for this example
#include <vtkImageViewer2.h>
#include <vtkDICOMImageReader.h>
#include <vtkInteractorStyleImage.h>
#include <vtkActor2D.h>
#include <vtkTextProperty.h>
#include <vtkTextMapper.h>
// needed to easily convert int to std::string
#include <sstream>
Originally it should be compiled with a CMakeLists.txt-file which looks like:
cmake_minimum_required(VERSION 2.8)
PROJECT(ReadDICOMSeries)
find_package(VTK REQUIRED)
include(${VTK_USE_FILE})
add_executable(ReadDICOMSeries MACOSX_BUNDLE ReadDICOMSeries)
if(VTK_LIBRARIES)
target_link_libraries(ReadDICOMSeries ${VTK_LIBRARIES})
else()
target_link_libraries(ReadDICOMSeries vtkHybrid vtkWidgets)
endif()
The problem is: When I simply copy this code and compile it, I get a lot of reference errors (for example: Undefined reference to 'vtkDICOMImageReader::SetDirectoryName(char const*)'). This leads me to the assumption that I should link some libraries to it. Unfortunately the CMakeLists-file does not tell me which libraries. How do I find that out?
Cmake is really strongly suggested for compiling VTK and related projects, especially as a beginner. I only use CMake, but I got an idea of what happens under the hood by checking the properties of the projects already built. In the example you provided, the CMake file only use "target_link_libraries(ReadDICOMSeries ${VTK_LIBRARIES}" , as far as I understood that it is providing to the linker ALL the libraries built with vtk.
To see what CMake will load with that instruction, check the file "VTKConfig.cmake" in the vtk build directory.
If you do it manually, you will also have to include a lot of directories
I managed to figure this out without using CMake directly or having to dig into each individual lib.
find a header file or C/CPP file that defines the object that's giving you linker pain.
Do a find-in-files of the *.cmake files that came with VTK, searching for that filename. So in your case, look for vtkDICOMImageReader.
I found vtkDICOMImageReader in vtkIOKit.cmake, so a decent guess might be libvtkio.
In my case, I was hunting for vtkDataSetAttributes, and this method successfully determined it was in vtkFiltering.lib.
If you've already got Cmake set up then doing it correctly might be easier, but I didn't want "set up vtk" to turn into "set up cmake so I can set up vtk" and then into "set up some other thing so I can set up cmake so I can set up vtk".