【问题标题】:How to correctly link xercesc libraries in C++ compiling?如何在 C++ 编译中正确链接 xercesc 库?
【发布时间】:2021-10-22 22:58:18
【问题描述】:

我对 C++ 完全陌生,并且正在编写一段将插入到更大程序中的代码。这段代码将使用 xercesc 库来读取 XML 文件。我写了一个非常基本的 main 函数来测试。但我无法编译它。非常感谢您的帮助。

Test1.cpp

#include <xercesc/dom/DOMNode.hpp>
#include <xercesc/dom/DOMElement.hpp>
#include <xercesc/dom/DOMNodeList.hpp>
#include <xercesc/parsers/XercesDOMParser.hpp>
#include <string>
#include <iostream>
#include <map>

using namespace std;
using namespace xercesc;


int main() {

  string scRes = "scRes.xml";

  XercesDOMParser* parser = new XercesDOMParser;
  parser->parse(scRes.c_str());

  DOMNodeList* nodeList = parser->getDocument()->getDocumentElement()->getChildNodes();
  DOMNode* root = nodeList->item(0);

  std::cout << root->getNodeName() << endl;

  for (unsigned int i = 0; i < nodeList->getLength(); i++) {
    DOMNode* curr = root->getChildNodes()->item(i);
    std::cout << curr << endl;
  }
}

我的 Makefile 是这样的:

CXX             = g++

LIBDIR          = -L/usr/local/lib -L$(XERCES_LIB) -L$(PWD)

ROOT            = $(PROJDIR)
XERCES_INCLUDE  = $(ROOT)/libs/xercesc/include
XERCES_LIB      = $(ROOT)/libs/xercesc/lib
INCLUDE         = -I$(XERCES_INCLUDE) -I${HOME}/include

CXXFLAGS        = $(INCLUDE) -MMD -std=c++14 -Wno-deprecated
LDFLAGS         = $(LIBDIR) -Wl,-rpath,$(XERCES_LIB)
LDLIBS          = -lxerces-c -lm -Wl,-rpath
AR              = ar ru

all: Test1

Test1: Test1.o
        $(CXX) $(CXXFLAGS) $(LDFLAGS) $(LDLIBS) -o $@ $^

Test1.o: Test1.cpp
        $(CXX) -c $(CXXFLAGS) $^

clean:
        rm *.d *.o

如果我输入 make,我会收到以下错误。

/bin/ld:警告:libicui18n.so.68,/some/server/dir/libs/xercesc/lib/libxerces-c.so 需要,未找到(尝试使用 -rpath 或 -rpath-link)

/bin/ld:警告:libicudata.so.68,/some/server/dir/libs/xercesc/lib/libxerces-c.so 需要,未找到(尝试使用 -rpath 或 -rpath-link)

/lib/../lib64/crt1.o:在函数`_start'中:

(.text+0x20): 对 `main' 的未定义引用

/some/server/dir/libs/xercesc/lib/libxerces-c.so:未定义对 `uset_setSerializedToOne_68'/bin/ld 的引用:警告:libicuuc.so.68,/some/server/dir/ 需要libs/xercesc/lib/libxerces-c.so,未找到(尝试使用 -rpath 或 -rpath-link)

【问题讨论】:

  • 根据documentation,依赖项(例如ICU,libicudata.so是其中的一部分)需要手动安装。

标签: c++ compiler-errors xerces xerces-c


【解决方案1】:

正如弗兰克所说,我没有意识到有必要在 Makefile 中包含 ICU 库。我还在 Makefile 中犯了一些其他错误,因此我将在此处发布现在工作的版本以供其他人参考。

CXX             = g++

LIBDIR          = -L/usr/local/lib -L$(XERCES_LIB) -L$(ICU_LIB)

ROOT            = $(PROJDIR)
XERCES_INCLUDE  = $(ROOT)/libs/xercesc/include
XERCES_LIB      = $(ROOT)/libs/xercesc/lib
ICU_INCLUDE     = $(ROOT)/libs/icu/include/unicode
ICU_LIB         = $(ROOT)/libs/icu/lib
INCLUDE         = -I${HOME}/include -I$(XERCES_INCLUDE) -I$(ICU_INCLUDE)

CXXFLAGS        = $(INCLUDE) -MMD -std=c++14 -Wno-deprecated -Wall
LDFLAGS         = $(LIBDIR) -Wl,-rpath,$(ICU_LIB) -Wl,-rpath,$(XERCES_LIB)
LDLIBS          = -lxerces-c -lm
AR              = ar ru

all: Test1

Test1: main.o
        $(CXX) $(CXXFLAGS) $(LDFLAGS) $(LDLIBS) -o $@ $<

main.o: main.cpp
        $(CXX) $(CXXFLAGS) -c $<

clean:
        rm *.d *.o

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多