Write CMakeLists.txt for this C++ program

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Write CMakeLists.txt for this C++ program

dexter810
This is the program I want to run, main.cpp:

#include <iostream>
#include "yaracpp/yaracpp.h"

int main() {
    yaracpp::YaraDetector yara;
    yara.addRules(R"(

        rule example {
            strings:
                $s = "Hello"
            condition:
                $s
        })");


    if (yara.analyze("test_file")) {
        for (const auto& rule : yara.getDetectedRules()) {
            std::cout << rule << '\n';
        }
    }
}


When I run this command on the terminal it compiles successfully:

g++ -Iinclude -Ibuild/deps/yara/src/yara/libyara/include/ -Lbuild/src/
-Lbuild/deps/yara/src/yara/libyara/.libs/ main.cpp -lyaracpp -lyara
-lpthread -lssl -lcrypto

*Approach 1:*
My CMakeLists.txt is:

cmake_minimum_required(VERSION 3.6)
project(main CXX C)

add_executable(main main.cpp)

set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Iinclude
-Ibuild/deps/yara/src/yara/libyara/include -Lbuild/src
-Lbuild/deps/yara/src/yara/libyara/.libs/")


target_link_libraries (main yaracpp yara pthread ssl crypto)


But this was not able to link it properly and threw errors that it cannot
find "yaracpp/yaracpp.h". The references for writing CMakeLists.txt like
this were:

https://stackoverflow.com/questions/43136418/how-to-add-l-ell-compiler-flag-in-cmake

https://stackoverflow.com/questions/53879422/how-to-set-g-compile-options-in-clion?answertab=oldest#tab-top




--
Sent from: http://cmake.3232098.n2.nabble.com/
--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
https://cmake.org/mailman/listinfo/cmake
Reply | Threaded
Open this post in threaded view
|

Re: Write CMakeLists.txt for this C++ program

Eric Dönges
On 19.06.19 12:42, dexter810 wrote:

> cmake_minimum_required(VERSION 3.6)
> project(main CXX C)
>
> add_executable(main main.cpp)
>
> set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Iinclude
> -Ibuild/deps/yara/src/yara/libyara/include -Lbuild/src
> -Lbuild/deps/yara/src/yara/libyara/.libs/")
>
>
> target_link_libraries (main yaracpp yara pthread ssl crypto)
>
>
> But this was not able to link it properly and threw errors that it cannot
> find "yaracpp/yaracpp.h". The references for writing CMakeLists.txt like
> this were:
>
> https://stackoverflow.com/questions/43136418/how-to-add-l-ell-compiler-flag-in-cmake
>
> https://stackoverflow.com/questions/53879422/how-to-set-g-compile-options-in-clion?answertab=oldest#tab-top

Since you don't include the error messages from the compiler, we are
reduced to guessing here:

1) Don't add -I flags to compiler command line by modifying the
CMAKE_CXX_FLAGS directly. Instead, use either the include_directories
command (if you want to specify the include directories for multiple
targets), or the target_include_directories command:

        target_include_directories(main PRIVATE
                include build/deps/yara/src/yara/libyara/include)

2) CMAKE_CXX_FLAGS is used for compiling, not linking. One way to get
the linker command line you want is to change

        target_link_libraries (main yaracpp yara pthread ssl crypto)

to

        target_link_libraries (main
                -Lbuild/src build/deps/yara/src/yara/libyara/.libs
                -lyaracpp -lyara pthread ssl crypto)

However, better practice would be to create import targets for the
libraries you want to use. For example (assuming that yaracpp is a
shared library):

        add_library(yaracpp SHARED IMPORTED)
        set_target_properties(yaracpp PROPERTIES
                IMPORTED_LOCATION "build/deps/yara/src/yara/libyara/.libs/libyara.so"
                INTERFACE_INCLUDE_DIRECTORIES "build/deps/yara/src/yara/libyara/include")

Then, you simply add yaracpp to your target_link_libraries command and
can skip the target_include_directories command. Even better practice
would be to use the find_library command to find the libyara library -
this way, your CMakelists.txt would also work for non-Unix target
systems like Windows.

For more information, I highly recommend reading the CMake
documentation, particularly
https://cmake.org/cmake/help/v3.15/manual/cmake-buildsystem.7.html
--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
https://cmake.org/mailman/listinfo/cmake
Reply | Threaded
Open this post in threaded view
|

Re: Write CMakeLists.txt for this C++ program

dexter810
*Approach 1:*
CMakeLists.txt:

cmake_minimum_required(VERSION 3.6)
project(main CXX C)

add_executable(main main.cpp)

target_include_directories(main PRIVATE
                include build/deps/yara/src/yara/libyara/include)


 target_link_libraries (main
                -Lbuild/src/ -Lbuild/deps/yara/src/yara/libyara/.libs
                yaracpp yara pthread ssl crypto)


