find_path in Cmake doesn't work with simple example, - cmake

I try to locate a document "netcdf.inc" in "/usr/local/include". I have tried PATHS, HINTS. Both of them don't work. In /usr/local/include exists netcdf.inc. Could someone help me? I will be very appreciative.
Best regards
set(netcdf_inc_names "netcdf.inc")
if (NetCDF_INCLUDE_DIRS)
message("it should be here")
if (EXISTS "${NetCDF_INCLUDE_DIRS}")
find_path(cdf_test_include_path
NAMES ${netcdf_inc_names}
PATHS /usr/local/
PATH_SUFFIXES include
NO_DEFAULT_PATH)
message("${cdf_test_include_path}")
if(NOT cdf_test_include_path)
message(SEND_ERROR "Can not locate ${netcdf_inc_names} in ${NetCDF_INCLUDE_DIRS}")
endif()
#...
Output:
it should be here
cdf_test_include_path-NOTFOUND
CMake Error at CMakeLists.txt:145 (message):
Can not locate netcdf.inc in /usr/local/include

Related

Relative paths not recognized with CMake

I'm new to C++ and I would like to use the Pytorch library in C++. The tutorial can be seen here https://pytorch.org/cppdocs/installing.html
I have an issue with my /home/local/CLionProjects/MachineAI/CMakeLists.txt file
cmake_minimum_required(VERSION 3.2)
project(MachineAI)
set(CMAKE_PREFIX_PATH ${CMAKE_SOURCE_DIR}/libtorch)
find_package(Torch REQUIRED)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${TORCH_CXX_FLAGS}")
add_executable(${PROJECT_NAME} main.cpp)
target_link_libraries(${PROJECT_NAME} "${TORCH_LIBRARIES}" )
set_property(TARGET ${PROJECT_NAME} PROPERTY CXX_STANDARD 14)
This produces a lot of errors but they mostly seem to have the same problem. It's not able to find a specific file. Here's an example:
CMake Error at libtorch/share/cmake/Caffe2/Caffe2Config.cmake:14 (include):
include could not find requested file:
public/utils.cmake
The error is suggesting the wrong path is provided. If I take a look inside the libtorch/share/cmake/Caffe2/Caffe2Config.cmake, the problem line is:
include("public/utils.cmake")
It cannot find the file even though the file is located at the correct relative path libtorch/share/cmake/Caffe2/public/utils.cmake
If I replace the path with an absolute path, the error goes away:
include("/home/local/CLionProjects/MachineAI/libtorch/share/cmake/Caffe2/public/utils.cmake")
Here is the screenshot of relevant files for better orientation:
I would like to know if there's a way to fix the CMakeLists.txt file at the top so that Caffe2Config.cmake will be able to find the files through the relative path.

cmake find_path not working for simple file

I'm trying to find a problem why cmake is not able to find a custom openssl version, therefore I debugged the original findOpenssl.cmake.
There they search for the ssl.h file. So, I created a small cmake file which tries to find out the issue:
project(CMAKETEST)
cmake_minimum_required(VERSION 3.16)
set(OPENSSL_CUSTOM_PATH "/home/user/Documents/CMakeTest/openssl/include/openssl/ssl.h")
message(STATUS CUSTOMOPENSSLPATH: ${OPENSSL_CUSTOM_PATH})
find_path(TEST123
NAMES ${OPENSSL_CUSTOM_PATH}
)
message(STATUS ${TEST123})
I created an empty ssl.h file in the above folder, but cmake is not able to find the file. TEST123 is always NOT-FOUND.
Solution:
set(OPENSSL_CUSTOM_PATH "/home/user/Documents/CMakeTest/openssl/include/openssl")
find_path(TEST123 NAMES ssl.h PATHS "${OPENSSL_CUSTOM_PATH}")
Update
This more concrete example does not work as expected:
project(CMAKETEST)
cmake_minimum_required(VERSION 3.16)
set(OPENSSL_CUSTOM_PATH "/home/user/Documents/CMakeTest/openssl/")
find_path(TEST123
NAMES openssl/ssl.h HINTS "${OPENSSL_CUSTOM_PATH}"
PATH_SUFFIX include
NO_DEFAULT_PATH
)
message(STATUS ${TEST123})
when removing NO_DEFAULT_PATH then it finds the system openssl library in /usr/include
Update 2
https://github.com/Kitware/CMake/blob/master/Modules/FindOpenSSL.cmake#L195-L204
find_path(OPENSSL_INCLUDE_DIR
NAMES
openssl/ssl.h
${_OPENSSL_ROOT_HINTS_AND_PATHS}
HINTS
${_OPENSSL_INCLUDEDIR}
${_OPENSSL_INCLUDE_DIRS}
PATH_SUFFIXES
include
)
finds first the path in /usr/include, if I add NO_DEFAULT_PATH it finds it. Probably it was because some caching that it was not found during I wrote this question.
It isn't working because you use it incorrectly, for your case it should be like this:
set(OPENSSL_CUSTOM_PATH "/home/user/Documents/CMakeTest/openssl/include/openssl")
find_path(TEST123 NAMES ssl.h PATHS "${OPENSSL_CUSTOM_PATH}")
Or simply find_path(TEST123 ssl.h PATHS "${OPENSSL_CUSTOM_PATH}")
In your update you have a error PATH_SUFFIX should be PATH_SUFFIXES.

