list(REMOVE_ITEM) not working in cmake - cmake

Following is a part of my CMakeLists.txt file.
file(GLOB SOURCES "xyz/*.cpp")
message("${SOURCES}")
list(REMOVE_ITEM SOURCES "src1.cpp")
message("${SOURCES}")
Here in file "xyz/*.cpp" is a relative path.
Content of ${SOURCES} is the same before and after REMOVE_ITEM.
Why is list(REMOVE_ITEM) not working in my case? Any help would be invaluable.

I got a solution for your problem. The idea behind my solution is, to get the full path of the special cpp file, because I saw, that the command file(GLOB SOURCES "src/*.cpp") gives me a list of full paths. After getting the full path of the special file, I could do a remove from the list. Here is a small example
cmake_minimum_required(VERSION 3.4)
project(list_remove_item_ex)
file(GLOB SOURCES "src/*.cpp")
# this is the file I want to exclude / remove from the list
get_filename_component(full_path_test_cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/test.cpp ABSOLUTE)
message("${full_path_test_cpp}")
list(REMOVE_ITEM SOURCES "${full_path_test_cpp}")
message("${SOURCES}")

In addition to accepted answer you can use the following function to filter lists using regexp:
# Filter values through regex
# filter_regex({INCLUDE | EXCLUDE} <regex> <listname> [items...])
# Element will included into result list if
# INCLUDE is specified and it matches with regex or
# EXCLUDE is specified and it doesn't match with regex.
# Example:
# filter_regex(INCLUDE "(a|c)" LISTOUT a b c d) => a c
# filter_regex(EXCLUDE "(a|c)" LISTOUT a b c d) => b d
function(filter_regex _action _regex _listname)
# check an action
if("${_action}" STREQUAL "INCLUDE")
set(has_include TRUE)
elseif("${_action}" STREQUAL "EXCLUDE")
set(has_include FALSE)
else()
message(FATAL_ERROR "Incorrect value for ACTION: ${_action}")
endif()
set(${_listname})
foreach(element ${ARGN})
string(REGEX MATCH ${_regex} result ${element})
if(result)
if(has_include)
list(APPEND ${_listname} ${element})
endif()
else()
if(NOT has_include)
list(APPEND ${_listname} ${element})
endif()
endif()
endforeach()
# put result in parent scope variable
set(${_listname} ${${_listname}} PARENT_SCOPE)
endfunction()
For example in question it could look as the following:
file(GLOB SOURCES "xyz/*.cpp")
message("${SOURCES}")
filter_regex(EXCLUDE "src1\\.cpp" SOURCES ${SOURCES})
message("${SOURCES}")
(Because regexp is used then . is replaced with \\.)

Related

How can I get a target's output-file's name during CMake's configuration phase (not the generation phase)?

