Is this the rigth way to build a shared library?

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

Is this the rigth way to build a shared library?

dexter810
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)




When I do ldd libmain.so I get:

   linux-vdso.so.1 (0x00007ffee6583000)
    libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6
(0x00007f7e1be18000)
    libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1
(0x00007f7e1bc00000)
    libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f7e1b80f000)
    libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f7e1b471000)
    /lib64/ld-linux-x86-64.so.2 (0x00007f7e1c3a4000)



CMakeLists.txt for creating a successfully running executable:
cmake_minimum_required(VERSION 3.6)
project(main CXX C)

add_executable(main main.cpp)

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)

on doing ldd main, I got:

    linux-vdso.so.1 (0x00007ffe723d4000)
    libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0
(0x00007fc0c644b000)
    libcrypto.so.1.1 => /usr/lib/x86_64-linux-gnu/libcrypto.so.1.1
(0x00007fc0c5f80000)
    libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6
(0x00007fc0c5bf7000)
    libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007fc0c5859000)
    libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1
(0x00007fc0c5641000)
    libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fc0c5250000)
    /lib64/ld-linux-x86-64.so.2 (0x00007fc0c68d6000)
    libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007fc0c504c000)



--
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: Is this the rigth way to build a shared library?

Kyle Edwards
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
--

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