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

Eric Dönges doenges at mvtec.com
Wed Jun 19 18:48:56 EDT 2019


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.


More information about the CMake mailing list