我在编译 gcc 时遇到问题,可以追溯到 Catalina 存在 xcode 问题,因此我在 OS X mojave 上安装 Haskell 时参考了此处的链接“macOS_SDK_headers_for_macOS_10.14.pkg 与此版本的 macOS 不兼容”并尝试解决问题。但是,虽然我重新安装了xcode,但我无法打开macOS_SDK_headers_for_macOS_10.14.pkg,因为该文件不在/Library/Developer/CommandLineTools/Packages/中。我的文件看起来像;
MacBook-Pro:CommandLineTools myname$ ls
Library SDKs usr
Run Code Online (Sandbox Code Playgroud)
我在哪里可以找到该文件?我应该手动将 pkg 添加到文件夹中吗?
对于我所知道的所有 C 编译器,-Wall都会对隐式转换发出警告,但不会对任何显式转换发出警告。我一直在尝试找到一个标志(无论是 gcc、clang、任何 c 编译器都可以),当存在导致未定义或实现定义的行为的强制转换时,该标志将导致发出警告。这是一个示例,说明了我期望和不期望出现警告的位置:
#include <math.h>
struct person {
int tag;
char* name;
};
int foo(struct person* p) {
int* tagPtr = (int*)p; // this cast is fine, cast to first member ptr is allowed
double* badPtr = (double*)p; // this cast should cause warning
return ((*tagPtr) + (int)(round(*badPtr)));
}
Run Code Online (Sandbox Code Playgroud)
到 a 的转换int*是明确定义的,并且到double*没有明确定义,但 gcc 不会警告我有关第二次强制转换的信息。有没有办法让它这样做?或者其他编译器甚至 linter 是否提供此功能?
然后是“明确定义的”和“明确定义的实现”之间的区域,即“可以根据上下文进行明确的定义”。例如,如果 a最初是通过另一个方向的转换形成的(由 C 规范的 6.7.2.1p15 节暗示),则从aint*到 a 的转换struct person*是明确定义的。int*指针的出处很少可用,因此编译器或 …
我可以编译并运行一个程序,该程序将一个 long int 文字(尽管它适合 int)分配给 int 变量。
$ cat assign-long-to-int.c
#include <stdio.h>
int main(void){
int i = 1234L; //assign long to an int
printf("i: %d\n", i);
return 0;
}
$ gcc assign-long-to-int.c -o assign-long-to-int
$ ./assign-long-to-int
i: 1234
Run Code Online (Sandbox Code Playgroud)
我知道 1234 适合 int,但仍然希望能够启用警告。我已经浏览了所有gcc 选项,但找不到任何合适的。
是否可以针对这种情况生成警告?从这里的讨论和 gcc 选项来看,简短的答案是否定的。这是不可能的。
这样的警告还有意义吗?在我发布的简单示例中,很明显 1234L 被分配给 int 变量,并且它适合。但是,如果声明和赋值被多行代码分隔开怎么办?编写 1234L 的程序员表示他们希望将这个字面整数分配给 long。否则,附加 L 有何意义?
在某些情况下,附加 L 确实会产生影响。例如
$ cat sizeof-test.c
#include <stdio.h>
void main(void){
printf("%ld\n", sizeof(1234));
printf("%ld\n", sizeof(1234L));
}
$ ./sizeof-test
4
8
Run Code Online (Sandbox Code Playgroud)
尽管编译器必须知道 1234L 适合 …
我试图删除字符串的第一个字符并保留其余部分,我当前的代码没有编译,我很困惑如何解决它.
我的代码:
char * newStr (char * charBuffer)
{
int len = strlen(charBuffer);
int i = 1;
char v;
if(charBuffer[0] == 'A' || charBuffer[0] == 'Q'){
for(i=1;i<len;i++)
v = v + charBuffer[i];
}
v = v + '\0';
return v;
}
Run Code Online (Sandbox Code Playgroud)
Gcc:"警告:返回从没有强制转换的整数生成指针"
另外:"char*newStr(char*charBuffer)"需要保持不变.
我正在进行套接字编程..我的代码以我想要的方式执行,我可以使用它.但它给了我一个关于编译的警告.
我编译使用
gcc server1.c -o server1 -lpthread
Run Code Online (Sandbox Code Playgroud)
我得到了警告
warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
Run Code Online (Sandbox Code Playgroud)
以下代码出现此错误
int newsockfd;
newsockfd = (int)newsockfdd; //this line
Run Code Online (Sandbox Code Playgroud)
我在下面的代码块中使用了newsockfdd(这是int)
if (pthread_create(&threadID[i++], NULL, serverThread, (void *)(intptr_t)newsockfdd) != 0)
{
perror("Thread create error");
}
Run Code Online (Sandbox Code Playgroud)
你可能会说,代码编写得不是很好(我正在努力使其更好).我知道这个警告是因为与int的大小有关.但我真的不知道如何解决它.在我在pthread_create语句中输入(intptr_t)之前,它在该行上显示警告,但那时警告是
warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
Run Code Online (Sandbox Code Playgroud)
似乎应该有一个简单的解决方案吗?但我找不到它.我正在使用Ubuntu 64位.这是警告的原因吗?
我按照一些类似的帖子来修改代码,但是仍然会生成警告.
$ g++ ncfile.c -o ncfile -g -lnetcdf
ncfile.c: In function ‘int main(int, char**)’:
ncfile.c:363: warning: format ‘%d’ expects type ‘int’, but argument 4 has type ‘size_t’
ncfile.c:363: warning: format ‘%d’ expects type ‘int’, but argument 4 has type ‘size_t’
ncfile.c:364: warning: format ‘%d’ expects type ‘int’, but argument 4 has type ‘size_t’
ncfile.c:364: warning: format ‘%d’ expects type ‘int’, but argument 4 has type ‘size_t’
Run Code Online (Sandbox Code Playgroud)
在第363和364行的那个区块中:
for(i = 0; i < ndimsp; i++) {
char * temp_name;
size_t temp_len; …Run Code Online (Sandbox Code Playgroud) 我有这个void函数,带有指向我线程的指针.当我去编译时,我收到警告:"控制到达非空函数的结束".如果我做
void mythread (void *arg)
Run Code Online (Sandbox Code Playgroud)
和函数我将解决编译器给出的警告,但得到一个新的警告,说:
TA.c:50:2: warning: passing argument 3 of ‘pthread_create’ from incompatible pointer type [enabled by default]
Run Code Online (Sandbox Code Playgroud)
那么,我放在return 0;"mythread"函数的末尾,然后编译.但我不确定这是否是正确的做法?
建议吗?我是一名新手程序员,正在尝试学习POSIX系统服务编程.
所以我不知道该怎么做.我应该尝试输入演员参数3吗?我怎样才能编译我的程序,并获得零警告?
#include <limits.h>
#include <pthread.h>
#include <stdio.h>
void *mythread (void *arg)
{
printf("This my Thread Maybe...\n");
}
int main()
{
pthread_t mythread_id;
pthread_attr_t mythread_attr;
size_t stack_size;
int detachstate;
pthread_attr_init (&mythread_attr);
pthread_attr_getdetachstate(&mythread_attr, &detachstate);
if(detachstate == PTHREAD_CREATE_DETACHED)
{
printf("Current Deteached state is Detached\n");
}
else
{
printf("Current Detached state is Joinable\n");
}
pthread_attr_setdetachstate (&mythread_attr, PTHREAD_CREATE_DETACHED);
pthread_attr_getdetachstate (&mythread_attr, …Run Code Online (Sandbox Code Playgroud) 我尝试使用带有优化标志-O1/-O2/-O3/-Og的gcc-5.1.0来编译一大块软件.它给了我警告或者-Wmaybe-uninitialized或-Wuninitialized在运行时失败.调试后我找到了导致它的代码,但是我无法理解为什么.我减少了代码以重现失败:
#include <cstdlib>
#include <iostream>
template<class T>
struct foo {
template<class U>
char bar(const U &x) {
//return id(x)[0];
const T &y = id(x);
return y[0];
}
const T &id(const T &elem) {
return elem;
}
};
int main(void) {
foo<const char *> f;
char *str = "hello world";
//std::cout << f.bar((const char *)str) << std::endl;
std::cout << f.bar(str) << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
gcc-5.1.0给出以下警告:
g++ -Og -Wall -Wextra -Wno-write-strings test.cpp -o test
test.cpp: In function …Run Code Online (Sandbox Code Playgroud) 当我的输入大小足够大时,分段错误萌芽,在项目中使用可变长度数组.我想删除它们,如何让GCC显示它找到的可变长度数组的每个声明?
我已经尝试过使用-Wstack-usage=1000和过滤消息warning: stack usage might be unbounded,但这给了我功能,而不是声明.有没有更好的办法?
当我用GCC 4.9.2编译下面的程序时,我得到以下警告:从不兼容的指针类型传递'P'的参数1.但是,我没有看到该程序有任何问题.有线索吗?
typedef int Row[10];
void P(const Row A[])
{
}
int main(void)
{
Row A[10];
P(A);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
以下是GCC到stderr的完整输出:
test.c: In function ‘main’:
test.c:12:4: warning: passing argument 1 of ‘P’ from incompatible pointer type
P(A);
^
test.c:3:6: note: expected ‘const int (*)[10]’ but argument is of type ‘int (*)[10]’
void P(const Row A[])
^
Run Code Online (Sandbox Code Playgroud)
编辑:该程序与Clang 3.5.0和选项完全编译-pedantic -std=c89 -Wall.