I am new to CMake and DBus. I am following along the guide here and make a basic program compile and execute.
The first problem that I ran into was my program will not find
<dbus/dbus.h>
I got around that issue by adding some include directories to my CMakeList.txt.
Currently, my CMakeLists.txt looks like this:
...
include_directories(/usr/lib/)
include_directories(/usr/include/dbus-1.0/)
include_directories(/usr/lib/x86_64-linux-gnu/dbus-1.0/include)
include_directories(/usr/include/glib-2.0)
include_directories(/usr/lib/x86_64-linux-gnu/glib-2.0/include/)
set (LIBS
dbus-1
dbus-glib-1
)
add_executable(mydbus mydbus.cpp)
target_link_libraries(mydbus ${LIBS} )
Now, my program is complaining about not being able to find dbus-arch-deps.h
/usr/include/dbus-1.0/dbus/dbus.h:29:33: fatal error: dbus/dbus-arch-deps.h: No such file or directory
#include <dbus/dbus-arch-deps.h>
I know that the solution for this is to use proper command line flags or pkg-config. As discussed here and numerous other posts.
However, I do not know how to configure CMakeLists.txt to have similar effect.
My guess would be to add something like find_package(dbus-1) to CMakeLists.txt. And if that is correct, I am going to have to write my own Finddbus-1.cmake. Does this sound correct? Or is there an easier way?
I will appreciate any pointers.
You may get an existing FindDBus.cmake script (e.g., this one), copy it into your project, and use as
find_package(DBus REQUIRED)
# Use results of find_package() call.
include_directories(${DBUS_INCLUDE_DIRS})
add_executable(mydbus mydbus.cpp)
target_link_libraries(mydbus ${DBUS_LIBRARIES})
Alternatively, as you know pkgconfig can find DBus, you may use CMake module PkgConfig. Actually, FindDBus.cmake script, referenced above, uses PkgConfig module in its implementation. Possible usage could be:
find_package(PkgConfig REQUIRED) # Include functions provided by PkgConfig module.
pkg_check_modules(DBUS REQUIRED dbus-1) # This calls pkgconfig with appropriate arguments
# Use results of pkg_check_modules() call.
include_directories(${DBUS_INCLUDE_DIRS})
link_directories(${DBUS_LIBRARY_DIRS})
add_executable(mydbus mydbus.cpp)
target_link_libraries(mydbus ${DBUS_LIBRARIES})
However, using link_directories is not recommended, it is better to use absolute paths to libraries in target_link_libraries() call. That is why it is better to combine pkg_check_modules with find_library, as it is done in the referenced Find script. That answer describes generic way for use result of pkgconfig in CMake.
Related
For my current CMake-project, i'm in need for the numpy/ndarrayobject.h-library. At first the project didn't compile and complained about numpy/ndarrayobject.h not being found in the include-path.
So what i did was sudo find / -type f -name 'ndarrayobject.h' and it spit out:
/usr/lib/python3.8/site-packages/numpy/core/include/numpy/ndarrayobject.h
which i included in CMakeLists.txt:
include_directories(/usr/lib/python3.8/site-packages/numpy/core/include/)
So, there is no actual error here. The project compiles and works as i expected it to. However, i think there must be a smarter way to find the directory. I have seen CMake-projects use
find_package(PythonLibs 3 REQUIRED)
find_package(OpenCV REQUIRED)
or something along these lines. So my initial thought is to learn about such macros to include the numpy-directory a bit smarter.
What are those macros called? Where are they defined? Where can i learn about them? Does such a macro exist for the above mentioned directory?
Is this maybe the wrong approach and there is a cleaner way to find and include the above mentioned directory?
However, i think there must be a smarter way
Yes, you are right, and you are on the right track.
What are those macros called? Where are they defined? Where can i learn about them?
Those are called "find modules", and are usually located in (assuming Unix/Linux): /usr/local/share/cmake-<version>. You can learn about them in the official documentation: https://cmake.org/cmake/help/v3.14/manual/cmake-developer.7.html#find-modules
However: The ability to find NumPy seems to have been added in CMake 3.14: https://cmake.org/cmake/help/v3.14/module/FindPython3.html.
So, assuming you are using CMake 3.14 or later, this should do the trick:
find_package(Python3 REQUIRED COMPONENTS NumPy)
# Use PUBLIC or INTERFACE scope if you need to
# propagate the include folder to dependents
target_include_directories(<your target> PRIVATE ${Python3_NumPy_INCLUDE_DIRS})
Here we use FindPython3.cmake, which exports a whole heap of variables related to Python, including the one you need: Python3_NumPy_INCLUDE_DIRS.
Edit: As #Pedro correctly pointed out, you can also link to NumPy and get access to its include that way. This is actually preferable, since you may have to link to NumPy libraries anyway when building your application, and this saves you from having to issue two separate commands.
target_link_libraries(<your target> Python3::NumPy)
Now you can include said header: #include <numpy/ndarrayobject.h>
If you are unable to use CMake 3.14 or newer, you can consult the latest find modules to see how it's done and simply write your own module, or build the latest CMake from source (which is trivial).
I added the xgboost library as a git submodule of my project and I'm trying to add it to cmake as a subdirectory. Unfortunately it's not working. A simple hello world project with the following CMakeLists.txt replicates the error that I'm getting.
cmake_minimum_required(VERSION 3.2)
project(foo)
add_subdirectory(xgboost)
add_executable(${PROJECT_NAME} foo.cpp)
target_link_libraries(${PROJECT_NAME} xgboost)
After building the library there is nothing in the xgboost/lib directory so I get the following error.
clang: error: no such file or directory:
'/Users/.../myproject/xgboost/lib/libxgboost.dylib'
I think that the problem is generated in their CMakeLists file since they have two different targets. Maybe cmake is choosing the wrong target but I'm not familiar enough with cmake to figure it out. The following code is from xgboost's CMakeLists.
# Executable
add_executable(runxgboost $<TARGET_OBJECTS:objxgboost> src/cli_main.cc)
set_target_properties(runxgboost PROPERTIES
OUTPUT_NAME xgboost
)
set_output_directory(runxgboost ${PROJECT_SOURCE_DIR})
target_link_libraries(runxgboost ${LINK_LIBRARIES})
# Shared library
add_library(xgboost SHARED $<TARGET_OBJECTS:objxgboost>)
target_link_libraries(xgboost ${LINK_LIBRARIES})
set_output_directory(xgboost ${PROJECT_SOURCE_DIR}/lib)
#Ensure these two targets do not build simultaneously, as they produce outputs with conflicting names
add_dependencies(xgboost runxgboost)
My questions in order of importance are:
Is there any way to fix it without modifying xgboost's CMakeLists.txt file?
Is it reasonable to try to add xgboost to my project as a git submodule?
Is there any reason cmake is not instructing to build the library?
Note: There were several edits to this question since I tried to narrow down the problem and to provide more information.
(I would love to ask for few things beforehand in the comment section, but I have too low reputation to do so, so I will just give it a shot ;))
I have few suspects, and one of them is ${CMAKE_SOURCE_DIR} of the submodule's root CMakeLists.txt. Although the paths are set properly when you run that CMakeLists.txt alone, cmake gets confused the moment you add it as your subdirectory. Have you looked into another directories for your output binaries?
First I would suggest testing this hypothesis, and then I would suggest writing similar, but separate CMakeLists.txt file for xgboost library, and then substitute it in the project temporarily. Unfortunately the CMakeLists.txt filename is hardcoded and there is no possibility to have two files of that kind in one directory; so it seems that the answer to 1) is, that you rather have to change the file.
For the 2): as long as it does not require huge additional logic in your CMakeLists.txt, it makes sense. Other viable option is to create an install target, which you can use to install your xgboost library locally (using CMAKE_INSTALL_PREFIX(doc) variable), and then add the installation path to your CMAKE_LIBRARY_PATH(doc).
I Ubuntu, I am learning about cmake and make, and just trying a simple example. I have two directories: src and build. In src, I have two files: main.cpp, and CMakeLists.txt, which has (only) the following text:
add_executable(test main.cpp)
link_directories(/usr/lib/x86_64-linux-gnu)
target_link_libraries(test protobuf)
In /usr/lib/x86_64-linux-gnu, there is a shared library called libprotobuf.so, which I want to link against. My main.cpp uses functions in this library, by including the releveant header file, #include <google/protobuf/message.h>.
Now, in my build directory, I run cmake ../src, and then make. However, I then get linker errors telling me that there are undefined references to some of the functions in the protobuf library. If I do a search through all the files and subdirectories in build, there is not mention of anything related to protobuf.
However, if I remove the link_directories line in my CMakeLists.txt file, and instead write the full path to the library when specifying the executable, i.e. target_link_libraries(test /usr/lib/x86_64-linux-gnu/libprotobuf.so), it compiles and links fine.
Why is link_directories not allowing cmake to find this library?
Do not use link_directories like this in CMake.
This is a common beginner's mistake, as many other build environments work like this, but in CMake it's just asking for trouble. Even the official documentation specifically advises against it:
Note that this command [link_directories] 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.
So instead, always pass absolute paths to target_link_libraries and use find_library to resolve the link directory:
find_library(PROTOBUF_LIBRARY protobuf HINTS /usr/lib/x86_64-linux-gnu)
target_link_libraries(test PUBLIC ${PROTOBUF_LIBRARY})
This has the huge benefit that you will probably get a diagnostic at CMake configure time if the expected library cannot be found, instead of a random linker error at compile time. Also, this allows the user to specify a library location via the GUI if the target machine has a non-standard directory layout.
So if it doesn't work right away, be sure to check the result of the find_library call and consult the official documentation to track down why it doesn't find your library as intended.
Make sure that your call to link_directories takes place before your call to the relevant add_executable.
I had mistakenly believed it only needed to be before the call to target_link_libraries, but that's not the case. After moving the call, the library is linked properly.
Make sure that the order will be link_directories, set PROJECT_LINK_LIBS, add_executable and then target_link_libraries.
Below is example to demonstarte it:
cmake_minimum_required(VERSION 2.8.9)
project (Logging)
include_directories(include)
file(GLOB LOGGINGSOURCES "libsrc/*.cpp")
file(GLOB SOURCES "src/*.cpp")
add_library(convertString SHARED ${LOGGINGSOURCES})
install(TARGETS convertString DESTINATION /root/Deepak/)
link_directories( /root/Deepak/ )
set(PROJECT_LINK_LIBS libconvertString.so)
add_executable(hello ${SOURCES})
target_link_libraries(hello ${PROJECT_LINK_LIBS} )
Perhaps it's very old topic but none of proposed solutions worked for me. So I had to make my own dirty hack. I do crosscompiling with buildroot and include toolchainfile.cmake.
#...
set(LIB_PATH ${PROJECT_SOURCE_DIR}/relative/path/to/your/lib)
#...
include_directories(/path/to/library/include)
set(LIB_MYLIB ${LIB_PATH}/libmylib.so)
#...
add_executable(${PROJECT_NAME} ${APP_SOURCES})
target_link_libraries(${PROJECT_NAME}
${LIB_MYLIB}
)
Hope this will help
I want to use a tree parsing library (NHParser) in my code. I have make-installed the library in /home/ikram/parser (with include files in /home/ikram/parser/include/NHparser/ and lib files in /home/ikram/parser/lib/ directory).
I am wondering if, in the first place, I can just add the library and include file in my main CMakeLists.txt file without resorting to FindNHPARSER.cmake script? If yes, how I may achieve that?
I tried the above but couldn't succeed so I wrote FindNHPARSER.cmake like the following.
find_path(NHPARSER_INCLUDE_DIR include/NHparser/NHparser.h
HINTS PATH_SUFFIXES NHparser)
find_library(NHPARSER_LIBRARY NAMES NHparser)
set(NHparser_LIBRARIES ${NHparser_LIBRARY})
set(NHparser_INCLUDE_DIRS ${NHparser_INCLUDE_DIR})
include(FindPackageHandleStandardArgs)
mark_as_advanced(NHparser_INCLUDE_DIR NHparser_LIBRARY )
Is it correctly written? How I can include this in my main CMakeLists.txt script and do I need to set CMAKE_MODULE_PATH while running cmake?
Thanks for your time.
For such a small (fairly standard) find process, I'd just add the code directly to the main CMakeLists.txt.
You can move it to a separate .cmake file if you prefer - then you'd just include that file at the appropriate point in your main CMakeLists.txt.
As for the code, I'd keep it pretty simple; essentially just a find_path and find_library call along with checks that the relevant things were actually found:
find_path(NHPARSER_INCLUDE_DIR NAMES NHparser.h PATH_SUFFIXES NHparser)
if(NOT NHPARSER_INCLUDE_DIR)
message(FATAL_ERROR "Didn't find NHparser.h")
endif()
find_library(NHPARSER_LIBRARY NAMES NHparser)
if(NOT NHPARSER_LIBRARY)
message(FATAL_ERROR "Didn't find NHparser library")
endif()
The you can use the variables like:
include_directories(${NHPARSER_INCLUDE_DIR})
...
target_link_libraries(MyExe ${NHPARSER_LIBRARY})
The variables are unlikely to be found unaided though, since the install prefix for this library on your machine seems to be "/home/ikram/parser". Since this location is probably specific to your own machine, I wouldn't hard-code that into your CMakeLists.txt.
Instead, to have CMake add that path to the list of default search paths, just set CMAKE_PREFIX_PATH when you invoke CMake:
cmake . -DCMAKE_PREFIX_PATH=/home/ikram/parser
I'm writing my first configure.ac and need to search for a C++ library.
I tried the following line, but when run the configure script, it finds nothing.
AC_SEARCH_LIBS([xmlpp::Document::get_root_node], [xml++-2.6])
Probably something is wrong with it. So, how can I make autoconf look for C++ libraries? I don't want to supply a global method (and don't think libxml++ has one either).
You might want to try AX_CXX_CHECK_LIB from the Autoconf macro archive. you should probably make sure that you either use AC_LANG([C++]) or surround the call with AC_LANG_PUSH([C++]) and AC_LANG_POP([C++]).
This link is about finding a C-style signature function to the library so that it can be tested by autoconf or write your own test:
https://nerdland.net/2009/07/detecting-c-libraries-with-autotools/
Maybe worth to try, but I am getting an error with the AC_LANG_PROGRAM macro. The problem is put -llibname before the foo.cpp file. My compiler cares about the order of the -l and cpp file. The linker will not be able to find the function in the library.
Hope this helps. My suggestion would be to use CMake instead of Autoconf.
This CMakeLists.txt file should get you started:
cmake_minimum_required(VERSION 2.8)
# http://www.cmake.org/cmake/help/cmake-2-8-docs.html#module:FindLibXml2
find_package(libxml2 2.6 REQUIRED) # http://www.cmake.org/cmake/help/cmake-2-8-docs.html#command:find_package
include_directories(${LIBXML2_INCLUDE_DIR}) # http://www.cmake.org/cmake/help/cmake-2-8-docs.html#command:include_directories
add_executable(myApp main.cpp other.cpp) # http://www.cmake.org/cmake/help/cmake-2-8-docs.html#command:add_executable
target_link_libraries(myApp ${LIBXML2_LIBRARIES}) # http://www.cmake.org/cmake/help/cmake-2-8-docs.html#command:target_link_libraries
To use the file, after adjusting for your project of course. Put it in your Source dir as CMakeLists.txt, then:
mkdir build
cd build
cmake .. # This is like autoconf and generates the make files
make
If it sounds intriguing check out the giant youtube vid on all the benefits: http://www.youtube.com/watch?v=8Ut9o4OdSC0
It's good to use CMake, CTest, CDash, and CPack together in a project.