c++ - 在 FreeBSD 上构建 lxml 示例时未定义对 glib 的引用

标签 c++ linker freebsd libxml2 glib

我试图在 FreeBSD 10.1-RELEASE 上从 libxml++ 构建示例代码,但在链接库时出现 undefined reference to Glib::... 错误。 在 CentOS 6.5 和 7.0 上编译的代码没有错误。 启用链接器详细模式时,似乎找到了所有 glib 库。 我尝试重新安装 glib 库几次,但没有任何改变。

对于编译失败有什么建议吗?

下面是源代码和 makefile。

主.cc:

#include <libxml++/libxml++.h>

#include <iostream>

void print_indentation(unsigned int indentation) {
        for (unsigned int i = 0; i < indentation; ++i)
                std::cout << " ";
}

void print_node(const xmlpp::Node* node, unsigned int indentation = 0) {
        std::cout << std::endl; //Separate nodes by an empty line.

        const xmlpp::ContentNode* nodeContent = dynamic_cast<const xmlpp::ContentNode*>(node);
        const xmlpp::TextNode* nodeText = dynamic_cast<const xmlpp::TextNode*>(node);
        const xmlpp::CommentNode* nodeComment = dynamic_cast<const xmlpp::CommentNode*>(node);

        if (nodeText && nodeText->is_white_space()) //Let's ignore the indenting - you don't always want to do this.
                return;

        Glib::ustring nodename = node->get_name();

        if (!nodeText && !nodeComment && !nodename.empty()) //Let's not say "name: text".
        {
                print_indentation(indentation);
                std::cout << "Node name = " << node->get_name() << std::endl;
                std::cout << "Node name = " << nodename << std::endl;
        } else if (nodeText) //Let's say when it's text. - e.g. let's say what that white space is.
        {
                print_indentation(indentation);
                std::cout << "Text Node" << std::endl;
        }

        //Treat the various node types differently: 
        if (nodeText) {
                print_indentation(indentation);
                std::cout << "text = \"" << nodeText->get_content() << "\"" << std::endl;
        } else if (nodeComment) {
                print_indentation(indentation);
                std::cout << "comment = " << nodeComment->get_content() << std::endl;
        } else if (nodeContent) {
                print_indentation(indentation);
                std::cout << "content = " << nodeContent->get_content() << std::endl;
        } else if (const xmlpp::Element* nodeElement = dynamic_cast<const xmlpp::Element*>(node)) {
                //A normal Element node:

                //line() works only for ElementNodes.
                print_indentation(indentation);
                std::cout << "     line = " << node->get_line() << std::endl;

                //Print attributes:
                const xmlpp::Element::AttributeList& attributes = nodeElement->get_attributes();
                for (xmlpp::Element::AttributeList::const_iterator iter = attributes.begin(); iter != attributes.end(); ++iter) {
                        const xmlpp::Attribute* attribute = *iter;
                        print_indentation(indentation);
                        std::cout << "  Attribute " << attribute->get_name() << " = " << attribute->get_value() << std::endl;
                }

                const xmlpp::Attribute* attribute = nodeElement->get_attribute("title");
                if (attribute) {
                        std::cout << "title found: =" << attribute->get_value() << std::endl;

                }
        }

        if (!nodeContent) {
                //Recurse through child nodes:
                xmlpp::Node::NodeList list = node->get_children();
                for (xmlpp::Node::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter) {
                        print_node(*iter, indentation + 2); //recursive
                }
        }
}

int main(int argc, char* argv[]) {
        std::string filepath;
        if (argc > 1)
                filepath = argv[1]; //Allow the user to specify a different XML file to parse.
        else
                filepath = "example.xml";

        try {
                xmlpp::DomParser parser;
                parser.set_validate();
                parser.set_substitute_entities(); //We just want the text to be resolved/unescaped automatically.
                parser.parse_file(filepath);
                if (parser) {
                        //Walk the tree:
                        const xmlpp::Node* pNode = parser.get_document()->get_root_node(); //deleted by DomParser.
                        print_node(pNode);
                }
        } catch (const std::exception& ex) {
                std::cout << "Exception caught: " << ex.what() << std::endl;
        }

        return 0;
}

生成文件:

OBJDIR=.obj

OBJS_MAIN = $(OBJDIR)/main.o

OBJS_ALL = $(OBJS_MAIN)

default: ao3

CC = g++48

#CENTOS_VERSION=$(shell sed 's/[^0-9]*\([0-9]*\)\.\([0-9]\).*/\1\2/' < /etc/redhat-release )
CENTOS_VERSION=70
CFLAGS_GS = -DWITH_OPENSSL -DWITH_GZIP -DWITH_CDATA -D CENTOS_VERSION=$(CENTOS_VERSION) -pthread

#  -Wl,--verbose 
OFLAGS= \
  -Wl,--verbose \
  -L/usr/local/lib \
  -L "/usr/lib64/mysql" \
  -L "/usr/lib/mysql" \
  -L "/usr/local/lib/mysql" \
  -L "/usr/lib64" \
  -L "/usr/lib" \
  -L "/usr/share/axiscpp/lib" \
  -L "/usr/local/axis/lib" \
  -L "/usr/local/lib/amadeus_2_1_39_5" \
  -lboost_iostreams \
  -lboost_date_time \
  -lmysqlclient \
  -lxml++-2.6 \
  -lxml2 \
  -lglibmm-2.4 \
  -lgobject-2.0 \
  -lgmodule-2.0 \
  -lglib-2.0 \
  -lloki \
  -liconv\
  -lpcre \
  -lsigc-2.0 \
  -lSockets \
  -lssl \
  -lintl \
  -lffi \
  -llzma \
  -lpthread \
  -lexpat \
  -lbz2 \
  -lglib \
  -lz \
  -luuid \
  -lthr \
  -lc++ \
  -lm \
  -lcxxrt \
  -lgcc_s \
  -lc

