CMAKE - find_library find_path find_file all not working - cmake

So I have a library here /path/to/CMSIS/libarm_cortexM4lf_math.a
I can't seem to find it using find_library with Cmake. Further more I can't find a simple text file with find_file or find_path.
I've tried the following (and many iterations thereof):
find_library(CMSIS_LIB libarm_cortexM4lf_math.a PATHS /path/to/CMSIS)
So then I thought I'd do something more simple and saved a file to /home/user/happy.txt and tried to
find_path(TEST_FILE happy.txt /home/user)
In all cases my variables are NOTFOUND.
I've read the cmake documentation for find_library, most of find_path, and skimmed find_file. They all seem to work the same way and I can't get any of them to work.
What am I missing here?

Related

cmake: stop if a manually specified variable is not used by the project

if I give as an input parameter an undefined variable to CMake, it will simply warn me about the fact that this variable is not used by the project.
So, I wonder whether one can tell cmake to stop in case of undefined variables given by the user.
The only approach I came so far to, was to parse the cmake output log and search for strings like:
CMake Warning:
Manually-specified variables were not used by the project
I had a look at the man of cmake, without finding something which could do the job.
so how can i tell cmake, that instead of a simple warning, that I may miss, it should stop on CMake Warning about unknown variables?
The solution, if any, should not involve modifying the CMakeList.txt files.
Of course, I can download the cmake sources and change in the
cmake::RunCheckForUnusedVariables()
method:
this->IssueMessage(cmake::WARNING, msg.str());
by:
this->IssueMessage(cmake::FATAL_ERROR, msg.str());
but if i could avoid doing that hack, I will be grateful!
thanks for your help.

CMake does not find CxxTest package

