cmake file to include relative directory out of current directory - c++

my folder like
my_dir
CMakeLists.txt
my_cpp.cpp
common_utils
common_util.h
common_util.cpp
In my_dir/CMakeLists.txt
include_directories(${CMAKE_SOURCE_DIR}/../common_utils)
In my_cpp.cpp I use
#include "common_util.h"
but I got compiling error for "no such file or directory" of "common_util.h".
How to solve the problem in CMakeLists.txt? Thanks.

Related

Installing spdlog by copying contents of include directory

I'm trying to use spdlog as a logging library, but I do not want to use my package manager (apt) to install it. I have copied the spdlog directory under include to the root of my project directory, but when I try to compile I get include errors because the #include directives in the spdlog headers are all absolute (relative to the project root), instead of relative, gcc cannot find the files.
0:10: fatal error: spdlog/common.h: No such file or directory
#include "spdlog/common.h"
Short of changing all the includes in the spdlog library to relative, how can I use spdlog in my project? I'm also using CMake to compile my project; I have a build directory in my project root and from within it I call cmake .. and then make to compile; is there something I need to add to my CMakeLists.txt file to include spdlog?
You need to add the project root directory into the cmake include_directories, like this:
include_directories(
/absolute/path/
)
Or in the project root directory, edit CMakeLists.txt and add this:
include_directories(
.
)

Link to static library by cmake

I have a project C++ using libnuma library. Because I don't have permission to install libnuma in the root system, so I have to install it in folder of user: /home/khangtg/opt. This folder contains 2 main folders:
Folder include contains: numacompat1.h, numa.h, numaif.h
Folder lib contains: libnuma.a, libnuma.la, libnuma.so, libnuma.so.1, libnuma.so.1.0.0
Now, I have a file .cpp include libnuma library:
#include <numa.h>
and I build the project by file CMakeLists.txt with content:
add_library (common Bigraph.cpp AdjList.cpp Vocab.cpp NumaArray.cpp clock.cpp)
set (LINK_LIBS ${LINK_LIBS} common gflags numa )
add_executable (warplda main.cpp lda.cpp warplda.cpp)
add_executable (format format.cpp)
target_link_libraries (warplda ${LINK_LIBS})
target_link_libraries (format ${LINK_LIBS})
After run cmake command, I get some error that is "can not include numa.h".
So, how can I fix this error and build the project by cmake. Many thanks!
you want to set the link_directories to include the directory of the libraries. More can be found in the cmake docs. This tells the linker where to look for the libraries.
It should probably look something like this
link_directories(/home/khangtg/opt/lib)
Also add the include directories command from this documentation.
This will look like this
include_directories(/home/khangtg/opt/include)
This might be useful to add to your cmake build file:
include_directories("/home/khangtg/opt/include")
From: cmake tutorial
You may also want to change the include to :
#include "numa.h"

Where is my Makefile looking for c++ headers?

I am working with opencv 3.0, and getting the following error:
error:
'opencv2/imgproc/imgproc.hpp' file not found
#include "opencv2/imgproc/imgproc.hpp"
So I looked in /usr/local/include/ for the file path and was able to find it. This tells me that my make file must be looking in the wrong place for header files. The make file is generated by cmake using the command cmake . from the directory containing CMakeLists.txt So I figure cmake must be generating the make file with the wrong path for headers.
So does anyone know where I can find what path cmake is trying to use? Or can I find this from the make file generated by cmake?
In CMakeLists.txt there is a line: target_link_libraries( BlobDetector ${OpenCV_LIBS} ) is the variable OpenCV_LIBS the path name used? If so how can I find what this path is?

Incorporating a project with CMakeLists.txt into my main project

I have been given a C++ project, which has the following structure:
/main.cpp
/CMakeLists.cpp
/engine/engine.cpp
/engine/CMakeLists.txt
/utils/utils.cpp
/utils/CMakeLists.txt
In /utils/utils.cpp, there is the line #include <cuda_runtime.h>. Now, on my machine, this header file is located at /usr/local/cuda-7.0/targets/x86_64-linux/include. But if I print out cmake's include directories in all three of the above CMakeLists.txt, using:
get_property(dirs DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY INCLUDE_DIRECTORIES)
foreach(dir ${dirs})
message(STATUS "include dir = '${dir}'")
endforeach()
then it just prints out the following: /usr/include, /usr/local/include, and /usr/local/cuda-7.0/include.
Despite this -- the code compiles. Why is this? How is cmake able to find this header file when it is not in the search directory for include files?
Now, I will explain the real reason I am asking this question. I want to use this code in another project of mine. So, I created a directory foo in my project, and added the contents from above. Then I added add_subdirectory(foo) to my main CMakeLists.txt file. I printed out the include directories, and got the same outputs as before. However, this time, it did not compile. It gave me the error message:
fatal error: cuda_runtime.h: No such file or directory
Can anybody explain to me why it might be that it is compiling in the original form, but not compiling when incorporate into my project?

ITK installation and sample program

I am new to ITK and I did the following step to install ITK and use it to programme in VS2010
Downloaded ITK 4.3.1 and build it with CMAKE
The build was successful and I had a lib->Debug folder containing the libs.
Added the bin folder path to environmental vairable path.
Following is my simple code...
#include <iostream>
#include <Core/Common/include/itkImage.h>
using namespace itk;
using namespace std;
int main()
{
return 0;
}
the above code returns
Cannot open include file: 'itkConfigure.h'
I tried searching for that header but no luck. However in C:\InsightToolkit-4.3.1\Modules\Core\Common\src I found itkConfigure.h.in file. I am really clueless about how to go about this .in file. Any help is most welcome..
The easiest way to set up your project is to use CMake again. Try creating a project with just a CMakeLists.txt and main.cpp. The CMakeLists.txt should have something like:
cmake_minimum_required(VERSION 2.8 FATAL_ERROR)
project(ItkTest)
find_package(ITK REQUIRED)
include(${ITK_USE_FILE})
add_executable(MyTest main.cpp)
target_link_libraries(MyTest ITKCommon)
So say you create these 2 files in a dir called ItkProject, then from a Visual Studio Command Prompt just do:
cd <path to ItkProject>
mkdir build
cd build
cmake .. -DITK_DIR="<path to build dir of ITK>"
The <path to build dir of ITK> is where you ran CMake from to configure the ITK project. It will contain the ITK.sln file, but critically it should also contain a file called ITKConfig.cmake. It is this file which is searched for in the cmake command find_package(ITK REQUIRED) - if CMake can't find it the configuring will fail.
Once that's been found, it sets a bunch of CMake variables which you can then use in your own CMakeLists.txt, including ITK_USE_FILE.
When you then invoke include(${ITK_USE_FILE}), this goes on to set up things like your includes paths, library search paths, and compiler flags. The path <path to ItkProject>/Core/Common/include will be added to the includes dirs, so in your main.cpp, you just need to do:
#include <Core/Common/include/itkImage.h>
#include "itkImage.h"
Anyway, the end result after running CMake should be solution file <path to ItkProject>\build\ItkTest.sln which is set up ready to use ITK.
I checked \ItkConfig.cmake and paths defined there should match physical paths, this is the case if ITK build has been untouched (directory wasn't renamed).
# The ITK source tree.
# For backward compatibility issues we still need to define this variable, although
# it is highly probable that it will cause more harm than being useful.
# Use ITK_INCLUDE_DIRS instead, since ITK_SOURCE_DIR may point to non-existent directory
IF(NOT ITK_LEGACY_REMOVE)
SET(ITK_SOURCE_DIR "C:/ITK320")
ENDIF(NOT ITK_LEGACY_REMOVE)
# The ITK include file directories.
SET(ITK_INCLUDE_DIRS "C:/ITK320-build;C:/ITK320/Code/Algorithms;C:/ITK320/Code/BasicFilters;C:/ITK320/Code/Common;C:/ITK320/Code/Numerics;C:/ITK320/Code/IO;C:/ITK320/Code/Numerics/FEM;C:/ITK320/Code/Numerics/NeuralNetworks;C:/ITK320/Code/SpatialObject;C:/ITK320/Utilities/MetaIO;C:/ITK320/Utilities/NrrdIO;C:/ITK320-build/Utilities/NrrdIO;C:/ITK320/Utilities/DICOMParser;C:/ITK320-build/Utilities/DICOMParser;C:/ITK320-build/Utilities/expat;C:/ITK320/Utilities/expat;C:/ITK320/Utilities/nifti/niftilib;C:/ITK320/Utilities/nifti/znzlib;C:/ITK320/Utilities/itkExtHdrs;C:/ITK320-build/Utilities;C:/ITK320/Utilities;C:/ITK320/Code/Numerics/Statistics;C:/ITK320/Utilities/vxl/v3p/netlib;C:/ITK320/Utilities/vxl/vcl;C:/ITK320/Utilities/vxl/core;C:/ITK320-build/Utilities/vxl/v3p/netlib;C:/ITK320-build/Utilities/vxl/vcl;C:/ITK320-build/Utilities/vxl/core;C:/ITK320-build/Utilities/gdcm;C:/ITK320/Utilities/gdcm/src")
# The ITK library directories.
SET(ITK_LIBRARY_DIRS "C:/ITK320-build/bin")