I know we can use $<TARGET_FILE_NAME:Foo> to get the filename for add_custom_command and add_custom_target during build time, but I can't seem to find the answer on out how to get the filename during config time. For example,
add_library(Foo SHARED foo.cpp foo.h)
The best I get is get_target_property(FOO_NAME Foo NAME), but ${FOO_NAME} is Foo, what I want is something like libFoo.so or libFoo.dylib depends on the platform. How can we get the target file name during cmake config time?
For context on why I thought I initially thought I needed to be able to do this, see this other question: In CMake how can I copy a target file to a custom location when target based generator expression for OUTPUT is not supported?.
Through a combination of the following:
variable/CMAKE_BUILD_TYPE
prop_tgt/TYPE
prop_tgt/FRAMEWORK
variable/CMAKE_STATIC_LIBRARY_PREFIX and variable/CMAKE_STATIC_LIBRARY_SUFFIX
variable/CMAKE_SHARED_LIBRARY_PREFIX and variable/CMAKE_SHARED_LIBRARY_SUFFIX
variable/CMAKE_SHARED_MODULE_PREFIX and variable/CMAKE_SHARED_MODULE_SUFFIX
variable/CMAKE_EXECUTABLE_SUFFIX
prop_tgt/OUTPUT_NAME and prop_tgt/OUTPUT_NAME_<CONFIG>
prop_tgt/ARCHIVE_OUTPUT_NAME and prop_tgt/ARCHIVE_OUTPUT_NAME_<CONFIG>
prop_tgt/LIBRARY_OUTPUT_NAME and prop_tgt/LIBRARY_OUTPUT_NAME_<CONFIG>
prop_tgt/RUNTIME_OUTPUT_NAME and prop_tgt/RUNTIME_OUTPUT_NAME_<CONFIG>
I believe you could do something like this:
# cmake_minimum_required(VERSION 3.x)
project(hello)
function(get_target_filename target outvar)
get_target_property(prop_type "${target}" TYPE)
get_target_property(prop_is_framework "${target}" FRAMEWORK)
get_target_property(prop_outname "${target}" OUTPUT_NAME)
get_target_property(prop_archive_outname "${target}" ARCHIVE_OUTPUT_NAME)
get_target_property(prop_library_outname "${target}" LIBRARY_OUTPUT_NAME)
get_target_property(prop_runtime_outname "${target}" RUNTIME_OUTPUT_NAME)
# message("prop_archive_outname: ${prop_archive_outname}")
# message("prop_library_outname: ${prop_library_outname}")
# message("prop_runtime_outname: ${prop_runtime_outname}")
if(DEFINED CMAKE_BUILD_TYPE)
get_target_property(prop_cfg_outname "${target}" "${OUTPUT_NAME}_${CMAKE_BUILD_TYPE}")
get_target_property(prop_archive_cfg_outname "${target}" "${ARCHIVE_OUTPUT_NAME}_${CMAKE_BUILD_TYPE}")
get_target_property(prop_library_cfg_outname "${target}" "${LIBRARY_OUTPUT_NAME}_${CMAKE_BUILD_TYPE}")
get_target_property(prop_runtime_cfg_outname "${target}" "${RUNTIME_OUTPUT_NAME}_${CMAKE_BUILD_TYPE}")
# message("prop_archive_cfg_outname: ${prop_archive_cfg_outname}")
# message("prop_library_cfg_outname: ${prop_library_cfg_outname}")
# message("prop_runtime_cfg_outname: ${prop_runtime_cfg_outname}")
if(NOT ("${prop_cfg_outname}" STREQUAL "prop_cfg_outname-NOTFOUND"))
set(prop_outname "${prop_cfg_outname}")
endif()
if(NOT ("${prop_archive_cfg_outname}" STREQUAL "prop_archive_cfg_outname-NOTFOUND"))
set(prop_archive_outname "${prop_archive_cfg_outname}")
endif()
if(NOT ("${prop_library_cfg_outname}" STREQUAL "prop_library_cfg_outname-NOTFOUND"))
set(prop_library_outname "${prop_library_cfg_outname}")
endif()
if(NOT ("${prop_runtime_cfg_outname}" STREQUAL "prop_runtime_cfg_outname-NOTFOUND"))
set(prop_runtime_outname "${prop_runtime_cfg_outname}")
endif()
endif()
set(outname "${target}")
if(NOT ("${prop_outname}" STREQUAL "prop_outname-NOTFOUND"))
set(outname "${prop_outname}")
endif()
if("${prop_is_framework}")
set(filename "${outname}")
elseif(prop_type STREQUAL "STATIC_LIBRARY")
if(NOT ("${prop_archive_outname}" STREQUAL "prop_archive_outname-NOTFOUND"))
set(outname "${prop_archive_outname}")
endif()
set(filename "${CMAKE_STATIC_LIBRARY_PREFIX}${outname}${CMAKE_STATIC_LIBRARY_SUFFIX}")
elseif(prop_type STREQUAL "MODULE_LIBRARY")
if(NOT ("${prop_library_outname}" STREQUAL "prop_library_outname-NOTFOUND"))
set(outname "${prop_library_outname}")
endif()
set(filename "${CMAKE_SHARED_MODULE_LIBRARY_PREFIX}${outname}${CMAKE_SHARED_MODULE_LIBRARY_SUFFIX}")
elseif(prop_type STREQUAL "SHARED_LIBRARY")
if(WIN32)
if(NOT ("${prop_runtime_outname}" STREQUAL "prop_runtime_outname-NOTFOUND"))
set(outname "${prop_runtime_outname}")
endif()
else()
if(NOT ("${prop_library_outname}" STREQUAL "prop_library_outname-NOTFOUND"))
set(outname "${prop_library_outname}")
endif()
endif()
set(filename "${CMAKE_SHARED_LIBRARY_PREFIX}${outname}${CMAKE_SHARED_LIBRARY_SUFFIX}")
elseif(prop_type STREQUAL "EXECUTABLE")
if(NOT ("${prop_runtime_outname}" STREQUAL "prop_runtime_outname-NOTFOUND"))
set(outname "${prop_runtime_outname}")
endif()
set(filename "${CMAKE_EXECUTABLE_PREFIX}${outname}${CMAKE_EXECUTABLE_SUFFIX}")
else()
message(FATAL_ERROR "target \"${target}\" is not of type STATIC_LIBRARY, MODULE_LIBRARY, SHARED_LIBRARY, or EXECUTABLE.")
endif()
set("${outvar}" "${filename}" PARENT_SCOPE)
endfunction()
add_library(static_lib STATIC test.cpp)
add_library(shared_lib SHARED test.cpp)
add_executable(executable test.cpp)
get_target_filename(static_lib static_lib_filename)
get_target_filename(shared_lib shared_lib_filename)
get_target_filename(executable executable_filename)
message(STATUS "static_lib_filename: ${static_lib_filename}")
message(STATUS "shared_lib_filename: ${shared_lib_filename}")
message(STATUS "executable_filename: ${executable_filename}")
The above is a basic implementation. It doesn't handle some (perhaps important) nuances like:
The fact that most of those target properties can themselves have generator expressions in them (see their docs), which, if it happens to you, I think you're out of luck.
The fact that CMAKE_BUILD_TYPE is only relevant for single-config generators- not multi-config generators.
https://cmake.org/cmake/help/latest/variable/CMAKE_EXECUTABLE_SUFFIX_LANG.html
Other language-specific overrides like CMAKE_SHARED_LIBRARY_PREFIX_<LANG>
You'd need to check if those exist and handle them if they do... except in honesty I'm not quite sure how, given that it doesn't seem like targets have a LANGUAGE property. Source files do, but that's not what we need here. One might need to go to the CMake Discourse to ask about this.
Note: If you want the full path to the target output file... oh boy...
prop_tgt/ARCHIVE_OUTPUT_DIRECTORY and prop_tgt/ARCHIVE_OUTPUT_DIRECTORY_<CONFIG>
prop_tgt/LIBRARY_OUTPUT_DIRECTORY and prop_tgt/LIBRARY_OUTPUT_DIRECTORY_<CONFIG>
prop_tgt/RUNTIME_OUTPUT_DIRECTORY and prop_tgt/RUNTIME_OUTPUT_DIRECTORY_<CONFIG>
More fun notes: If you want to evaluate generator expressions recursively at generation time (for generator expressions that themselves evaluate to generator expressions), you can use the $<GENEX_EVAL:...> generator expression, but of course- that doesn't apply to this question, which is about configure time.
A little bit clumsy but the file name can be constructed in the following way:
set(FOO_NAME "${CMAKE_SHARED_LIBRARY_PREFIX}Foo${CMAKE_SHARED_LIBRARY_SUFFIX}")
See also cmake doc.
Note that the two variables will be overridden by the respective CMAKE_SHARED_LIBRARY_??FIX_<LANG> variables. So if that's a possibility, you need make sure you catch the right variable.
Let me add the final remark that the rationale behind CMake is you don't need to know. CMake operates on targets, not files. So whatever you're trying to achieve might be possible without getting the filename.

