编译报错 undefined reference to `google::kLogSiteUninitialized‘ 的参考解决方法

/usr/bin/ld: CMakeFiles/testIcp.dir/src/testIcp.cpp.o: in function `main':
testIcp.cpp:(.text.startup+0xee): undefined reference to `google::InitGoogleLogging(char const*)'
/usr/bin/ld: testIcp.cpp:(.text.startup+0x101): undefined reference to `fLI::FLAGS_stderrthreshold'
/usr/bin/ld: testIcp.cpp:(.text.startup+0x10b): undefined reference to `fLB::FLAGS_colorlogtostderr'
/usr/bin/ld: testIcp.cpp:(.text.startup+0x26e): undefined reference to `google::LogMessage::LogMessage(char const*, int)'
/usr/bin/ld: testIcp.cpp:(.text.startup+0x276): undefined reference to `google::LogMessage::stream()'
/usr/bin/ld: testIcp.cpp:(.text.startup+0x28d): undefined reference to `google::LogMessage::~LogMessage()'
/usr/bin/ld: testIcp.cpp:(.text.startup+0x2a1): undefined reference to `google::LogMessage::LogMessage(char const*, int)'
/usr/bin/ld: testIcp.cpp:(.text.startup+0x2a9): undefined reference to `google::LogMessage::stream()'
/usr/bin/ld: testIcp.cpp:(.text.startup+0x2e1): undefined reference to `google::LogMessage::~LogMessage()'
/usr/bin/ld: testIcp.cpp:(.text.startup+0x2f5): undefined reference to `google::LogMessage::LogMessage(char const*, int)'
/usr/bin/ld: testIcp.cpp:(.text.startup+0x2fd): undefined reference to `google::LogMessage::stream()'
/usr/bin/ld: testIcp.cpp:(.text.startup+0x338): undefined reference to `google::LogMessage::~LogMessage()'
/usr/bin/ld: testIcp.cpp:(.text.startup+0x391): undefined reference to `google::LogMessage::LogMessage(char const*, int, int)'
/usr/bin/ld: testIcp.cpp:(.text.startup+0x399): undefined reference to `google::LogMessage::stream()'
/usr/bin/ld: testIcp.cpp:(.text.startup+0x3b4): undefined reference to `google::LogMessage::~LogMessage()'
/usr/bin/ld: CMakeFiles/testIcp.dir/src/testIcp.cpp.o: in function `main.cold':
testIcp.cpp:(.text.unlikely+0x4): undefined reference to `google::LogMessage::~LogMessage()'
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/testIcp.dir/build.make:189: testIcp] Error 1
make[1]: *** [CMakeFiles/Makefile2:83: CMakeFiles/testIcp.dir/all] Error 2
make: *** [Makefile:91: all] Error 2

我试了很多种方法:包括重新编译安装gflags和glog等方法;

1、重新安装gflags

git clone https://github.com/gflags/gflags.git
cd gflags
mkdir build && cd build
cmake -DCMAKE_INSTALL_PREFIX=/usr/local -DBUILD_SHARED_LIBS=ON -DGFLAGS_NAMESPACE=gflags ../ 
make -j4
sudo make install

2、重新安装glog

git clone https://github.com/google/glog
cd glog/cmake/
cmake ..
sudo make install

最后没办法,我参照了高翔自动驾驶书中的做法:新建一个cmake文件夹,在里面新建一个FindGlog.cmake文件,内容如下:

# Ceres Solver - A fast non-linear least squares minimizer
# Copyright 2015 Google Inc. All rights reserved.
# http://ceres-solver.org/
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice,
#   this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright notice,
#   this list of conditions and the following disclaimer in the documentation
#   and/or other materials provided with the distribution.
# * Neither the name of Google Inc. nor the names of its contributors may be
#   used to endorse or promote products derived from this software without
#   specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
# Author: alexs.mac@gmail.com (Alex Stewart)
#

# FindGlog.cmake - Find Google glog logging library.
#
# This module defines the following variables:
#
# GLOG_FOUND: TRUE iff glog is found.
# GLOG_INCLUDE_DIRS: Include directories for glog.
# GLOG_LIBRARIES: Libraries required to link glog.
#
# The following variables control the behaviour of this module:
#
# GLOG_INCLUDE_DIR_HINTS: List of additional directories in which to
#                         search for glog includes, e.g: /timbuktu/include.
# GLOG_LIBRARY_DIR_HINTS: List of additional directories in which to
#                         search for glog libraries, e.g: /timbuktu/lib.
#
# The following variables are also defined by this module, but in line with
# CMake recommended FindPackage() module style should NOT be referenced directly
# by callers (use the plural variables detailed above instead).  These variables
# do however affect the behaviour of the module via FIND_[PATH/LIBRARY]() which
# are NOT re-called (i.e. search for library is not repeated) if these variables
# are set with valid values _in the CMake cache_. This means that if these
# variables are set directly in the cache, either by the user in the CMake GUI,
# or by the user passing -DVAR=VALUE directives to CMake when called (which
# explicitly defines a cache variable), then they will be used verbatim,
# bypassing the HINTS variables and other hard-coded search locations.
#
# GLOG_INCLUDE_DIR: Include directory for glog, not including the
#                   include directory of any dependencies.
# GLOG_LIBRARY: glog library, not including the libraries of any
#               dependencies.

# Reset CALLERS_CMAKE_FIND_LIBRARY_PREFIXES to its value when
# FindGlog was invoked.
macro(GLOG_RESET_FIND_LIBRARY_PREFIX)
    if (MSVC)
        set(CMAKE_FIND_LIBRARY_PREFIXES "${CALLERS_CMAKE_FIND_LIBRARY_PREFIXES}")
    endif (MSVC)
endmacro(GLOG_RESET_FIND_LIBRARY_PREFIX)

# Called if we failed to find glog or any of it's required dependencies,
# unsets all public (designed to be used externally) variables and reports
# error message at priority depending upon [REQUIRED/QUIET/<NONE>] argument.
macro(GLOG_REPORT_NOT_FOUND REASON_MSG)
    unset(GLOG_FOUND)
    unset(GLOG_INCLUDE_DIRS)
    unset(GLOG_LIBRARIES)
    # Make results of search visible in the CMake GUI if glog has not
    # been found so that user does not have to toggle to advanced view.
    mark_as_advanced(CLEAR GLOG_INCLUDE_DIR
            GLOG_LIBRARY)

    glog_reset_find_library_prefix()

    # Note <package>_FIND_[REQUIRED/QUIETLY] variables defined by FindPackage()
    # use the camelcase library name, not uppercase.
    if (Glog_FIND_QUIETLY)
        message(STATUS "Failed to find glog - " ${REASON_MSG} ${ARGN})
    elseif (Glog_FIND_REQUIRED)
        message(FATAL_ERROR "Failed to find glog - " ${REASON_MSG} ${ARGN})
    else ()
        # Neither QUIETLY nor REQUIRED, use no priority which emits a message
        # but continues configuration and allows generation.
        message("-- Failed to find glog - " ${REASON_MSG} ${ARGN})
    endif ()
endmacro(GLOG_REPORT_NOT_FOUND)

# Handle possible presence of lib prefix for libraries on MSVC, see
# also GLOG_RESET_FIND_LIBRARY_PREFIX().
if (MSVC)
    # Preserve the caller's original values for CMAKE_FIND_LIBRARY_PREFIXES
    # s/t we can set it back before returning.
    set(CALLERS_CMAKE_FIND_LIBRARY_PREFIXES "${CMAKE_FIND_LIBRARY_PREFIXES}")
    # The empty string in this list is important, it represents the case when
    # the libraries have no prefix (shared libraries / DLLs).
    set(CMAKE_FIND_LIBRARY_PREFIXES "lib" "" "${CMAKE_FIND_LIBRARY_PREFIXES}")
endif (MSVC)

# Search user-installed locations first, so that we prefer user installs
# to system installs where both exist.
list(APPEND GLOG_CHECK_INCLUDE_DIRS
        /usr/local/include
        /usr/local/homebrew/include # Mac OS X
        /opt/local/var/macports/software # Mac OS X.
        /opt/local/include
        /usr/include)
# Windows (for C:/Program Files prefix).
list(APPEND GLOG_CHECK_PATH_SUFFIXES
        glog/include
        glog/Include
        Glog/include
        Glog/Include)

list(APPEND GLOG_CHECK_LIBRARY_DIRS
        /usr/local/lib
        /usr/local/homebrew/lib # Mac OS X.
        /opt/local/lib
        /usr/lib)
# Windows (for C:/Program Files prefix).
list(APPEND GLOG_CHECK_LIBRARY_SUFFIXES
        glog/lib
        glog/Lib
        Glog/lib
        Glog/Lib)

# Search supplied hint directories first if supplied.
find_path(GLOG_INCLUDE_DIR
        NAMES glog/logging.h
        PATHS ${GLOG_INCLUDE_DIR_HINTS}
        ${GLOG_CHECK_INCLUDE_DIRS}
        PATH_SUFFIXES ${GLOG_CHECK_PATH_SUFFIXES})
if (NOT GLOG_INCLUDE_DIR OR
        NOT EXISTS ${GLOG_INCLUDE_DIR})
    glog_report_not_found(
            "Could not find glog include directory, set GLOG_INCLUDE_DIR "
            "to directory containing glog/logging.h")
endif (NOT GLOG_INCLUDE_DIR OR
        NOT EXISTS ${GLOG_INCLUDE_DIR})

find_library(GLOG_LIBRARY NAMES glog
        PATHS ${GLOG_LIBRARY_DIR_HINTS}
        ${GLOG_CHECK_LIBRARY_DIRS}
        PATH_SUFFIXES ${GLOG_CHECK_LIBRARY_SUFFIXES})
if (NOT GLOG_LIBRARY OR
        NOT EXISTS ${GLOG_LIBRARY})
    glog_report_not_found(
            "Could not find glog library, set GLOG_LIBRARY "
            "to full path to libglog.")
endif (NOT GLOG_LIBRARY OR
        NOT EXISTS ${GLOG_LIBRARY})

# Mark internally as found, then verify. GLOG_REPORT_NOT_FOUND() unsets
# if called.
set(GLOG_FOUND TRUE)

# Glog does not seem to provide any record of the version in its
# source tree, thus cannot extract version.

# Catch case when caller has set GLOG_INCLUDE_DIR in the cache / GUI and
# thus FIND_[PATH/LIBRARY] are not called, but specified locations are
# invalid, otherwise we would report the library as found.
if (GLOG_INCLUDE_DIR AND
        NOT EXISTS ${GLOG_INCLUDE_DIR}/glog/logging.h)
    glog_report_not_found(
            "Caller defined GLOG_INCLUDE_DIR:"
            " ${GLOG_INCLUDE_DIR} does not contain glog/logging.h header.")
endif (GLOG_INCLUDE_DIR AND
        NOT EXISTS ${GLOG_INCLUDE_DIR}/glog/logging.h)
# TODO: This regex for glog library is pretty primitive, we use lowercase
#       for comparison to handle Windows using CamelCase library names, could
#       this check be better?
string(TOLOWER "${GLOG_LIBRARY}" LOWERCASE_GLOG_LIBRARY)
if (GLOG_LIBRARY AND
        NOT "${LOWERCASE_GLOG_LIBRARY}" MATCHES ".*glog[^/]*")
    glog_report_not_found(
            "Caller defined GLOG_LIBRARY: "
            "${GLOG_LIBRARY} does not match glog.")
endif (GLOG_LIBRARY AND
        NOT "${LOWERCASE_GLOG_LIBRARY}" MATCHES ".*glog[^/]*")

# Set standard CMake FindPackage variables if found.
if (GLOG_FOUND)
    set(GLOG_INCLUDE_DIRS ${GLOG_INCLUDE_DIR})
    set(GLOG_LIBRARIES ${GLOG_LIBRARY})
endif (GLOG_FOUND)

glog_reset_find_library_prefix()

# Handle REQUIRED / QUIET optional arguments.
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(Glog DEFAULT_MSG
        GLOG_INCLUDE_DIRS GLOG_LIBRARIES)

# Only mark internal variables as advanced if we found glog, otherwise
# leave them visible in the standard GUI for the user to set manually.
if (GLOG_FOUND)
    mark_as_advanced(FORCE GLOG_INCLUDE_DIR
            GLOG_LIBRARY)
endif (GLOG_FOUND)

完事在自己的CMakeLists.txt文件中,添加上这一句话:编译通过了。 

list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)

细节原因未知:如果有谁知道感谢您的留言。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C++编译过程中,当出现"undefined reference to"的错误时,一般是由于链接时找不到对应的库引起的。解决这个问题的方法有以下几种: 1. 确保库文件存在:首先要确保所需的库文件存在,并且路径正确。可以使用`ls`命令检查库文件是否存在。 2. 添加库文件路径:如果库文件存在于非标准路径下,可以通过设置环境变量`LIBRARY_PATH`来告诉编译器库文件的位置。例如,可以使用以下命令将`/usr/local/lib`路径添加到`LIBRARY_PATH`环境变量中: ```shell export LIBRARY_PATH=/usr/local/lib:$LIBRARY_PATH ``` 3. 指定库文件:如果库文件的路径无法通过环境变量解决,可以直接在编译命令中指定库文件的路径。例如,使用`-L`选项指定库文件的路径: ```shell g++ -o output_file source_file.cpp -L/path/to/library -llibrary_name ``` 4. 检查库文件名:确保在编译命令中正确指定了库文件的名称。库文件名通常以`lib`开头,并以`.a`(静态库)或`.so`(共享库)结尾。 5. 检查函数名:如果报错信息中指定了具体的函数名,可以检查函数名是否正确拼写,并确保在编译命令中正确链接了对应的库文件。 6. 检查编译顺序:如果项目中有多个源文件和库文件,确保在编译命令中按照正确的顺序指定它们。一般来说,先编译源文件,再链接库文件。 7. 检查编译选项:有时候需要添加一些特定的编译选项来解决链接错误。例如,使用`-l`选项指定链接的库文件。 8. 检查编译器版本:某些链接错误可能是由于编译器版本不兼容导致的。可以尝试升级或更换编译器版本。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值