ifeq ($(CENTOS_VERSION),65)
OFLAGS += -lboost_system
endif

ifeq ($(CENTOS_VERSION),70)
OFLAGS += -lboost_system
OFLAGS += -lcrypto
OFLAGS += -lboost_filesystem
endif

EXTCFLAGS := -g

LDFLAGS= -L/usr/local/lib

#  -Weffc++ 
CFLAGS= $(EXTCFLAGS) \
  -Wall \
  -Werror \
  -Wextra \
  -D LARGEFILE64_SOURCE \
  -D FILE_OFFSET_BITS=64 \
  -D _UP_UNIX_ \
  -D CNT_VERSION=2_3 \
  -D LOKI_OBJECT_LEVEL_THREADING \
  -D CENTOS_VERSION=$(CENTOS_VERSION) \
  -isystem"/usr/include/" \
  -isystem"/usr/local/include/" \
  -isystem"/usr/include/libxml++-2.6" \
  -isystem"/usr/local/include/libxml++-2.6" \
  -isystem"/usr/local/include/libxml++-2.6/include" \
  -isystem"/usr/lib/libxml++-2.6/include" \
  -isystem"/usr/local/lib/libxml++-2.6/include" \
  -isystem"/usr/lib64/libxml++-2.6/include" \
  -isystem"/usr/include/glibmm-2.4" \
  -isystem"/usr/local/include/glibmm-2.4" \
  -isystem"/usr/lib/glibmm-2.4/include" \
  -isystem"/usr/local/lib/glibmm-2.4/include" \
  -isystem"/usr/lib64/glibmm-2.4/include" \
  -isystem"/usr/local/include/glib-2.0/" \
  -isystem"/usr/include/glib-2.0/" \
  -isystem"/usr/lib/glib-2.0/include" \
  -isystem"/usr/local/lib/glib-2.0/include" \
  -isystem"/usr/lib64/glib-2.0/include" \
  -isystem"/usr/lib/glib/include" \
  -isystem"/usr/lib64/glib/include" \
  -isystem"/usr/local/include/glib-1.2" \
  -isystem"/usr/include/mysql" \
  -isystem"/usr/local/include/mysql"

$(OBJDIR)/%.o: %.cc
        @mkdir -p $(@D)
        $(CC) -MMD -MP -c $(CFLAGS) $(INCLUDE) -o $@   $<
        @echo -n '.' >>  "$(DOTSFILE)"
        @curr=`wc -c < $(DOTSFILE)`; max=`cat $(MAXFILE)`; echo -e "\033[1m$$curr/$$max\033[0m"

$(OBJDIR)/main.o: main.cc
        @mkdir -p $(OBJDIR)
        $(CC) -MMD -MP -c $(CFLAGS) $(INCLUDE) -o $@   $<

-include $(OBJS_ALL:.o=.d)

DOTSFILE := /dev/null
MAXFILE := /dev/null

help:
        @echo 'ao3, test, profile, clean, cleantest, cleanprofile'
        @echo 'CentOS version: '$(CENTOS_VERSION)

ao3: $(OBJS_ALL)
        $(CC) --verbose -o ao3 -Wl,--start-group $(OBJS_ALL) $(OFLAGS) -Wl,--end-group

test: $(OBJS_TEST_ALL)
        $(CC) -o ao3_test $(OBJS_TEST_ALL) $(OFLAGS)

profile: $(OBJS_PROFILE_ALL)
        $(CC) -o ao3_profile $(OBJS_PROFILE_ALL) $(OFLAGS)

clean:
        rm -f $(wildcard $(OBJS_ALL) ao3 $(OBJS_ALL:.o=.d))
        find $(OBJDIR) -type d -empty -delete

cleantest:
        rm -f $(wildcard $(OBJS_TEST) ao3_test $(OBJS_TEST:.o=.d))
        find $(OBJDIR) -type d -empty -delete

cleanprofile:
        rm -f $(wildcard $(OBJS_PROFILE) ao3_profile $(OBJS_PROFILE:.o=.d))
        find $(OBJDIR) -type d -empty -delete

最佳答案

所以问题是 glibmm 是由 clang 编译的,而我正在使用 gcc 进行编译。当使用 c++ 而不是 g++48 进行编译时,它工作正常。

关于c++ - 在 FreeBSD 上构建 lxml 示例时未定义对 glib 的引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27783834/

相关文章:

freebsd - 为什么 "freebsd-version"和 "freebsd-version -k"的输出不同?

python - kqueue 跟踪文件更改 - 在处理以前的事件时有可能丢失事件吗?

c++ - QToolButton 在 QFrame/QWidget 中的视觉行为

c++ - 将 "*&"参数传递给函数

c++ - g++ 说引用未定义,即使 `nm` 在我的目标文件中列出了符号定义

c++ - 在编译和运行时之间发生变化时如何解析共享库位置?

c++ - C++ 标准中 [basic.scope.hiding] 中的 "same scope"到底意味着什么?

c++ - 如何将字符数组转换为字符串?

c++ - Libusb undefined reference

c++ - 如何在应用程序中使用四核 CPU