cmake cross-compile for raspberry pi

I am cross-compiling a library (ORBSLAM3) on pc using /usr and /lib
from rpi4 (After using rsync). And I put them in a folder named "rootf".The prerequisite libraries have been installed before so they are now in ~/rootf/usr and ~/rootf/lib. I need to compile ORBSLAM3 using ~/rootf/ as the root.
The thing is I am not familiar with cmake so I have been struggling for a while.
The process of compiling is this:
using Toolchain-rpi.cmake for the CMakeLists.txt on the top level of ORBSLAM3.
In CMakeLists.txt, it finds pangolin by find_package(Pangolin REQUIRED)
find_package() is actually doing the right.
It successfully finds
/home/ethan/raspberrypi/rootfs/usr/local/lib/cmake/Pangolin/PangolinTargets.cmake
The CMAKE_PREFIX_PATH in this CMakeLists.txt is right, which is
/home/ethan/raspberrypi/rootfs/usr/local/lib;/home/ethan/raspberrypi/rootfs/usr/lib/arm-linux-gnueabihf
After I uninstall pangolin on ubuntu, I get this:
CMake Error at /home/ethan/raspberrypi/rootfs/usr/local/lib/cmake/Pangolin/PangolinTargets.cmake:80 (message):
The imported target "pangolin" references the file
"/usr/local/lib/libpangolin.so"
but this file does not exist. Possible reasons include:
* The file was deleted, renamed, or moved to another location.
Since Pangolin was originally installed on board, so on board libpangolin.so is at /usr/local/lib/libpangolin.so.
I guess when I use PangolinTargets.cmake, it is doing the same thing.
I output CMAKE_SYSROOT,CMAKE_PREFIX_PATH,_IMPORT_PREFIX in PangolinTargets.cmake. It's what I expect.
CMAKE_SYSROOT = /home/ethan/raspberrypi/rootfs
CMAKE_PREFIX_PATH = ;/home/ethan/raspberrypi/rootfs/usr/local/lib;/home/ethan/raspberrypi/rootfs/usr/lib/arm-linux-gnueabihf
_IMPORT_PREFIX = /home/ethan/raspberrypi/rootfs/usr/local
Even if I add /home/ethan/raspberrypi/rootfs as prefix to the below paths, nothing changed.
set_target_properties(pangolin PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES "/usr/include;/usr/include;/usr/include;/usr/local/include/eigen3;${_IMPORT_PREFIX}/include"
INTERFACE_LINK_LIBRARIES "rt;pthread;/usr/lib/arm-linux-gnueabihf/libGL.so;/usr/lib/arm-linux-gnueabihf/libGLU.so;
INTERFACE_SYSTEM_INCLUDE_DIRECTORIES "/usr/include;/usr/include;/usr/include;/usr/local/include/eigen3"
)
Here is the content of PangolinTargets.cmake. I am not sure which parts leads to searching only /usr. Thanks all.
# Generated by CMake
if("${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION}" LESS 2.5)
message(FATAL_ERROR "CMake >= 2.6.0 required")
endif()
cmake_policy(PUSH)
cmake_policy(VERSION 2.6)
#----------------------------------------------------------------
# Generated CMake target import file.
#----------------------------------------------------------------
# Commands may need to know the format version.
set(CMAKE_IMPORT_FILE_VERSION 1)
# Protect against multiple inclusion, which would fail when already imported targets are added once more.
set(_targetsDefined)
set(_targetsNotDefined)
set(_expectedTargets)
foreach(_expectedTarget pangolin)
list(APPEND _expectedTargets ${_expectedTarget})
if(NOT TARGET ${_expectedTarget})
list(APPEND _targetsNotDefined ${_expectedTarget})
endif()
if(TARGET ${_expectedTarget})
list(APPEND _targetsDefined ${_expectedTarget})
endif()
endforeach()
if("${_targetsDefined}" STREQUAL "${_expectedTargets}")
unset(_targetsDefined)
unset(_targetsNotDefined)
unset(_expectedTargets)
set(CMAKE_IMPORT_FILE_VERSION)
cmake_policy(POP)
return()
endif()
if(NOT "${_targetsDefined}" STREQUAL "")
message(FATAL_ERROR "Some (but not all) targets in this export set were already defined.\nTargets Defined: ${_targetsDefined}\nTargets not yet defined: ${_targetsNotDefined}\n")
endif()
unset(_targetsDefined)
unset(_targetsNotDefined)
unset(_expectedTargets)
# Compute the installation prefix relative to this file.
get_filename_component(_IMPORT_PREFIX "${CMAKE_CURRENT_LIST_FILE}" PATH)
get_filename_component(_IMPORT_PREFIX "${_IMPORT_PREFIX}" PATH)
get_filename_component(_IMPORT_PREFIX "${_IMPORT_PREFIX}" PATH)
get_filename_component(_IMPORT_PREFIX "${_IMPORT_PREFIX}" PATH)
if(_IMPORT_PREFIX STREQUAL "/")
set(_IMPORT_PREFIX "")
endif()
# Create imported target pangolin
add_library(pangolin SHARED IMPORTED)
set_target_properties(pangolin PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES "/usr/include;/usr/include;/usr/include;/usr/local/include/eigen3;${_IMPORT_PREFIX}/include"
INTERFACE_LINK_LIBRARIES "rt;pthread;/usr/lib/arm-linux-gnueabihf/libGL.so;/usr/lib/arm-linux-gnueabihf/libGLU.so;/usr/lib/arm-linux-gnueabihf/libGLEW.so;/usr/lib/arm-linux-gnueabihf/libEGL.so;/usr/lib/arm-linux-gnueabihf/libSM.so;/usr/lib/arm-linux-gnueabihf/libICE.so;/usr/lib/arm-linux-gnueabihf/libX11.so;/usr/lib/arm-linux-gnueabihf/libXext.so;rt;pthread;/usr/lib/arm-linux-gnueabihf/libdc1394.so;/usr/lib/arm-linux-gnueabihf/libpng.so;/usr/lib/arm-linux-gnueabihf/libz.so;/usr/lib/arm-linux-gnueabihf/libjpeg.so;/usr/lib/arm-linux-gnueabihf/libtiff.so;/usr/lib/arm-linux-gnueabihf/libIlmImf.so"
INTERFACE_SYSTEM_INCLUDE_DIRECTORIES "/usr/include;/usr/include;/usr/include;/usr/local/include/eigen3"
)
if(CMAKE_VERSION VERSION_LESS 2.8.12)
message(FATAL_ERROR "This file relies on consumers using CMake 2.8.12 or greater.")
endif()
# Load information for each installed configuration.
get_filename_component(_DIR "${CMAKE_CURRENT_LIST_FILE}" PATH)
file(GLOB CONFIG_FILES "${_DIR}/PangolinTargets-*.cmake")
foreach(f ${CONFIG_FILES})
include(${f})
endforeach()
# Cleanup temporary variables.
set(_IMPORT_PREFIX)
# Loop over all imported files and verify that they actually exist
foreach(target ${_IMPORT_CHECK_TARGETS} )
foreach(file ${_IMPORT_CHECK_FILES_FOR_${target}} )
if(NOT EXISTS "${file}" )
message(FATAL_ERROR "The imported target \"${target}\" references the file
\"${file}\"
but this file does not exist. Possible reasons include:
* The file was deleted, renamed, or moved to another location.
* An install or uninstall procedure did not complete successfully.
* The installation package was faulty and contained
\"${CMAKE_CURRENT_LIST_FILE}\"
but not all the files it references.
")
endif()
endforeach()
unset(_IMPORT_CHECK_FILES_FOR_${target})
endforeach()
unset(_IMPORT_CHECK_TARGETS)
# This file does not depend on other imported targets which have
# been exported from the same project but in a separate export set.
# Commands beyond this point should not need to know the version.
set(CMAKE_IMPORT_FILE_VERSION)
cmake_policy(POP)
how to show the path of the file for the one that we search using find_package( )?
CMAKE_PREFIX_PATH
Semicolon-separated list of directories specifying installation prefixes to be searched by the find_package()...
set(CMAKE_PREFIX_PATH /home/users/rootf/usr/local/lib)
# additionalllly I also do a precaution
set(CMAKE_IGNORE_PATH / /bin /include /usr/lib /usr/local/lib)
You might also to manipulate with CMAKE_FIND_USE_*_PATH variables.

Why CMake C/C++ treats two binary library differently? (detail inside)

Here's my CMake script:
cmake_minimum_required(VERSION 2.8)
project(cimutils)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_VERBOSE_MAKEFILE TRUE)
# not recommended to search for all *.c (This sets all source in variable SOURCES)
#file(GLOB_RECURSE SOURCES RELATIVE ${CMAKE_SOURCE_DIR} "*.c")
# explicitly define each .c file is preferred. (This sets all source in variable SOURCES)
set(SOURCES
"src/process_picture/saveraw.c"
"src/process_picture/savebmp.c"
"src/process_picture/savejpeg.c"
"src/lcd/framebuffer.c"
"src/main.c"
"src/cim/video.c"
"src/cim/process.c"
"src/cim/convert.c"
"src/cim/cim_fmt.c"
)
# set include path in variable INC
set (INC
"src/include"
"src/jpeg_hw_encoder"
)
# Set include path
include_directories (AFTER ${INC})
# Set all source files
add_executable(${PROJECT_NAME} ${SOURCES})
# Sets default (system) library path
set (CMAKE_SYSROOT "/usr/local/mips-linux-gnu-gcc-toolchains/mips-gcc-glibc/mips-linux-gnu/libc")
find_library (jpeg
NAMES jpeg
PATHS "${CMAKE_SOURCE_DIR}/lib"
)
find_library (jpeg-hw
NAMES jpeg-hw
PATHS "${CMAKE_SOURCE_DIR}/lib"
)
target_link_libraries(${PROJECT_NAME} c gcc_s ${jpeg} ${jpeg-hw})
The build is OK. However, it treated jpeg-hw and jpeg differently, since I saw the link command (detail truncated, to show only the relevant parts) to be:
gcc ..... ${PROJECT_HOME}/lib/libjpeg.so -ljpeg-hw
Apparently, these two binary libraries were placed in the same directory, and they uses the same syntax in the script file. But why CMake choose full path for one, and choose to use -l for the other? Just very curious....