CMake: Not Connecting -lrt to project

I have a project, dependent on the rt library, that shows undeclared errors for functions ddefined in LIBRT when using make following running the "cmake .". I've looked at the plethora of suggestions for questions similar to this but none worked for me. Using the functions check_include_files() and check_library_exists() return TRUE, and it appears I succeed in finding the library using find_library(). I even get "-lrt" appended at the end of the link.txt file.
Anyways, I am a bit stumped and looking for some guidance. Below is my CMakeLists.txt file:
cmake_minimum_required (VERSION 3.5)
project (proj_name C)
enable_language(C)
set (SOURCES
/source/file/path
)
add_definitions (-std=c99)
add_executable (proj_name ${SOURCES})
find_library(LIBRT rt)
if(LIBRT)
target_include_directories (proj_name PUBLIC ${PROJECT_SOURCE_DIR}
/proj/includes/path #not including rt header path
${RT_INCLUDE_DIRS}
)
target_link_libraries(proj_name PUBLIC ${LIBRT})
endif(LIBRT)
set(CMAKE_C_COMPILER /usr/bin/gcc)
include(CheckIncludeFiles)
check_include_files(mqueue.h HAVE_MQUEUE_H)
if(NOT HAVE_MQUEUE_H)
message(FATAL_ERROR "mqueue.h not found")
endif(NOT HAVE_MQUEUE_H)
include(CheckLibraryExists)
check_library_exists(rt timer_gettime "" HAVE_RT)
if(NOT HAVE_RT)
message(FATAL_ERROR "timer_gettime not found")
endif(NOT HAVE_RT)
Running on WSL Ubuntu.
Thanks for any insight possible!

How to tell CMake to find the lib in local folder through command line?

I am trying to link my local version of gflags (which is in ~/mylibs/gflags) while compiling google-test. In gtest's CMakeLists.txt, it's using find_package:
if (WITH_GFLAGS)
find_package (gflags 2.2.0)
if (gflags_FOUND)
set (HAVE_LIB_GFLAGS 1)
determine_gflags_namespace (gflags_NAMESPACE)
endif (gflags_FOUND)
endif (WITH_GFLAGS)
I don't want to modify the CMakeLists.txt file. Is there anyway to tell cmake to find the package in my folder ?
Thanks!
The gflags documentation describes specifying the location of a non-standard installation when using CMake:
The gflags_DIR variable must be set to the <prefix>/lib/cmake/gflags directory containing the gflags-config.cmake file
In your case, this might look like:
cmake -Dgflags_DIR=/home/blackball/mylibs/gflags/lib/cmake/gflags [...]
Since you are integrating gflags as subproject in a super cmake, you basically want find_package() to be a no-op if you have it in your "meta-project" source dir. This can be done by overloading the find_package() command.
Top CMakeLists.txt:
# Use find_package everywhere, no-op if it's a subproject
macro(find_package)
if(NOT TARGET ${ARGV0} AND NOT TARGET ${ARGV0}::${ARGV0})
_find_package(${ARGV})
else()
if(TARGET ${ARGV0})
get_target_property(TGT_VER ${ARGV0} VERSION)
set(TGT ${ARGV0})
else()
get_target_property(TGT_VER ${ARGV0}::${ARGV0} VERSION)
set(TGT ${ARGV0}::${ARGV0})
endif()
message(STATUS "Found ${ARGV0}: CMake Target ${TGT} (found version \"${TGT_VER}\")")
set(${ARGV0}_FOUND TRUE)
endif()
endmacro()
note: for gflags you may need to "force" gflags_NAMESPACE also since gflags it's a source not a binary (cf glog issue ToDo)

How could I replace a find_package of CMakeList by its installation directory?

I need to install SFML by sources but I can't run cmake because a package is not installed (xcb-image)
I installed this packages by sources, but how can I tell CMake that this package is installed, and that it needs to look at a special directory?
if(NOT SFML_OPENGL_ES)
find_package(OpenGL REQUIRED)
include_directories(${OPENGL_INCLUDE_DIR})
if(SFML_OS_LINUX OR SFML_OS_FREEBSD)
find_package(XCB COMPONENTS xlib_xcb image randr REQUIRED)
if(NOT LIBXCB_FOUND)
message(FATAL_ERROR "Xcb library not found")
endif()
include_directories(${LIBXCB_INCLUDE_DIRS})
endif()
endif()
I don't have root access.
Try adding your xcb /include and /lib directory to your CMakeLists, by adding the following lines :
INCLUDE_DIRECTORIES(/path/to/your/xcb/include)
LINK_DIRECTORIES(/path/to/your/xcb/lib)
Otherwise, if that didn't work and you have a cmake file for cxb (sth like xcb.cmake), Create a folder named cmake/Modules/ under your project root, add xcb.cmake under that folder, and in the root CMakeLists.txt, include the following code:
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/Modules/")
For a better understanding, take a look at CMake:How To Find Libraries
Hope that helps !