【问题标题】:Clion Cmake Static Library link -static-libgcc -static-libstdc++Clion Cmake 静态库链接 -static-libgcc -static-libstdc++
【发布时间】:2020-06-17 08:05:47
【问题描述】:

我正在使用 centos 8 海合会

Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/8/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-redhat-linux
Configured with: ../configure --enable-bootstrap --enable-languages=c,c++,fortran,lto --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-shared --enable-threads=posix --enable-checking=release --enable-multilib --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-gcc-major-version-only --with-linker-hash-style=gnu --enable-plugin --enable-initfini-array --with-isl --disable-libmpx --enable-offload-targets=nvptx-none --without-cuda-driver --enable-gnu-indirect-function --enable-cet --with-tune=generic --with-arch_32=x86-64 --build=x86_64-redhat-linux
Thread model: posix
gcc version 8.3.1 20190507 (Red Hat 8.3.1-4) (GCC)

我的 CMake [更新]

cmake_minimum_required(VERSION 3.10)
project(TestProject)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_FLAGS " -static -static-libgcc -static-libstdc++ -std=c++17")
add_executable(TestProject main.cpp)
SET(BUILD_SHARED_LIBS OFF)

#--------------- Static Includes -----------------
set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)
set(PHTREADS_LIB "-lphreads")
#----- Include Curl
set(CURL_LIBRARY "-lcurl")
find_package(CURL REQUIRED)
include_directories(${CURL_INCLUDE_DIR})

target_link_libraries(TestProject ${CURL_LIBRARIES}  ${PHTREADS_LIB})


问题 [已更新

/usr/bin/ld: cannot find -lcurl
/usr/bin/ld: cannot find -lphreads
/usr/bin/ld: cannot find -lm
/usr/bin/ld: cannot find -lc
collect2: error: ld returned 1 exit status
gmake[3]: *** [CMakeFiles/TestProject.dir/build.make:84: TestProject] Error 1
gmake[2]: *** [CMakeFiles/Makefile2:68: CMakeFiles/TestProject.dir/all] Error 2
gmake[1]: *** [CMakeFiles/Makefile2:80: CMakeFiles/TestProject.dir/rule] Error 2

我的主要目标是尝试将 (libgcc libstdc++) 添加为静态。

有人可以指导我做错了什么吗?

谢谢

【问题讨论】:

  • [root@centos8dev ~]# find /usr/lib* -name 'libstdc++*.a' /usr/lib/gcc/x86_64-redhat-linux/8/libstdc++fs.a /usr/lib/gcc/x86_64-redhat-linux/8/32/libstdc++.a /usr/lib/gcc/x86_64-redhat-linux/8/32/libstdc++fs.a /usr/lib/gcc/x86_64-redhat-linux/8/libstdc++.a
  • 您可以对 libm.alibc.a 进行相同的搜索以确保。
  • 欢迎来到 Stack Overflow!除了 CMake 文件中的几个过时的 CMake 习惯用法之外,对 target_link_libraries() 的调用甚至在定义之前就引用了 TestProject,这应该是一个错误。这没有错误吗?
  • 我不会通过PTHREADS_LIB 变量手动链接-lpthreads,而是将Threads::Threads 添加到您的target_link_libraries 调用中。这也为编译器设置了必要的定义并添加了包含目录。
  • CURL::libcurl 也一样,而不是手动链接。请参阅documentation 了解这些电话。

标签: c++ linux gcc makefile cmake


【解决方案1】:
/usr/bin/ld: cannot find -lm
/usr/bin/ld: cannot find -lc

缺少 glibc-static

Centos 8

dnf --enablerepo=PowerTools install glibc-static
or
yum install glibc-static

for libstdc++

dnf --enablerepo=PowerTools install libstdc++-static
or 
yum install libstdc++-static

Centos 7

yum install glibc-static

包含线程

#----- Include phtread
find_package(Threads REQUIRED)
target_link_libraries(${PROJECT_NAME} Threads::Threads)

包括卷曲 [FIX]

这里对于 Centos 8 来说更复杂

Zlib

dnf --enablerepo=PowerTools install zlib-static

CMake 包含 Zlib

add_library(
        libz
        STATIC IMPORTED
)
set_target_properties(
        libz
        PROPERTIES IMPORTED_LOCATION
        /usr/lib64/libz.a
)

安装静态库

wget https://curl.haxx.se/download/curl-7.61.0.tar.gz

tar -xvf curl-7.61.0.tar.gz

cd curl-7.61.0/

设置配置

./configure --disable-debug --disable-ftp --disable-ldap --disable-ldaps --disable-rtsp --disable-proxy --disable-dict --disable-telnet --disable-tftp --disable-pop3 --disable-imap --disable-smb --disable-smtp --disable-gopher --disable-manual --disable-ipv6 --disable-sspi --disable-crypto-auth --disable-ntlm-wb --disable-tls-srp --without-nghttp2 --without-libidn2 --without-libssh2 --without-brotli --with-zlib --without-ssl

for SSL ( --with-ssl  required openssl-static (not availabe for Centos 8 ) ) 

安装

make && make install

Cmake 更改

add_library(
        libz
        STATIC IMPORTED
)
set_target_properties(
        libz
        PROPERTIES IMPORTED_LOCATION
        /usr/lib64/libz.a
)


target_link_libraries(TestProject libcurl libz Threads::Threads)

【讨论】:

    猜你喜欢
    • 2015-10-04
    • 2012-10-21
    • 2015-06-04
    • 2016-12-06
    • 2014-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-22
    相关资源
    最近更新 更多