I got:
*cmake -DCMAKE_BUILD_TYPE=Debug -DCMAKE_INSTALL_PREFIX="$(pwd)/install" ..*
-- The CXX compiler identification is GNU 7.4.0
-- The C compiler identification is GNU 7.4.0
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/mevasu/yaracpp/build2

*
cmake --build . -- -j $NPROC*
Scanning dependencies of target main
[ 50%] Building CXX object CMakeFiles/main.dir/main.cpp.o
[100%] Linking CXX executable main
/usr/bin/ld: cannot find -lyaracpp
/usr/bin/ld: cannot find -lyara
collect2: error: ld returned 1 exit status
CMakeFiles/main.dir/build.make:94: recipe for target 'main' failed
make[2]: *** [main] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/main.dir/all' failed
make[1]: *** [CMakeFiles/main.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2


*
Approach 2:*

CMakeLists.txt:
cmake_minimum_required(VERSION 3.6)
project(main CXX C)

add_executable(main main.cpp)



add_library(yara STATIC IMPORTED)
set_target_properties(yara PROPERTIES
                IMPORTED_LOCATION
"build/deps/yara/src/yara/libyara/.libs/libyara.a"
                INTERFACE_INCLUDE_DIRECTORIES
"build/deps/yara/src/yara/libyara/include")

add_library(yaracpp STATIC IMPORTED)
set_target_properties(yaracpp PROPERTIES
                IMPORTED_LOCATION "build/src/libyaracpp.a"
                INTERFACE_INCLUDE_DIRECTORIES "include")

I got:

*cmake -DCMAKE_BUILD_TYPE=Debug -DCMAKE_INSTALL_PREFIX="$(pwd)/install" ..*
-- The CXX compiler identification is GNU 7.4.0
-- The C compiler identification is GNU 7.4.0
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/mevasu/Desktop/yaracpp/build2

*
 cmake --build . -- -j $NPROC*
Scanning dependencies of target main
[ 50%] Building CXX object CMakeFiles/main.dir/main.cpp.o
/home/mevasu/Desktop/yaracpp/main.cpp:2:10: fatal error: yaracpp/yaracpp.h:
No such file or directory
 #include "yaracpp/yaracpp.h"
          ^~~~~~~~~~~~~~~~~~~
compilation terminated.
CMakeFiles/main.dir/build.make:62: recipe for target
'CMakeFiles/main.dir/main.cpp.o' failed
make[2]: *** [CMakeFiles/main.dir/main.cpp.o] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/main.dir/all' failed
make[1]: *** [CMakeFiles/main.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2


The libraries that I'm trying to link are actually static libraries and from
the link that you gave in the above answer, I encountered:
https://cmake.org/cmake/help/latest/command/target_link_libraries.html
where I tried giving this approach:
*
Approach 3*
CMakeLists.txt:
cmake_minimum_required(VERSION 3.6)
project(main CXX C)

add_executable(main main.cpp)

target_include_directories(main PRIVATE
                include build/deps/yara/src/yara/libyara/include)


 target_link_libraries (main
                -Lbuild/src/libyaracpp.a
-Lbuild/deps/yara/src/yara/libyara/.libs/libyara.a
                yaracpp yara pthread ssl crypto)



I got:
*
cmake -DCMAKE_BUILD_TYPE=Debug -DCMAKE_INSTALL_PREFIX="$(pwd)/install" ..*
-- The CXX compiler identification is GNU 7.4.0
-- The C compiler identification is GNU 7.4.0
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/mevasu/yaracpp/build2

*cmake --build . -- -j $NPROC*
Scanning dependencies of target main
[ 50%] Building CXX object CMakeFiles/main.dir/main.cpp.o
[100%] Linking CXX executable main
/usr/bin/ld: cannot find -lyaracpp
/usr/bin/ld: cannot find -lyara
collect2: error: ld returned 1 exit status
CMakeFiles/main.dir/build.make:94: recipe for target 'main' failed
make[2]: *** [main] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/main.dir/all' failed
make[1]: *** [CMakeFiles/main.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2


Also, inside target_link_libraries() irrespective of putting -lyaracpp and
-lyara or yaracpp and yara, I got the same errors.



--
Sent from: http://cmake.3232098.n2.nabble.com/
--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
https://cmake.org/mailman/listinfo/cmake
Reply | Threaded
Open this post in threaded view
|

Re: Write CMakeLists.txt for this C++ program

Eric Dönges
On 19.06.19 15:36, dexter810 wrote:

> *Approach 1:*
> CMakeLists.txt:
>
> cmake_minimum_required(VERSION 3.6)
> project(main CXX C)
>
> add_executable(main main.cpp)
>
> target_include_directories(main PRIVATE
>                 include build/deps/yara/src/yara/libyara/include)
>
>
>  target_link_libraries (main
>                 -Lbuild/src/ -Lbuild/deps/yara/src/yara/libyara/.libs
>                 yaracpp yara pthread ssl crypto)

I've done a little testing - the reason this approach doesn't work is
because when CMake calls the linker, the current directory is not the
source directory, but the build directory. For this approach to work you
would need to write:

target_include_directories(main PRIVATE
        ${CMAKE_CURRENT_SOURCE_DIR}/include
        ${CMAKE_CURRENT_SOURCE_DIR}/build/deps/yara/src/yara/libyara/include)

target_link_libraries (main
        -L${CMAKE_CURRENT_SOURCE_DIR}/build/src/
        -L${CMAKE_CURRENT_SOURCE_DIR}/build/deps/yara/src/yara/libyara/.libs
    yaracpp yara pthread ssl crypto)

> *
> Approach 2:*
>
> CMakeLists.txt:
> cmake_minimum_required(VERSION 3.6)
> project(main CXX C)
>
> add_executable(main main.cpp)
>
>
>
> add_library(yara STATIC IMPORTED)
> set_target_properties(yara PROPERTIES
>                 IMPORTED_LOCATION
> "build/deps/yara/src/yara/libyara/.libs/libyara.a"
>                 INTERFACE_INCLUDE_DIRECTORIES
> "build/deps/yara/src/yara/libyara/include")
>
> add_library(yaracpp STATIC IMPORTED)
> set_target_properties(yaracpp PROPERTIES
>                 IMPORTED_LOCATION "build/src/libyaracpp.a"
>                 INTERFACE_INCLUDE_DIRECTORIES "include")

This cannot work because you forgot the

target_link_libraries(main yara yaracpp pthread ssl crypto)

CMake is not psychic, you need to explicitly tell it which libraries
main will be needing.

> Approach 3*
> CMakeLists.txt:
> cmake_minimum_required(VERSION 3.6)
> project(main CXX C)
>
> add_executable(main main.cpp)
>
> target_include_directories(main PRIVATE
>                 include build/deps/yara/src/yara/libyara/include)
>
>
>  target_link_libraries (main
>                 -Lbuild/src/libyaracpp.a
> -Lbuild/deps/yara/src/yara/libyara/.libs/libyara.a
>                 yaracpp yara pthread ssl crypto)

This approach cannot work because the -L option tells the compiler in
which (additional) directory to look for libraries. If you want to use
the full path to the library, you would use:

target_link_libraries (main
        build/src/libyaracpp.a
        build/deps/yara/src/yara/libyara/.libs/libyara.a
        yaracpp yara pthread ssl crypto)

> Also, inside target_link_libraries() irrespective of putting -lyaracpp and
> -lyara or yaracpp and yara, I got the same errors.

I believe CMake will automatically add the -l if you specify an argument
to target_link_libraries that is not a CMake target itself and not an
absolute path when building for Unix-like systems.
--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
https://cmake.org/mailman/listinfo/cmake
Reply | Threaded
Open this post in threaded view
|

Re: Write CMakeLists.txt for this C++ program

Eric Dönges
To correct myself:

On 20.06.19 00:48, Eric Dönges wrote:
>
> target_link_libraries (main
> build/src/libyaracpp.a
> build/deps/yara/src/yara/libyara/.libs/libyara.a
> yaracpp yara pthread ssl crypto)
>

This of course should be

target_link_libraries (main
  build/src/libyaracpp.a
  build/deps/yara/src/yara/libyara/.libs/libyara.a
  pthread ssl crypto)
--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
https://cmake.org/mailman/listinfo/cmake
Reply | Threaded
Open this post in threaded view
|

Re: Write CMakeLists.txt for this C++ program

dexter810
In reply to this post by dexter810
I built this C++ executable using CMake 3.10.2 in Ubuntu 18.04 and it was
built
successfully using the first approach. I'm now trying to build it using the
same CMakeLists.txt and
folder structure in Android Studio which is also using CMake 3.10.2, this
time a library not an executable, that being the only change.

On running inspect code inside Android Studio, it gave me errors related to
the headers, it couldn't find them:
<http://cmake.3232098.n2.nabble.com/file/t398066/headers_problem.headers_problem>

From my understanding when I built on Ubuntu, my build folder got populated
with certain folders and files which contained the header files, but after
building in Android Studio and looking inside the default build folder I
couldn't find anything remotely similar.
Is the way CMake works in Android Studio different than how CMake works in
say, Ubuntu?



--
Sent from: http://cmake.3232098.n2.nabble.com/
--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
https://cmake.org/mailman/listinfo/cmake