【发布时间】:2015-01-24 01:12:25
【问题描述】:
我是 C++ 新手,遇到以下问题:
文件: - main.cpp -utils.h - utils.cpp
当我在做的时候:
g++ -c -std=c++11 utils.cpp(编译) g++ -c -std=c++11 main.cpp(编译)
当我尝试链接时:
g++ -o main.o utils.o
/usr/lib/gcc/i686-redhat-linux/4.8.3/../../../crt1.o:在函数
_start': (.text+0x18): undefined reference tomain' utils.o:在函数clean_html(std::string const&)': utils.cpp:(.text+0xfa): undefined reference totidyCreate' utils.cpp:(.text+0x118): 未定义引用tidyOptSetBool' utils.cpp:(.text+0x13b): undefined reference totidyOptSetBool' utils.cpp:(.text+0x15e): 未定义引用tidyOptSetBool' utils.cpp:(.text+0x181): undefined reference totidyOptSetBool' utils.cpp:(.text+0x1a4): 未定义引用tidyOptSetBool' utils.cpp:(.text+0x1c7): undefined reference totidyOptSetValue' utils.cpp:(.text+0x1ea): 未定义引用tidyOptSetValue' utils.cpp:(.text+0x209): undefined reference totidyOptSetBool' utils.cpp:(.text+0x228): 未定义引用tidyOptSetBool' utils.cpp:(.text+0x247): undefined reference totidyOptSetInt' utils.cpp:(.text+0x281): 未定义引用tidyParseString' utils.cpp:(.text+0x295): undefined reference totidyCleanAndRepair' utils.cpp:(.text+0x2b0): 未定义引用tidySaveBuffer' utils.cpp:(.text+0x322): undefined reference totidyBufFree' utils.cpp:(.text+0x32d): 未定义对“tidyRelease”的引用 collect2:错误:ld 返回 1 个退出状态
在 utils.cpp 我有一个函数 clean_html。当我删除此功能时,代码与成功相关联。
我正在使用 gcc 版本 4.8.3 20140911 (Red Hat 4.8.3-7) (GCC)。
Tidy 是通过 fedora repos 安装的,使用: 软件包 libtidy-devel-0.99.0-28.20091203.fc19.i686 已安装且最新版本 软件包 libtidy-0.99.0-28.20091203.fc19.i686 已安装且最新版本
编辑:
忘了说: - 我包括 tidy.h 使用 #包括
tidy.h 位于 /usr/include/tidy.h
【问题讨论】:
-
What is an undefined reference/unresolved external symbol error and how do I fix it? 的可能重复项。您可能错过了在链接阶段指定
-ltidy。 -
你能显示链接命令吗?您可能缺少链接整洁库的
-l选项。 -
@geert3 我添加了链接命令。添加到 -l 的库名称是什么?我该如何解决这个问题?
-
好的,我解决这个问题。谢谢@geert3。我必须将 -ltidy 添加到链接命令中。如果您想添加答案以接受
-
很可能是
/usr/lib/libtidy.so,所以你需要g++ -ltidy -o main.o utils.o。