我需要围绕标准系统调用的一组包装器 - 打开,监听等.对于这些我有一些"#define",如:
#define open(a,b,c) JCL_Open(a,b,c)
Run Code Online (Sandbox Code Playgroud)
但是当我编译头文件和相关的.c文件时,我收到以下警告:
/jcl_wrappers.h:114:1:警告:"打开"重新定义
/jcl_wrappers.h:113:1:警告:这是前一个定义的位置
我怎么能抑制这些警告?
我是C的新手,所以我进入man stdlib.h,搜索"随机",看到它random()返回了long,因为它只是一个学习练习,以为我只是转换为int(而不是费心去查找一个返回int- rand()是一个).
无论如何,程序编译并正确运行.但:
$ gcc -std=c99 part1.c
part1.c: In function 'main':
part1.c:44:5: warning: implicit declaration of function 'random'
Run Code Online (Sandbox Code Playgroud)
如果我移除-std=99旗帜就会消失.这是"违规"代码:
int test6[1000];
for(i = 0; i < 1000; i++)
{
test6[i] = (int) random();
printf("it is %d\n", test6[i]); //check random() is working (it isn't seeded)
}
printf("%d\n", largest(test6, 1000));
Run Code Online (Sandbox Code Playgroud)
所以我只是想知道是否有人知道这是为什么,因为我认为这可能很有趣.
我在 C 中遇到严格别名问题。我使用的是 GCC 4.7.1。
示例 1:
当使用 -fstrict-aliasing -Wstrict-aliasing=3 编译此代码时,我收到“警告:取消引用类型双关指针将破坏严格别名规则”
#include <stdio.h>
#include <stdint.h>
int main(void)
{
uint8_t a[4] = {0x01, 0x23, 0x45, 0x67};
uint32_t b;
b = *(uint32_t *)a;
printf("%x\n", b);
return(0);
}
Run Code Online (Sandbox Code Playgroud)
示例 2:
此代码使用 -fstrict-aliasing 和 -Wstrict-aliasing=3 或 -Wstrict-aliasing=2 或 -Wstrict-aliasing=1 时不发出警告
#include <stdio.h>
#include <stdint.h>
int main(void)
{
uint8_t a[4] = {0x01, 0x23, 0x45, 0x67};
uint32_t b;
void *p;
p = a;
b = *(uint32_t *)p;
printf("%x\n", b);
return(0);
}
Run Code Online (Sandbox Code Playgroud)
两个例子都工作正常。
使用 union 也是未定义的行为,并且在我的情况下使用 memcpy() …
我遇到了类似这个问题.
int j = 6;
int *k = new int[j]{4};
Run Code Online (Sandbox Code Playgroud)
警告是:
warning: non-constant array size in new, unable to verify length
of initializer-list [enabled by default]
Run Code Online (Sandbox Code Playgroud)
我只面对一个警告,没有错误,我用-std = gnu ++ 11运行
另外,我想要为每个实例调用构造函数.如果我打印数组值,全部
为什么在gcc中编译以下代码不会产生任何类型不匹配警告?-1是int类型,并f()期望类型为char:
void f(char c) {}
int main(void)
{
f(-1);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
即使我们明确指定了类型,也没有警告:
void f(unsigned char c) {}
int main(void)
{
f((signed int)-1);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
什么是好奇:如果我们指定超出范围的值,则打印警告:
void f(char c) {}
int main(void)
{
f(65535);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
warning: overflow in implicit constant conversion
gcc版本6.1.1
我是C ++的新手。
我想忽略警告-Wunused-result,我猜是由于-Wall标志而弹出的。
我确实在网上搜索,发现这是我可以通过声明一个来忽略它pragma。我没有太多的知识,pragma但是它说我必须写warning number才能忽略它。
什么是warning number的-Wunused-result,或者是有任何其他方式,我可以忽略或禁用此特定的警告?
码:-
freopen("input", "r", stdin);
freopen("output", "a", stdout);
Run Code Online (Sandbox Code Playgroud)
关于编译:
warning: ignoring return value of ‘FILE* freopen(const char*, const char*, FILE*)’, declared with attribute warn_unused_result [-Wunused-result]
Run Code Online (Sandbox Code Playgroud)
我发现我需要声明类似
#pragma warning( disable : number_of_warning )
以下程序使用 gcc 8.2.1 生成此内容:
警告:类型限定符在转换结果类型 [-Wignored-qualifiers] 上被忽略 int * const ptrCast = const_cast(ptr);
int main() {
int i = 0;
const int * const ptr = &i;
int * const ptrCast = const_cast<int * const>(ptr);
return *ptrCast;
}
Run Code Online (Sandbox Code Playgroud)
编译为:gcc -Wignored-qualifiers test.cc
根据我对 const_cast 的理解,这不应该发出警告。任何人都可以验证这一点吗?
我有一个使用enumas 参数的 C 函数,如下例所示:
typedef enum
{
AB,
CD
} A;
void f(A input)
{
// do something
}
int main(void)
{
// do something
f(-10);
// do something
}
Run Code Online (Sandbox Code Playgroud)
是否有一个警告,我可以启用分配值超出枚举范围的枚举变量?
考虑这个 C 代码:
void foo(char *);
void bar(void) {
foo("");
}
Run Code Online (Sandbox Code Playgroud)
-pedantic -Wall -Wextra当我使用 GCC 或 Clang 或Clang编译该文件时-Weverything,它会编译而不会给出任何相关警告。如果我添加-Wwrite-strings,那么 GCC 会给我这个:
<source>:4:9: warning: passing argument 1 of 'foo' discards 'const' qualifier from pointer target type [-Wdiscarded-qualifiers]
4 | foo("");
| ^~
<source>:1:10: note: expected 'char *' but argument is of type 'const char *'
1 | void foo(char *);
| ^~~~~~
Run Code Online (Sandbox Code Playgroud)
clang 给了我这个:
<source>:4:9: warning: passing 'const char [1]' to parameter of type 'char *' discards …Run Code Online (Sandbox Code Playgroud) 最近,我阅读了一个编码示例,其中解释了以下内容会导致 Visual Studio 中的编译器错误:
int* pointer1;
*pointer1 = 10;
Run Code Online (Sandbox Code Playgroud)
作者声称由于pointer1在解引用之前未初始化,所以在Visual Studio中出现如下错误:
C4700:使用了未初始化的局部变量“pointer1”
确实如此。这个场景是有道理的。
但是,如果我使用完全相同的代码并使用 g++ 进行编译,则不会发生编译器错误并且我可以pointer1以任何正常方式自由使用。
为什么会有差异?g++ 是否pointer1在初始化之前分配了地址?