Tag: gcc

gcc / g ++可以告诉我何时忽略了我的注册?

使用gcc / g ++编译C / C ++代码时,如果它忽略了我的寄存器,它可以告诉我吗? 例如,在此代码中 int main() { register int j; int k; for(k = 0; k < 1000; k++) for(j = 0; j < 32000; j++) ; return 0; } j将用作寄存器,但在此代码中 int main() { register int j; int k; for(k = 0; k < 1000; k++) for(j = 0; j < […]

通过GCC -fstack-check选项在C中引发了什么exception

根据gcc文档 -fstack-check 生成代码以validation您是否超出了堆栈的边界。 请注意,此开关实际上不会导致检查完成; 操作系统必须这样做。 该开关导致生成代码以确保操作系统看到堆栈被扩展。 我的假设是这个额外的代码会产生exception,让操作系统知道。 使用C语言时,我需要知道额外代码生成了什么exception。 谷歌也没多大帮助。 关闭我知道它是在Ada语言的情况下生成Storage_Errorexception( 参考 )。 我正在开发一种小型操作系统/调度程序,我需要捕获此exception。 我正在使用C / C ++。 我的GCC版本3.4.4

仅评估一次宏参数

在下面的代码中,任何传递为retval被评估为每次使用该令牌时给出的内容。 #define _CPFS_RETURN(commit, retval) do { \ util_cpfs_exit(commit); \ return retval; \ } while (false) #define CPFS_RETURN_BOOL(retval) do { \ _CPFS_RETURN(retval, retval); \ } while (false) 例如,给定使用CPFS_RETURN_BOOL(inode && file_truncate(inode, len)); ,这是生成的: do { do { util_cpfs_exit(inode && file_truncate(inode, len)); return inode && file_truncate(inode, len); } while (0); } while (0); 显然我不想执行语句inode && file_truncate(inode, len); 不止一次。 […]

gcc – 属性nothrow用于什么?

我正在浏览一些gcc属性列表,我发现这个引起我注意的那个: nothrow The nothrow attribute is used to inform the compiler that a function cannot throw an exception. For example, most functions in the standard C library can be guaranteed not to throw an exception with the notable exceptions of qsort and bsearch that take function pointer arguments. The nothrow attribute is not implemented in […]

我怎样才能理解我的库的标准是什么以及gcc在我的计算机中编译的标准是什么?

这来自我的终端: Korays-MacBook-Pro:~ koraytugay$ gcc –version Configured with: –prefix=/Library/Developer/CommandLineTools/usr –with-gxx-include-dir=/usr/include/c++/4.2.1 Apple LLVM version 6.1.0 (clang-602.0.49) (based on LLVM 3.6.0svn) Target: x86_64-apple-darwin14.3.0 Thread model: posix 据我所知,有三种C标准,ANSI C,C99和C11。 我如何知道我拥有哪个库以及我的编译器支持哪些库?

当给定`const char *`作为第一个参数时,C标准库函数`strchr`如何返回指向非const的指针?

使用gcc / g ++编译给定代码示例成功。 strchr调用没有错误,这显然将const char *赋予char * 。 我发现strchr在两个不同的源pubs.opengroup.org和cplusplus.com上声明为char * strchr(const char *, int) 如果strchr被实现为抛弃strchr ,那为什么会这样呢? 如果目标是提供对char *和const char *字符串都有效的函数 – 它可以使用两个不同的函数名实现。 你能否对此作出更全面的解释? #include int main () { const char *str = “Sample string”; char * ptr; //ptr = str; // Error: discards const qualifier – both on gcc and g++ pch = strchr(str,’S’); […]

“未定义的符号首先在文件中引用”链接错误

我正在用C语言编写我的第一个程序; 我已经设法解决了大部分语法错误,但是当gcc尝试将目标文件链接在一起时,我遇到了一个奇怪的错误。 它打印完全如下: gcc -o proj04.support.o proj04.driver.o Undefined first referenced symbol in file convert proj04.driver.o 我环顾四周寻找一些答案,但没有一个对我有意义。 我将发布我正在使用的文件来制作下面的程序,如果你有答案,我会非常感谢你的帮助。 这似乎是一个非常基本的错误,所以它可能是我没有做的傻事。 Makefile (首先发布,因为我怀疑问题在这里) # Comments # Comments proj04: proj04.support.o proj04.driver.o gcc -o proj04.support.o proj04.driver.o proj04.support.o: proj04.support.c gcc -Wall -c proj04.support.c proj04.driver.o: proj04.driver.c gcc -Wall -c proj04.driver.c 头文件 (由教授提供,不可更改,一行长): int convert( int, unsigned, char[], int ) 实施文件 #include #include […]

为什么没有lpthread标志的gcc链接?

我正在做一个互动项目,其中互斥体表现得很神秘。 我把它归结为这个应该明显陷入僵局的测试用例。 #include #include int main() { pthread_mutex_t test; pthread_mutex_init(&test, NULL); pthread_mutex_lock(&test); pthread_mutex_lock(&test); printf(“Took lock twice\n”); return 0; } 但是,当我在没有-lpthread标志的情况下编译时,不仅程序仍然可以编译和链接,它也可以在没有死锁的情况下运行。 为什么? gcc pthread_break.c -o pthread_test ./pthread_test Took lock twice 使用-lpthread标志进行编译会产生预期结果: gcc pthread_break.c -o pthread_test -lpthread ./pthread_test <- deadlocked here 我正在运行GCC 7.2.0版。

是否存在使用g ++ 无法正确编译有效C代码的情况

可能重复: “C ++的C子集” – >哪里没有? 例子? 我知道C是C ++的一个子集(即不存在有效的C代码,它不是有效的C ++代码)。 我的问题是g++是否与所有C代码完全兼容。 例如,将 g++ -o testing test.c 产生相同的二进制文件 gcc -o testing test.c 在所有情况下? 更具体地说,如果他们并不总是创建相同的二进制文件,那么有什么理由可能会出现问题吗? 如果我不确定代码,总是使用g++是否安全?

C ++ while循环优化不能正常工作

我有这段代码: #include int main(int argc, const char** argv) { int a = argv[0][0]; int b = argv[0][1]; while ((a >= 0) && (a < b)) { printf("a = %d\n", a); a++; } return 0; } 我用gcc-4.5 -02 -Wstrict-overflow=5编译它。 编译器向我大吼大叫warning: assuming signed overflow does not occur when changing X +- C1 cmp C2 to X cmp […]