简体   繁体   English

如何在 C++ 编译中正确链接 xercesc 库?

[英]How to correctly link xercesc libraries in C++ compiling?

I am completely new to C++ and am working on a piece of code that will be inserted into a larger program.我对 C++ 完全陌生,正在编写一段将插入到更大程序中的代码。 The piece of code will use the xercesc library to read an XML file.这段代码将使用 xercesc 库来读取一个 XML 文件。 I wrote a very basic main function to test things out.我写了一个非常基本的 main function 来测试。 But I cannot get it compiled.但我无法编译它。 Help will be much appreciated.帮助将不胜感激。

Test1.cpp测试1.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;
  }
}

My Makefile is like:我的 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

If I type make, I receive the following errors.如果我输入 make,我会收到以下错误。

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

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

/lib/../lib64/crt1.o: In function `_start': /lib/../lib64/crt1.o: 在 function `_start':

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

/some/server/dir/libs/xercesc/lib/libxerces-c.so: undefined reference to `uset_setSerializedToOne_68'/bin/ld: warning: libicuuc.so.68, needed by /some/server/dir/libs/xercesc/lib/libxerces-c.so, not found (try using -rpath or -rpath-link) /some/server/dir/libs/xercesc/lib/libxerces-c.so: 未定义引用`uset_setSerializedToOne_68'/bin/ld: warning: libicuuc.so.68, /some/server/dir/libs/xercesc 需要/lib/libxerces-c.so,未找到(尝试使用 -rpath 或 -rpath-link)

As Frank commented, I did not realize it is necessary to include the ICU library in the Makefile. I also made a few other mistakes in the Makefile, so I will post the now-working version here for other people's reference.正如弗兰克评论的那样,我没有意识到有必要在 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM