[CMake] Write CMakeLists.txt for this C++ program

dexter810 meghna.vasudeva at gmail.com
Wed Jun 19 09:36:30 EDT 2019


*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/


More information about the CMake mailing list