尝试在Windows上使用glog会导致未定义的引用

时间:2018-02-19 12:31:38

标签: c++ cmake glog

我正在尝试glog。我已经下载了最新版本(0.3.5)

我尝试用cmake编译。

我所做的是:

  • 我编译为带有选项BUILD_TESTING = OFFWITH_GFLAGS = OFF的静态库,并在linux上完美地使用了libglog.a

  • 现在我尝试使用Windows。我用与静态库相同的选项编译了glog:

    [ 11%] Building CXX object CMakeFiles/glog.dir/src/demangle.cc.obj
    [ 22%] Building CXX object CMakeFiles/glog.dir/src/logging.cc.obj
    [ 33%] Building CXX object CMakeFiles/glog.dir/src/raw_logging.cc.obj
    [ 44%] Building CXX object CMakeFiles/glog.dir/src/symbolize.cc.obj
    [ 55%] Building CXX object CMakeFiles/glog.dir/src/utilities.cc.obj
    [ 66%] Building CXX object CMakeFiles/glog.dir/src/vlog_is_on.cc.obj
    [ 77%] Building CXX object CMakeFiles/glog.dir/src/signalhandler.cc.obj
    [ 88%] Building CXX object CMakeFiles/glog.dir/src/windows/port.cc.obj
    [100%] Linking CXX static library libglogd.a
    [100%] Built target glog
    
  • 然后,当我尝试在包含libglogd.a的项目(可执行文件)中使用它时就像在Linux中一样,我在编译可执行文件时遇到了链接的这些例外:

    undefined reference to `_imp___ZN6google17InitGoogleLoggingEPKc'
    undefined reference to `_imp___ZN6google10LogMessageC1EPKci'
    undefined reference to `_imp___ZN6google10LogMessage6streamEv'
    undefined reference to `_imp___ZN6google10LogMessageD1Ev'
    undefined reference to `_imp___ZN6google10LogMessageC1EPKcii'
    undefined reference to `_imp___ZN6google10LogMessage6streamEv'
    undefined reference to `_imp___ZN6google10LogMessageD1Ev'
    undefined reference to `_imp___ZN6google10LogMessageD1Ev'
    undefined reference to `_imp___ZN6google10LogMessageD1Ev'
    
  • 我找不到任何关于此的更多信息。

这是可执行文件的CMakeLists.txt:

project(exe)
cmake_minimum_required(VERSION 2.8)
aux_source_directory(. SRC_LIST)

#Ignore QT specified variables
set(ignoreMe "${QT_QMAKE_EXECUTABLE}")

#set(HEADERS foo.h)

add_executable(${PROJECT_NAME} ${SRC_LIST})

if (!WIN32)
    target_include_directories(${PROJECT_NAME} PUBLIC
        /home/glog-master/trunk/build/linux/Debug
        /home/glog-master/trunk/src
        $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
        $<INSTALL_INTERFACE:include/${PROJECT_NAME}>)

    target_link_libraries(${PROJECT_NAME} /home/glog-master/trunk/build/linux/Debug/libglogd.a)
else()
    target_include_directories(${PROJECT_NAME} PUBLIC
        D:/glog-master/trunk/build/windows/Debug
        D:/glog-master/trunk/src
        $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
        $<INSTALL_INTERFACE:include/${PROJECT_NAME}>)

    target_link_libraries(${PROJECT_NAME} D:/glog-master/trunk/build/windows/Debug/libglogd.a)
endif()

这是唯一的可执行文件main.cpp:

#include <iostream>
#include "glog/logging.h"

using namespace std;

int main() {

    google::InitGoogleLogging("MYEXE");

    LOG(INFO) << "This is an info  message MAIN";
    LOG(WARNING) << "This is a warning message MAIN";

    return 0;
}

我在Windows中想念什么?

编辑:图书馆包含没有 _imp 的符号。我怎么摆脱它?我没有dllimport。

编辑2:好吧,我确实有dllimport。我不应该。为了摆脱它,我发现我应该定义 GOOGLE_GLOG_DLL_DECL 。当我定义它时,未定义的引用是:

undefined reference to google::InitGoogleLogging(char const*)
undefined reference to google::LogMessage::LogMessage(char const*, int)
...

1 个答案:

答案 0 :(得分:0)

我做了这些事情:

  • 我必须将glog安装到系统中,然后将其与此链接:

    target_link_libraries(${PROJECT_NAME glog::glog}
    

Here表示这样做会自动添加定义和选项。因此,Windows上还有一些选项/定义。我宁愿知道它们而不是像我在linux上那样强制安装glog并直接从它的构建路径使用库。

这仍然很神秘。如果有人知道,请发表评论。

  • 然后,我有其他未解决的符号。通过添加 dbghelp 作为目标库来解决这些问题。我也不知道为什么我被迫将其包含在Windows上。

但最终在这些更改后,我的可执行文件被编译并运行。