cmake:target_link_libraries使用不共享的静态库 [英] cmake: target_link_libraries use static library not shared

查看:2818
本文介绍了cmake:target_link_libraries使用不共享的静态库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以告诉cmake链接到静态库,而不是共享?

Is it possible to tell cmake to link against a static library instead of shared?

CMakeLists.txt的顶部,我配置了以下内容:

At the top of my CMakeLists.txt I have the following configured:

set(CMAKE_FIND_LIBRARY_SUFFIXES .a ${CMAKE_FIND_LIBRARY_SUFFIXES})

稍后,我添加一个二进制文件,并告诉它在发布模式下链接到tcmalloc:

Later, I add a binary, and tell it to link against tcmalloc in release mode:

target_link_libraries(${BIN_NAME} optimized tcmalloc_minimal)

生成的makefile链接位于tcmalloc的共享版本上:

The resulting makefile links aginst the shared version of tcmalloc:

$ make VERBOSE=1 | grep tcmalloc
/usr/bin/c++ ... -Wl,-Bdynamic ltcmalloc_minimal 

进一步的证明:

$ ldd app 
    ...
    libtcmalloc_minimal.so.4 => /usr/local/lib/libtcmalloc_minimal.so.4 (0x00007eff89733000)
    ...

tcmalloc的静态版本和共享版本都存在:

Both static and shared versions of tcmalloc exist:

$ ls -1 /usr/local/lib/libtcmalloc_minimal*
/usr/local/lib/libtcmalloc_minimal.a
/usr/local/lib/libtcmalloc_minimal_debug.a
/usr/local/lib/libtcmalloc_minimal_debug.la
/usr/local/lib/libtcmalloc_minimal_debug.so
/usr/local/lib/libtcmalloc_minimal_debug.so.4
/usr/local/lib/libtcmalloc_minimal_debug.so.4.2.6
/usr/local/lib/libtcmalloc_minimal.la
/usr/local/lib/libtcmalloc_minimal.so
/usr/local/lib/libtcmalloc_minimal.so.4
/usr/local/lib/libtcmalloc_minimal.so.4.2.6

问题:

如何配置cmake以链接到tcmalloc的静态版本?

How can I configure cmake to link against the static version of tcmalloc?

推荐答案

您可以创建一个在函数范围内设置CMAKE_FIND_LIBRARY_SUFFIXES的帮助器函数(

You can create a helper function which sets CMAKE_FIND_LIBRARY_SUFFIXES at function scope (so therefore doesn't affect the parent scope) which searches for the library in question and sets an output variable with the result

function(find_static_library LIB_NAME OUT)

    if (WIN32 OR MSVC)
        set(CMAKE_FIND_LIBRARY_SUFFIXES ".lib")
    elseif (UNIX)
        set(CMAKE_FIND_LIBRARY_SUFFIXES ".a")
    endif()      

    find_library(
        FOUND_${LIB_NAME}_STATIC
        ${LIB_NAME}
        )

    if (FOUND_${LIB_NAME}_STATIC)
        get_filename_component(ABS_FILE ${FOUND_${LIB_NAME}_STATIC} ABSOLUTE)
    else()
        message(SEND_ERROR "Unable to find library ${LIB_NAME}")
    endif()

    set(${OUT} ${ABS_FILE} PARENT_SCOPE)

endfunction()

然后可以从CMakeLists.txt中的某个位置调用此函数,以使用库的位置填充变量.

You can then call this function from somewhere in your CMakeLists.txt to populate a variable with the location of the library.

未能找到它会导致严重失败

Failure to find it results in a hard failure

find_static_library(tcmalloc_minimal TCMALLOC)

然后您可以在调用target_link_libraries的过程中使用此变量,并确保您要链接到静态版本

You can then use this variable in your call to target_link_libraries and be sure you're linking against the static version

target_link_libraries(${BIN_NAME} optimized ${TCMALLOC})

在这里您可以看到结果:

Here you can see the result:

$ make VERBOSE=1 | grep tcmalloc

/usr/bin/c++ ... /usr/local/lib/libtcmalloc_minimal.a ...

这篇关于cmake:target_link_libraries使用不共享的静态库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