【问题标题】:How to compile a c++ program that uses both windows.h and google glog (glog/logging.h)?如何编译同时使用 windows.h 和 google glog (glog/logging.h) 的 c++ 程序?
【发布时间】:2017-05-11 17:09:32
【问题描述】:

我有两个库 A 和 B。库 B 是我自己的库,我需要其中的“windows.h”。此外,对于某些功能,我需要使用第三方库 A。A 使用谷歌日志库,这是问题所在:

第一个错误是这样的:

Severity Code Description Project File Line Error C1189 #error:  ERROR macro is defined. Define GLOG_NO_ABBREVIATED_SEVERITIES before including logging.h

我在“A.h”之前定义了 GLOG_NO_ABBREVIATED_SEVERITIES,但在那之后出现了奇怪的链接错误。我已经测试了here 建议的所有解决方案,但它们都不起作用。

在使用“windows.h”的项目中有没有其他方法可以使用glog?

编辑:

链接器错误是:

 error LNK2019: unresolved external symbol "__declspec(dllimport) public: char __thiscall std::basic_ios<char,struct std::char_traits<char> >::fill(char)" (__imp_?fill@?$basic_ios@DU?$char_traits@D@std@@@std@@QAEDD@Z) referenced in function "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl std::operator<<<char,struct std::char_traits<char>,char>(class std::basic_ostream<char,struct std::char_traits<char> > &,struct std::_Fillobj<char> const &)" (??$?6DU?$char_traits@D@std@@D@std@@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@0@AAV10@ABU?$_Fillobj@D@0@@Z)

 error LNK2019: unresolved external symbol "__declspec(dllimport) public: class std::basic_ostream<char,struct std::char_traits<char> > & __thiscall std::basic_ostream<char,struct std::char_traits<char> >::operator<<(class std::ios_base & (__cdecl*)(class std::ios_base &))" (__imp_??6?$basic_ostream@DU?$char_traits@D@std@@@std@@QAEAAV01@P6AAAVios_base@1@AAV21@@Z@Z) referenced in function _getHtsLables

 error LNK2019: unresolved external symbol "__declspec(dllimport) public: class std::basic_ostream<char,struct std::char_traits<char> > & __thiscall std::basic_ostream<char,struct std::char_traits<char> >::operator<<(double)" (__imp_??6?$basic_ostream@DU?$char_traits@D@std@@@std@@QAEAAV01@N@Z) referenced in function "public: virtual void __thiscall AD3::FactorDense::Print(class std::basic_ostream<char,struct std::char_traits<char> > &)" (?Print@FactorDense@AD3@@UAEXAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@@Z)

 fatal error LNK1120: 3 unresolved externals

【问题讨论】:

  • 您在这里可能遇到的问题不止一个。通过遵循#error 消息中的建议似乎可以修复“错误宏已定义”:“在包含 logging.h 之前定义 GLOG_NO_ABBREVIATED_SEVERITIES”(这不是在您的 #include “Ah”之前,就像您所做的那样,但似乎有效不知何故。链接器错误可能是由于未链接到某些必需的库引起的。我建议您编辑您的问题并添加链接器错误。
  • 将A和B的使用分开到两个编译单元中,这样就不需要在一个编译单元中包含两者,从而避免冲突?
  • 它们并不令人困惑,但是在没有看到显示您的问题的最小可编译源代码示例的情况下试图理解您所做的事情可能非常困难。我看到您正在使用 Microsoft Visual C++ 编译器(哪个版本?)。导致这些链接器错误的原因之一可能是导出 C++ 符号时库和程序的编译器设置不匹配(即有些是调试的,有些不是)。
  • 只要不在同一个B的函数中调用A的函数和windows.h的函数,并且B的header不依赖A的header和windows.h就可以。跨度>
  • 要回答你的其他问题,你可能需要在添加GLOG_NO_ABBREVIATED_SEVERITIES后重新编译A

标签: c++ c windows visual-c++ glog


【解决方案1】:
  1. 在包含logging.h之前定义GLOG_NO_ABBREVIATED_SEVERITIES

     #define GLOG_NO_ABBREVIATED_SEVERITIES
     #include <windows.h>
     #include <glog/logging.h>
    
  2. 在 Visual Studio 的预处理器定义中添加GLOG_NO_ABBREVIATED_SEVERITIES,即Project &gt; Properties &gt; C/C++ &gt; Preprocessor

【讨论】:

    【解决方案2】:

    我在添加 GLOG_NO_ABBREVIATED_SEVERITIES 后重新编译了 A,但没有任何改变。但是通过重新排序包含“A.h”和“windows.h”解决了这个问题。当我在“windows.h”之前包含“A.h”时,不会出现错误!!!我不明白链接器错误的真正原因是什么! – 付款人

    【讨论】:

      猜你喜欢
      • 2018-09-14
      • 2019-07-22
      • 2014-06-25
      • 2014-06-28
      • 1970-01-01
      • 2021-04-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多