我有一个跨平台应用程序,在我的一些函数中,并没有使用传递给函数的所有值.因此我收到GCC的警告,告诉我有未使用的变量.
编码警告的最佳编码方式是什么?
围绕这个功能的#ifdef?
#ifdef _MSC_VER
void ProcessOps::sendToExternalApp(QString sAppName, QString sImagePath, qreal qrLeft, qreal qrTop, qreal qrWidth, qreal qrHeight)
#else
void ProcessOps::sendToExternalApp(QString sAppName, QString sImagePath, qreal /*qrLeft*/, qreal /*qrTop*/, qreal /*qrWidth*/, qreal /*qrHeight*/)
#endif
{
Run Code Online (Sandbox Code Playgroud)
这太丑了,但似乎是编译器喜欢的方式.
或者我在函数末尾为变量赋值为零?(我讨厌它,因为它改变了程序流程中的某些东西以使编译器警告静音).
有正确的方法吗?
例如:
Bool NullFunc(const struct timespec *when, const char *who)
{
return TRUE;
}
Run Code Online (Sandbox Code Playgroud)
在C++中,我能够对/*...*/参数进行评论.但当然不是在C中,它给了我错误error: parameter name omitted.
#include <stdio.h>
volatile int i;
int main()
{
int c;
for (i = 0; i < 3; i++)
{
c = i &&& i;
printf("%d\n", c);
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
上面编译的程序的输出gcc是
0
1
1
Run Code Online (Sandbox Code Playgroud)
使用-Wall或-Waddress选项,gcc发出警告:
warning: the address of ‘i’ will always evaluate as ‘true’ [-Waddress]
Run Code Online (Sandbox Code Playgroud)
如何c在上述计划中进行评估?
我想启用 - 字面上 - GCC 所有的警告.(你会觉得这很容易......)
你认为-Wall可能会有所作为,但不是!仍然需要-Wextra.
你认为-Wextra可能会有所作为,但不是!并非此处列出的所有警告(例如-Wshadow)都已启用.我仍然不知道这份清单是否全面.
我如何告诉海湾合作委员会启用(不是,不是,或者是!)所有警告?
编译几个二进制文件时,我收到了一些警告:
warning: incompatible implicit declaration of built-in function ‘strcpy’
warning: incompatible implicit declaration of built-in function ‘strlen’
warning: incompatible implicit declaration of built-in function ‘exit’
Run Code Online (Sandbox Code Playgroud)
为了解决这个问题,我补充道
#include <stdlib.h>
Run Code Online (Sandbox Code Playgroud)
在与此警告关联的C文件的顶部,除了使用以下标志进行编译之外:
CFLAGS = -fno-builtin-exit -fno-builtin-strcat -fno-builtin-strncat -fno-builtin-strcpy -fno-builtin-strlen -fno-builtin-calloc
Run Code Online (Sandbox Code Playgroud)
我正在使用GCC 4.1.2:
$ gcc --version
gcc (GCC) 4.1.2 20080704
Run Code Online (Sandbox Code Playgroud)
我该怎么做才能解决这些警告?
我有一个项目使用log4cxx,boost等库,其头文件生成大量(重复)警告.有没有办法抑制来自库包含的警告(即#include <some-header.h>)或包含某些路径?我希望像往常一样在项目代码中使用-Wall和/或-Wextra而不会隐藏相关信息.我目前在make输出上使用grep,但我想要更好的东西.
我正在尝试编译并运行以下程序而没有main()函数C.我使用以下命令编译了我的程序.
gcc -nostartfiles nomain.c
Run Code Online (Sandbox Code Playgroud)
编译器发出警告
/usr/bin/ld: warning: cannot find entry symbol _start; defaulting to 0000000000400340
Run Code Online (Sandbox Code Playgroud)
好的,没问题.然后,我已经运行了可执行文件(a.out),两个printf语句都成功打印,然后得到分段错误.
那么,我的问题是,为什么在成功执行print语句后出现分段错误?
我的代码:
#include <stdio.h>
void nomain()
{
printf("Hello World...\n");
printf("Successfully run without main...\n");
}
Run Code Online (Sandbox Code Playgroud)
输出:
Hello World...
Successfully run without main...
Segmentation fault (core dumped)
Run Code Online (Sandbox Code Playgroud)
注意:
这里,-nostartfilesgcc标志阻止编译器在链接时使用标准启动文件
当我第一次使用GCC 4.3编译我的C++代码时(在成功编译它之后没有4.1,4.0,3.4的-Wall -Wextra选项的警告)我突然遇到了一堆表单错误warning: type qualifiers ignored on function return type.
考虑temp.cpp:
class Something
{
public:
const int getConstThing() const {
return _cMyInt;
}
const int getNonconstThing() const {
return _myInt;
}
const int& getConstReference() const {
return _myInt;
}
int& getNonconstReference() {
return _myInt;
}
void setInt(const int newValue) {
_myInt = newValue;
}
Something() : _cMyInt( 3 ) {
_myInt = 2;
}
private:
const int _cMyInt;
int _myInt;
};
Run Code Online (Sandbox Code Playgroud)
跑步g++ temp.cpp …
在去年我使用的是nlohmann json库[1]并使用GCC 5.x在x86_64上进行交叉编译arm-linux-gnueabi-*,没有任何警告.当我将GCC更新为更新版本时,GCC会生成一些神秘的诊断笔记.例如,这是其中一个注释
In file included from /usr/arm-linux-gnueabi/include/c++/7/vector:69:0,
from include/json.hpp:58,
from src/write_hsi.cpp:23:
/usr/arm-linux-gnueabi/include/c++/7/bits/vector.tcc: In member function ‘void std::vector<_Tp, _Alloc>::_M_realloc_insert(std::vector<_Tp, _Alloc>::iterator, _Args&& ...) [with _Args = {nlohmann::basic_json<std::map, std::vector, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, bool, long long int, long long unsigned int, double, std::allocator, nlohmann::adl_serializer>}; _Tp = nlohmann::basic_json<>; _Alloc = std::allocator<nlohmann::basic_json<> >]’:
/usr/arm-linux-gnueabi/include/c++/7/bits/vector.tcc:394:7: note: parameter passing for argument of type ‘std::vector<nlohmann::basic_json<>, std::allocator<nlohmann::basic_json<> > >::iterator {aka __gnu_cxx::__normal_iterator<nlohmann::basic_json<>*, std::vector<nlohmann::basic_json<>, std::allocator<nlohmann::basic_json<> > > >}’ changed in GCC 7.1
vector<_Tp, _Alloc>::
^~~~~~~~~~~~~~~~~~~ …Run Code Online (Sandbox Code Playgroud) 以下代码(包含恶意错误)在没有任何警告的情况下编译GCC.但是,当然,它不像开发人员(我)那样工作.
#include <iostream>
struct A
{
bool b;
void set(bool b_) { this->b = b_; }
bool get() const { return this-b; } // The bug is here: '-' instead of '->'
};
int main()
{
A a;
a.set(true);
std::cout << a.get() << std::endl; // Print 1
a.set(false);
std::cout << a.get() << std::endl; // Print 1 too...
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我可以为编译器添加哪个警告(GCC 4.8)以避免这种错误?
链接问题:是否有任何强制(或警告)访问成员变量/函数的选项this->?