I am a novice in the field of CMake and I want to compile an example from OpenMVG official samples. However, I do not how to link the third library by CMake. The following is the part of the C++ file that I want to compile.
#include "openMVG/features/feature.hpp"
#include "openMVG/features/sift/SIFT_Anatomy_Image_Describer.hpp"
#include "openMVG/features/svg_features.hpp"
#include "openMVG/image/image_io.hpp"
#include "openMVG/image/image_concat.hpp"
#include "openMVG/matching/regions_matcher.hpp"
#include "openMVG/matching/svg_matches.hpp"
//
#include "third_party/stlplus3/filesystemSimplified/file_system.hpp"
#include <cstdlib>
#include <string>
#include <iostream>
My CMake file is as the following.
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O2 -std=c++11")
add_executable(openMVG_sample_features_siftPutative siftmatch.cpp)
find_package(Eigen3 3.3 REQUIRED)
target_link_libraries(openMVG_sample_features_siftPutative
openMVG_image
openMVG_features
openMVG_matching
Eigen3::Eigen
${STLPLUS_LIBRARY})
target_compile_definitions(openMVG_sample_features_siftPutative
PRIVATE -DTHIS_SOURCE_DIR="${CMAKE_CURRENT_SOURCE_DIR}")
set_property(TARGET openMVG_sample_features_siftPutative PROPERTY FOLDER OpenMVG/Samples)
However, I cannot let the cmake find the file of "third_party/stlplus3/filesystemSimplified/file_system.hpp". I have tried several times and I always got the error that file_system.hpp is not found
Like YaleCheung said, you do not have the path to your include.
Now your second problem is that you do not provide to your project the compiled-object of the method used (from the 3rdlib includes).
To fix this, as you did for the eigen3 library where you have a find_package which (certainly) defines where to find the include and the lib, you should have the equivalent for your third_party library.
So I would add:
include_directories (${thirdLib_INCLUDE_DIRS}) # I let you define this variable (could be define while the configure process)
and then: I think your linking problem is coming from the fact that ${STLPLUS_LIBRARY} might not contain path and lib. Try to "message()" in your cmakelists to see the content of your variable ${STLPLUS_LIBRARY}
Related
I am trying to setup some include paths using CMake and QT Creator and I'd like to get the rid of the path traversal on my includes. What I meant by that is below.
#include <QtTest>
#include <QCoreApplication>
#include <QDebug>
#include "../../../../UI/Controller/PageNavigator/PageNavigatorPT.h" //this works
//#include "PageNavigatorPT.h" // but this what I want
I want what is commented out, so I avoid these huge path traversals on my source. I added this to my cmake to try to fix the issue:
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
add_executable(pageNavigatorTest
../../../../UI/Controller/PageNavigator/PageNavigatorBase.cpp
../../../../UI/Controller/PageNavigator/PageNavigatorPT.cpp
tst_pagenavigatortest.cpp)
target_include_directories(${PROJECT_NAME}
PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}../../../../UI/Controller/PageNavigator
)
add_test(NAME pageNavigatorTest COMMAND pageNavigatorTest)
but it does not work. I still have to spell out the full path traversal.
Is there a way to make CMake make my source find the correct header by just writing:
#include "PageNavigatorPT.h" // but this what I want
thanks #Tsyvarev, this solved the problem (slash in front of the first dir
target_include_directories(${PROJECT_NAME}
PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/../../../../UI/Controller/PageNavigator
)
I can run and compile with no problem, but VSCode Intellisense is saying that it cannot open source file boost/asio.hpp (even tho it obviously can) and is marking it as an error all the time, I want to know why its doing it and how to fix it
I have the following directory:
root/
include/
atr_include.hpp
user_interface.hpp
src/
user_interface.cpp
CMakeList.txt
main.cpp
CMakeList.txt
With CMakeList.txt:
#CMake minimum version
cmake_minimum_required(VERSION 3.0.0)
#C++ Standard version
set(CMAKE_CXX_STANDARD 20)
### Searches for the VCPKG
if(DEFINED ENV{VCPKG_ROOT})
set(CMAKE_TOOLCHAIN_FILE "$ENV{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake")
endif()
project(main LANGUAGES CXX VERSION 0.1.0 )
find_package(Boost COMPONENTS system json REQUIRED)
add_executable(${PROJECT_NAME} main.cpp)
include_directories(include)
link_directories(src)
add_subdirectory(src)
link_libraries(atr_lib)
if(Boost_FOUND)
include_directories(${Boost_INCLUDE_DIRS})
link_libraries(${PROJECT_NAME} ${Boost_LIBRARIES})
endif()
if(MSVC OR MSYS OR MINGW)
target_link_libraries(${PROJECT_NAME} ws2_32)
endif()
src/CMakeList:
add_library(atr_lib STATIC user_interface.cpp)
include/atr_include.hpp:
#include <boost/asio.hpp>
#include <chrono>
#include <iostream>
#include <math.h>
#include <mutex>
#include <thread>
include/user_interface.hpp:
#include "atr_include.hpp"
class UserInterface
{
private:
public:
};
src/user_interface.cpp:
#include <user_interface.hpp>
main.cpp:
#include <user_interface.hpp>
int main() { return 0; }
I had the same problem and it was related to the incorrect configuration of the IntelliSense Configuration Provider item.
"configurations": [
{
...
"configurationProvider": "ms-vscode.cmake-tools"
...
}
The previous value corresponded to another Cmake extension that was no longer installed. After changing the value to ms-vscode.cmake-tools (I have the "CMake Tools" extension installed), IntelliSense started working correctly. Also boost headers includes correctly without errors from VS Code.
VSCode is not a C++ compiler. It is (mostly) a text editor, and some development tools. It merely runs some C++ compiler in order to actually compile and build C++ code.
Your actual C++ compiler is the final and ultimate authority on where it can find various header files.
For whatever reason, your VSCode configuration does not know where boost's header files are installed, so that's what it tells you. Your compiler knows, but that's your compiler, not VSCode.
So, no, VSCode really can't open or find these header files. Your question was:
I want to know why its doing it
Because it simply doesn't know. It's not your C++ compiler, and it doesn't automatically know where all header files are installed.
and how to fix it
That ...depends on your very, very specific computer, operating system (VSCode is available for multiple operating systems), whichever C++ compiler you are actually using with VSCode, and how exactly you are using VSCode (for a brief time, for example, I used VSCode to edit files that weren't even on the same computer, but accessed via ssh). Each one of these factors affects the how everything works, together.
If you know where boost's header files are installed, this may simply be a matter of digging through VSCode's configuration settings, there must be a setting somewhere that tells boost which directories contain header files to search for. If you don't know where boost's header files are installed, your first step will be to figure that part out, before anything else can be done.
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.
I don't understand what I need to do to use a library which is located in /usr/include.
For example: I want to use the json library which is located in /usr/include/json.
In my project 'main.cpp' I do #include <json/json.h>.
I don't get any errros but when I start to use the functions in the library I get undefined reference errors. I have this problem with multiple libraries, I have no idea what to do I searched on google but I only came across vague answers.
I'm pretty sure I need to do something in the CMakeLists.txt file but I have no idea what.
/usr/include is accessible for include by default. But when you include an external library, you must link it to your target. In case you are using cmake this can be done as follows: add to your CMakeLists.txt the following line:
target_link_libraries(your_target_name your_library_name)
For example, on my machine (Fedora 21) jsoncpp package is named jsoncpp, and it's include files are in /usr/include/jsoncpp/json. So I create test.cpp like this
#include <jsoncpp/json/json.h>
#include <iostream>
int main(int, char**)
{
Json::Value val(42);
Json::StyledStreamWriter sw;
sw.write(std::cout, val);
return 0;
}
and CMakeLists.txt
add_executable(test
test.cpp
)
target_link_libraries(test jsoncpp)
and all works well.
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".