C++ 链接器命令-没有规则为第三方库创建目标
在我的代码中,我引用了pugixml,如下所示:C++ 链接器命令-没有规则为第三方库创建目标,c++,c++11,makefile,C++,C++11,Makefile,在我的代码中,我引用了pugixml,如下所示: #include "pugi/pugixml.hpp" 编译时出现以下错误: main in main-bf0b72.o "pugi::xml_node::children(char const*) const", referenced from: _main in main-bf0b72.o ld: symbol(s) not found for architecture x86_64 clang: error: link
#include "pugi/pugixml.hpp"
编译时出现以下错误:
main in main-bf0b72.o
"pugi::xml_node::children(char const*) const", referenced from:
_main in main-bf0b72.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [main] Error 1
make: *** No rule to make target `pugi/pugixml.cpp', needed by `main'. Stop.
根据另一个问题,我被告知将pugi作为一个额外的翻译单元,并在我的Makefile中相应地链接它们:
CC = g++
CFLAGS = -g -Wall -std=c++11
SRC = src
INCLUDES = include
TARGET = main
all: $(TARGET)
$(TARGET): $(SRC)/$(TARGET).cpp pugi/pugixml.cpp
$(CC) $(CFLAGS) -I $(INCLUDES) -o $@ $^
clean:
$(RM) $(TARGET)
但是,当我尝试在此更改后运行Makefile时,会出现以下错误:
main in main-bf0b72.o
"pugi::xml_node::children(char const*) const", referenced from:
_main in main-bf0b72.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [main] Error 1
make: *** No rule to make target `pugi/pugixml.cpp', needed by `main'. Stop.
我真的不知道我该做什么。pugi应该有自己的Makefile来单独构建它,还是应该在我的Makefile中指定它不是自己的“目标”
编辑
这是我的文件系统:
- 根/生成文件
- root/src/main.cpp
- root/include/pugi/pugixml.hpp
- root/include/pugi/pugixml.cpp
- root/include/pugi/pugiconfig.hpp
pugi/pugixml.cpp
文件,但在文件系统中,它位于include/pugi/pugixml.cpp
(相对于Makefile本身)。你应该:
- 创建
目录并将root/pugi
文件放在那里 或pugixml.cpp
- 或者在Makefile中将
替换为pugi/pugixml.cpp
(但将源文件保留在include/pugi/pugixml.cpp
子目录中是个坏主意)include/
pugi/pugixml.cpp
吗?@myaut我有,在与.hppI相同的目录中,您认为可以使用Makefile
从目录开始发布文件系统层次结构helpful@myaut编辑!希望这有助于了解情况。我真的很感激你的帮助。非常感谢你的帮助。我是否需要将.coo与.hpp保存在同一文件夹中?我是新手,那么你提到的第一种方法,哪种方法是最标准和推荐的方法?这更像是一个库而不是一个包含吗?@JamesWillson:include子目录(.hpp)中的源文件(.cpp)令人困惑。您可以这样做,它也会起作用,但如果您将源代码显示给其他人,他可能无法找到pugixml.cpp
,因为hi不希望它位于include/
子目录中