CPack / CMake: Different installation prefixes per CPACK_GENERATOR

How can I specify different installation prefixes for the different CPACK_GENERATORs?
For example:
the DEB package should be installed to /opt/project
the TGZ archive should consist only of the project directory
From the documentation I understood that I would have to use the CPACK_PROJECT_CONFIG_FILE variable. Using that, it should be possible to achieve the desired goal. However, it did not work for me.
This is my CPack configuration:
set(CPACK_GENERATOR "DEB;TGZ")
set(CPACK_PROJECT_CONFIG_FILE ${CMAKE_SOURCE_DIR}/cmake/package.linux.txt)
And this is my ${CMAKE_SOURCE_DIR}/cmake/package.linux.txt file:
IF (CPACK_GENERATOR MATCHES "DEB")
set(CPACK_NATIVE_INSTALL_DIRECTORY "/opt")
set(CMAKE_INSTALL_PREFIX "/opt")
set(CPACK_PACKAGING_INSTALL_PREFIX "/opt")
set(CPACK_INSTALL_DIRECTORY "/opt")
ELSEIF(CPACK_GENERATOR MATCHES "TGZ")
set(CPACK_NATIVE_INSTALL_DIRECTORY "")
set(CMAKE_INSTALL_PREFIX "")
set(CPACK_PACKAGING_INSTALL_PREFIX "")
set(CPACK_INSTALL_DIRECTORY "")
ENDIF()
I have made sure that all files get parsed using MESSAGE() directives, but the prefix of my packages is always /usr/local.
Finally, I found out what was wrong. I had the CPACK_SET_DESTDIR flag set. This makes CPACK append usr/local to the directory containing the binaries of the compilation. After removing the corresponding SET() directive, everything worked like a charm.
For the record I will provide you with my minimal working example.
CMakeLists.txt:
cmake_minimum_required(VERSION 3.2)
project(stripped-down C)
add_executable(main main.c)
install(TARGETS main
RUNTIME DESTINATION bin
)
set(CPACK_GENERATOR "DEB;TGZ")
set(CPACK_PROJECT_CONFIG_FILE ${CMAKE_SOURCE_DIR}/package.txt)
set(CPACK_PACKAGE_CONTACT "Some One <some.one#somewhere.com>")
include(CPack)
${CMAKE_SOURCE_DIR}/package.txt:
IF (CPACK_GENERATOR MATCHES "DEB")
set(CPACK_PACKAGING_INSTALL_PREFIX "/opt")
ELSEIF(CPACK_GENERATOR MATCHES "TGZ")
set(CPACK_PACKAGING_INSTALL_PREFIX "")
ENDIF()

