[CMake] Is this the rigth way to build a shared library?

Kyle Edwards kyle.edwards at kitware.com
Fri Jun 28 09:31:31 EDT 2019


On Thu, 2019-06-27 at 22:24 -0700, dexter810 wrote:
> This is my CMakeLists.txt which is successfully building a shared
> library, I
> just wanted to confirm if it's the right way and it: 
> 
> cmake_minimum_required(VERSION 3.6)
> project(main)
> 
> 
> 
> include_directories(
>         ${CMAKE_CURRENT_SOURCE_DIR}/include
>        
> ${CMAKE_CURRENT_SOURCE_DIR}/build/deps/yara/src/yara/libyara/include)
> 
> add_library(main SHARED main.cpp)
> 
> 
> link_libraries (
>         -L${CMAKE_CURRENT_SOURCE_DIR}/build/src/
>         -
> L${CMAKE_CURRENT_SOURCE_DIR}/build/deps/yara/src/yara/libyara/.libs
>     yaracpp yara pthread ssl crypto)

Rather than specifying -L flags and specifying library names, there are
two other approaches you can take that would be better:

1) Use target_link_directories() in conjunction with
target_link_libraries().
2) Use find_package(Threads) to find pthread, and use find_library()
for everything else.

https://cmake.org/cmake/help/latest/command/find_package.html
https://cmake.org/cmake/help/latest/command/find_library.html

Kyle


More information about the CMake mailing list