[CMake] Cmake

Dan Liew dan at su-root.co.uk
Wed Dec 21 08:03:19 EST 2016


Hi Aishwarya,

On 21 December 2016 at 11:51, aishwarya selvaraj
<aishwaryaselvaraj1708 at gmail.com> wrote:
> Hi Everyone,
> Myself Aishwarya .I wanted to make use of Cmake , but I'm really new to this
> and doesn't have a clear idea about it.
>
> 1) My Goal :
> I'm using Praat a speech Processing Software to create plug - in for my
> Algorithm on Pitch and Time scaling .(I have the .cpp file)
> In order to do that I need to use the binary file obtained by compiling .cpp
> file
> in my Praat Script .
> I intent to share this plug - in with others ,Hence I want it to be machine
> independent .The binary file I obtained is using gcc in Linux (16.0.2),
> which clearly wont work in Mac or Windows or even in lower Linux Version.
> To solve this Problem I understand that I need to make use of Cmake files .

CMake is not going make your binaries machine independent. It can give
a way to build your application that will successfully compile on
different platforms but you will have to compile the binary on each
platform that you are targeting.

> 2) My .cpp file called TSM_CODE_V3.cpp is compiled as
> g++ TSM_CODE_V3.cpp -larmadillo -lsndfile -o TSM
>
> where armadillo and sndfile are two depended libraries.
>
> 3) What I have done :
> I have gone through a loot of tutorials for cmake .I have wage idea , but
> I'm not able to solve my problem mentioned above .
> My folder TSM consists of
> build - the folder which consists the final executable binary file and the
> related cmake files required to create binary file
> src - consists of my TSM_CODE_V3.cpp code
> CMakeLists.txt - The top level Cmake file :
>
>          cmake_minimum_required(VERSION 2.8.9)
>          project(directory_TSM)
>          include_directories(include)
>          file(GLOB SOURCES "src/*.cpp")
>          add_executable(tsm ${SOURCES})

Do not use `file(GLOB ...)` to find source files. It is very bad
practice because it prevents CMake from re-generating itself
automatically. You should list the source files explicitly. That way
when you add/remove a source file the fact you updated the
`CMakeLists.txt` file will trigger a re-configure of the build system.

>
> I need to include the library armadillo and lidsndfile .
> I dont know how to include it into my cmakefile .
> I have come across find_package ,find_library  but I am sure about it .
>  To create my Praat Plug - in I need to compile the source code no matter in
> which system it is ,which may or may not have these two libraries already
> installed.
> So how how do I add libraries using cmake ?

Use `target_link_libraries()` to link libraries into your target. If
you're not sure how to use it I highly recommend you read the
documentation for it [1].

In terms of finding the library if there is no package for those
libraries you can use the `find_library()` command. Again if you're
not sure how to use it check out the documentation [2].

Seeing as you are very new to CMake I would also recommend reading [3]
which explains some important high level concepts in CMake.

[1] https://cmake.org/cmake/help/v3.6/command/target_link_libraries.html
[2] https://cmake.org/cmake/help/v3.6/command/find_library.html
[3] https://cmake.org/cmake/help/v3.6/manual/cmake-buildsystem.7.html


More information about the CMake mailing list