Simple CMakeLists.txt that reflects typical directory structure (/src, /inc, /bin subdirectories)

I am struggling to make a CMakeList.txt file to reflect a simple, typical makefile. The original is here http://pastebin.com/S9Czr1pt .
I tried many things (like SET(SOURCE ... and SET(HEADERS... ) to add /src, /lib and /third-party//include ), but I have no luck.
Can anyone either help me out or point to a tutorial that does this thing?
This is just an out of the blue skeleton - please see the CMake documentation for details of each function:
cmake_minimum_required(VERSION 2.6)
# Project name, can be used as target name too
project(Example)
# Search all .cpp files within src - recursive!
# You can add all source files by hand here too
file(GLOB_RECURSE SRCS "src/*.cpp")
# Add include path (you can also add it's sub directories
include_directories("include")
# Search for packages -- PLEASE NOTE: many libraries provide alternative's to this
# which often provide more functionality
find_package(PkgConfig REQUIRED)
# TODO: add proper pkg modul search info's and change the variable's name
pkg_search_module(PACKAGE_NO1 ...)
# Show some messages (optional)
if( (PACKAGE_NO1 )
include_directories(${(PACKAGE_NO1_INCLUDE_DIRS})
message(STATUS "Using OpenSSL ${(PACKAGE_NO1_VERSION}")
else()
# do error handling
endif()
# Add compiler flags
add_definitions(-std=c++11 -Wall) # -g O3 etc are added according to Release / Debug compilation
# Build a executable target (1st param: Target name; 2nd: source files)
add_executable(${PROJECT_NAME} ${SRCS})