我目前有这样的声明
std::string student::returnData()
{
// return "This is return type" ;
std::cout << "Hello World";
}
然而,当我构建C ++项目时
entry: main.o student.o
g++ -W -Wall -Werror -o $@ main.o student.o
main.o:
g++ -W -Wall -Werror -c main.cpp
clean:
rm -rf *.o entry
我没有收到警告/错误,当这个方法执行时,输出开始变得疯狂 关于我能做什么的任何建议,我可以得到像这些愚蠢的错误警告
答案 0 :(得分:2)
gcc
将使用标记-Wall -Werror
发出错误。问题是您没有目标student.o
的规则,隐式规则不使用任何警告标志。最简单的方法可能是全局设置警告标志,让隐式规则执行它们的操作。这可以通过使用CXXFLAGS
变量(或CFLAGS
用于C代码)来实现:
CXXFLAGS += -Wall -Werror
这将照顾student.o
以及没有明确规则的任何.o
。请注意,此规则会使student.o
隐式依赖于student.cpp
,student.cc or student.C
。您可以在不修改配方的情况下添加依赖项:
student.o: student.h # student.o now depends on student.h too
这会导致格式错误
错误:函数返回非void [-Werror = return-type]
时没有return语句
我建议添加Wextra
警告标志。