makefile 编译或者链接问题
未命名.jpg (16.79 KB, 下载次数: 10)
目录
这是我的文件的目录
我写的makefile如下:
#VPATH=$(srcdir)(objdir)(header):.
src=$(wildcard $(srcdir)/*.cpp)
object=$(patsubst $(srcdir)/%.cpp,$(objdir)/%.o,$(src))
srcdir=./source
objdir=./object
hderdir=./header
CXX=g++
CXXFLAGS=-Wall -c -O3 -Iheader
a.out(objdir) $(object)
$(CXX) $(object)
$(objdir):
mkdir -p ./object
$(object)(src)
$(CXX) $(CXXFLAGS) $< -o $@
ls(objdir)
echo $(object)
#main.o:%o:%cpp
# $(CXX) $(CXXFLAGS) $<
#Point.o:%o:%cpp
# $(CXX) $(CXXFLAGS) $<
#Circle.o:%o:%cpp
# $(CXX) $(CXXFLAGS) $<
#Cylinder.o:%o:%cpp
# $(CXX) $(CXXFLAGS) $<
clean:
$(RM) $(object)
$(RM) a.out
rmdir ./object
一共是两个版本,注释掉的部分是我把所有文件放在同一个目录下时写的,编译运行都通过了。
但是按照头文件和源文件分类放了之后,可以编译生成.o文件但是链接就不行了。
报错内容是找不到 运算符重载<<
如下:
g++ ./object/Circle.o ./object/Cylinder.o ./object/main.o ./object/Point.o
./object/Cylinder.o: In function `operator<<(std::basic_ostream<char, std::char_traits<char> >&, Circle&':
Circle.cpp.text+0x70): multiple definition of `operator<<(std::basic_ostream<char, std::char_traits<char> >&, Circle&'
./object/Circle.o:Circle.cpp.text+0x70): first defined here
./object/main.o: In function `operator<<(std::basic_ostream<char, std::char_traits<char> >&, Circle&':
Circle.cpp.text+0x70): multiple definition of `operator<<(std::basic_ostream<char, std::char_traits<char> >&, Circle&'
./object/Circle.o:Circle.cpp.text+0x70): first defined here
./object/Point.o: In function `operator<<(std::basic_ostream<char, std::char_traits<char> >&, Circle&':
Circle.cpp.text+0x70): multiple definition of `operator<<(std::basic_ostream<char, std::char_traits<char> >&, Circle&'
./object/Circle.o:Circle.cpp.text+0x70): first defined here
/usr/lib/gcc/i386-redhat-linux/4.1.2/../../../crt1.o: In function `_start':
(.text+0x1: undefined reference to `main'
./object/Circle.o: In function `operator<<(std::basic_ostream<char, std::char_traits<char> >&, Circle&':
Circle.cpp.text+0x8a): undefined reference to `operator<<(std::basic_ostream<char, std::char_traits<char> >&, Point&'
./object/Cylinder.o: In function `operator<<(std::basic_ostream<char, std::char_traits<char> >&, Circle&':
Circle.cpp.text+0x8a): undefined reference to `operator<<(std::basic_ostream<char, std::char_traits<char> >&, Point&'
./object/main.o: In function `operator<<(std::basic_ostream<char, std::char_traits<char> >&, Circle&)':
Circle.cpp.text+0x8a): undefined reference to `operator<<(std::basic_ostream<char, std::char_traits<char> >&, Point&)'
./object/Point.o: In function `operator<<(std::basic_ostream<char, std::char_traits<char> >&, Circle&)':
Circle.cpp.text+0x8a): undefined reference to `operator<<(std::basic_ostream<char, std::char_traits<char> >&, Point&)'
collect2: ld returned 1 exit status
源文件没有问题,不知道我的makefile错在什么地方?请高手赐教。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论