使g ++指出此错误/警告

时间:2014-11-02 06:23:08

标签: c++ makefile g++

我目前有这样的声明

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

我没有收到警告/错误,当这个方法执行时,输出开始变得疯狂 关于我能做什么的任何建议,我可以得到像这些愚蠢的错误警告

1 个答案:

答案 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警告标志。