I wrote the following CMakeLists.txt in order to build a tester using CxxTest (it's almost the standard example provided by FindCxxTest):
cmake_minimum_required(VERSION 2.8)
project(tester)
set(CMAKE_INCLUDE_PATH "${CMAKE_CURRENT_SOURCE_DIR}")
find_package(CxxTest REQUIRED)
if(CXXTEST_FOUND)
include_directories(${CXXTEST_INCLUDE_DIR})
enable_testing()
CXXTEST_ADD_TEST(tester_executable runner.cpp
${CMAKE_CURRENT_SOURCE_DIR}/myTestsuite.h)
endif()
The problem is that CxxTest is not found although I have the folder cxxtest and the file cxxtest/TestSuite.h inside the directory where CMakeLists.txt exists.
I already looked into the source code of FindCxxTest.cmake to see how far it gets, but it doesn't even find the path CXXTEST_INCLUDE_DIR.
Do I have to set another search path or something? I'm testing on Windows.
find_path does not search the current list directory. That is, just because a relative path is reachable from the current CMakeLists.txt, does not mean that it will be found. You can find a complete description of the search order in the manual.
A quick and dirty solution is to add the respective directory to your CMAKE_PREFIX_PATH:
set(CMAKE_PREFIX_PATH ${CMAKE_CURRENT_LIST_DIR})
find_package(CxxTest)
More fine grained control is available via the CMAKE_SYSTEM_PREFIX_PATH, CMAKE_INCLUDE_PATH, CMAKE_LIBRARY_PATH and CMAKE_PROGRAM_PATH variables. Also, some find scripts provide means of injecting search hints without having to pollute the global variables (although FindCxxTest does not seem to offer this).
Another option is, if you anyway include the CxxTest sources at a fixed place in your source tree, to not use the the find script at all, but hardcode the paths instead.
This may feel like a dirty solution at first, but if you can really rely on the sources always being there, it's actually the cleanest and most robust way to solve this. Find scripts really only make sense when you can not say where exactly on the system the files are located.

Pass target file name as compile definition in cmake

I have CMakeLists.txt with many targets and the targets have configuration-specific suffixes defined. Additionally I want to know the name of the executable inside the program.
Under MSVC++ I do this by defining
add_definitions("-DTARGET_FILE_NAME=\"$(TargetFileName)\"")
But this does not work in other generators. Now that CMake got generator expressions, is there a way that would work with any generator?
I tried something like
add_definitions(-DTARGET_FILE_NAME=$<TARGET_FILE_NAME:$<TARGET_PROPERTY:NAME>>)
but even
add_definitions(-DTARGET_FILE_NAME=$<TARGET_PROPERTY:NAME>)
just places the unescaped $<TARGET_PROPERTY:NAME> in the buildfile. I also tried with just$`, but no luck either.
Note that the compile build command does not know the name of the linker output in many build files, so there does not seem to be any generator-specific hack for some of them either.
compile_definitions does not support generator expressions. Use target_compile_definitions, which does:
target_compile_definitions(somelib PRIVATE NAME=$<TARGET_FILE_NAME:somelib>)
http://www.cmake.org/cmake/help/v3.1/command/target_compile_definitions.html

find libavahi with cmake

I need to add libavahi-client3 to cmake dependency of my project. Also need to check libavahi-client3 and libavahi-common3 existence. Problems only with current library(avahi)
Trying to do these things:
find_package(libavahi-common3)
if(NOT libavahi-common3_FOUND)
message(ERROR " libavahi-common3 is not found")
endif(NOT libavahi-common3_FOUND)
OR this variant:
find_library(AVAHI_COMMON_LIBRARY NAMES libavahi-common3)
if(NOT AVAHI_COMMON_LIBRARY_FOUND)
message(ERROR " libavahi-common3 is not found")
endif(NOT AVAHI_COMMON_LIBRARY_FOUND)
Both do not work, i searched for something like findAvahi.cmake, but found nothing. So should i write my own search module or there is another better option?
There is currently no find script for avahi shipping with CMake, which is why your first example does not work. It is important to understand that find_package simply runs an external find script, it does not perform any searching by itself.
Your second example is broken, mixing idioms from find_library and find_package. Please read up on the documentation of find_library and find_path which will help you find the required libraries and include paths.
If you want you can turn that into a find script later (look at the scripts in CMake's module directory to get an idea what such a script should look like), which will allow you to use the more compact find_package for locating the library again. Note that writing a find script that works reasonably well on different platforms is a complex task that will require some research effort to get it right.

find_library or link_directories or find_package? What is better way? Error - Link libraries using cmake

Given
The file /usr/lib/gstreamer-0.10/libgstffmpeg.so is present
Making changes in CMakeLists.txt
Approach 1 find_library()
find_library(GST_FFMPEG NAMES gstffmpeg PATHS /usr/lib/gstreamer-0.10/ )
...
target_link_libraries( MyLibraryOrMyExecutable ${GST_FFMPEG} )
When I run make with above configuration(approach 1), I get the following errors
/bin/ld: warning: libvpx.so.1, needed by /usr/lib/i386-linux-gnu/libavcodec.so.53, not found (try using -rpath or -rpath-link)
/bin/ld: warning: libschroedinger-1.0.so.0, needed by /usr/lib/i386-linux-gnu/libavcodec.so.53, not found (try using -rpath or -rpath-link)
/bin/ld: warning: libgsm.so.1, needed by /usr/lib/i386-linux-gnu/libavcodec.so.53, not found (try using -rpath or -rpath-link)
Looks like the added library depends on more libraries which are not linked! I can see above 3 .so files in /usr/lib. So one possible solution to approach 1 could be to add three more find_library() functions. right ?
May be not- This question explores the issues found in above possible solution
Q1. Is there any other method which saves the effort of finding the
dependent libraries and linking them ? An approach using which all the dependent libraries get automatically linked ?
Approach 2 link_directories()
link_directories(/usr/lib/gstreamer-0.10/)
target_link_libraries( MyLibraryOrMyExecutable gstffmpeg )
When I run make with above configuration(approach 2), I get the following errors
bin/ld: cannot find -lgstffmpeg
Q2. How to solve the issue with above approach 2?
Q3. Which approach 1 or 2 is better ?
P.S. Tried reading documentation for cmake and searching on SO but could not resolve my problem.
I tried two approaches and have issues with both them
To answer your Q3 first, I think the preferred method is approach 1.
From the docs for link_directories:
Note that this command is rarely necessary. Library locations returned by find_package() and find_library() are absolute paths. Pass these absolute library file paths directly to the target_link_libraries() command. CMake will ensure the linker finds them.
Regardless of the approach you take, I don't know of an easy way to automatically get the list of these "sub-dependencies" and add them. I'd just do a find_package or find_library for each.
At least that way, if a dependency isn't found, your project fails at CMake configure time rather than at link time.
I encounter similar scenario, I solve it by add in CMakesList.txt, just before find_library(), add this command:
set(CMAKE_PREFIX_PATH /the/path/to/your/lib/)
,but normally if your dependencies libraries are in usr/lib or usr/local/lib paths, it could find by well called the functions.