除了-Wall之外,其他人发现的警告有用吗?
http://gcc.gnu.org/onlinedocs/gcc-4.3.2/gcc/Warning-Options.html
为了真正符合标准,C中的所有函数(main除外)都必须有原型,即使它们只是在同一个翻译单元中定义之后才使用它们吗?
ANSI C标准化过程的目标之一是生成K&R C(第一个发布的标准)的超集,其中包含随后引入的许多非官方特征.但是,标准委员会还包括一些新功能,例如函数原型(借用C++编程语言)和更强大的预处理器.参数声明的语法也已更改,以反映C++样式.
这让我觉得有差异.但是,我没有看到K&R C和ANSI C之间的比较.是否有这样的文件?如果没有,主要区别是什么?
编辑:我相信K&R书的封面上写着"ANSI C".至少我相信我在家里的版本确实如此.那么也许没有什么区别了?
我从GCC收到这个警告:
警告:不能传递非POD类型的对象'class Something'到'...'; call将在运行时中止
它非常致命,特别是因为它叫做中止.为什么这不是错误?我想把它弄错,但是:
-Wno-invalid-offsetof看起来像隐藏它的标志,但它不#include <stdio.h>
int main()
{
int a = 4;
int b = 3;
addNumbers(a, b);
}
int addNumbers(int a, int b)
{
return a + b;
}
Run Code Online (Sandbox Code Playgroud)
为什么这不编译,我得到一条消息说隐含的函数声明addNumbers()?
作为初学者,我在Code :: Blocks上键入以下hello world程序 -
#include<stdio.h>
main()
{
printf("Hello world \n");
}
Run Code Online (Sandbox Code Playgroud)
现在,我点击"Build and Run",输出屏幕显示"Hello world".
但是,我正在阅读的这本书建议我删除程序的某些元素,以查看程序中发生的错误.
我做了2次更改.首先,我从程序中删除了\n.(这本书告诉我,如果没有\n,运行程序时会出错)但是,当我点击"Build and Run"时,输出屏幕会给出与它没有任何错误时相同的输出.
我做的第二个更改是从程序中删除#include.即使是现在,输出屏幕也会显示与没有错误时相同的输出.
为什么会这样?请告诉我如何解决这个问题?
我使用的编译器是GNU GCC编译器.
编辑:正如所建议的,我添加了-wall,-wextra,-pedantic.现在,当我点击"Build and Run"时,它说找不到-1-wall,-1-wextra和-1-pedantic,程序也没有运行.现在该怎么解决?
这是我的阶乘程序——它正在执行并给出正确的结果:
#include <stdio.h>
int main()
{
int n;
printf("enter the no=");
scanf("%d", &n);
fun(n);
printf("%d\n", fun(n));
return 0;
}
int fun(int n)
{
if(n == 0)
return 1;
else
return fun(n - 1) * n;
}
Run Code Online (Sandbox Code Playgroud)
这是我计算一个数的幂的程序——这给出了 0 而不是正确的结果,但几乎相同:
#include <stdio.h>
int main()
{
int m, n;
printf("enter the no=");
scanf("%d%d", &m, &n);
pow(m, n);
printf("%d\n", pow(m, n));
return 0;
}
int pow(int m, int n)
{
if(n == 0)
return 1;
else
return pow(m, n - 1) * m; …Run Code Online (Sandbox Code Playgroud) 切换到Ubuntu 13.10后,我需要为更新的内核编译设备驱动程序.调用make导致2个错误:
error: implicit declaration of function ‘kzalloc’
error: implicit declaration of function ‘kfree’
Run Code Online (Sandbox Code Playgroud)
相同的make命令在Ubuntu 13.04中运行正常,但现在失败了.我也检查过它的存在
KDIR := /lib/modules/$(shell uname -r)/build
Run Code Online (Sandbox Code Playgroud)
它在Makefile中使用,/lib/modules/3.11.0-18-generic/build在解析时变为.与此模块编译指南相比,Makefile似乎非常标准.
移动到13.10时我错过了什么?是否需要额外的包裹?build-essentials包已安装.在13.10中有其他变化吗?
在完整的错误消息的make是:
user1@pc:/etc/opt/elo-mt-usb/elo_mt_input_mod_src$ sudo make
make -C /lib/modules/3.11.0-18-generic/build SUBDIRS=/etc/opt/elo-mt-usb/elo_mt_input_mod_src modules
make[1]: Betrete Verzeichnis '/usr/src/linux-headers-3.11.0-18-generic'
CC [M] /etc/opt/elo-mt-usb/elo_mt_input_mod_src/elo_mt_input.o
/etc/opt/elo-mt-usb/elo_mt_input_mod_src/elo_mt_input.c: In function ‘elo_input_write’:
/etc/opt/elo-mt-usb/elo_mt_input_mod_src/elo_mt_input.c:79:2: error: implicit declaration of function ‘kzalloc’ [-Werror=implicit-function-declaration]
if(!(buffer = kzalloc(count, GFP_KERNEL)))
^
/etc/opt/elo-mt-usb/elo_mt_input_mod_src/elo_mt_input.c:79:14: warning: assignment makes pointer from integer without a cast [enabled by …Run Code Online (Sandbox Code Playgroud) 最近我学会了C中的隐式函数声明.主要观点很明确,但我对在这种情况下理解链接过程有些麻烦.
请考虑以下代码(文件ac):
#include <stdio.h>
int main() {
double someValue = f();
printf("%f\n", someValue);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如果我尝试编译它:
gcc -c a.c -std=c99
Run Code Online (Sandbox Code Playgroud)
我看到关于隐式声明功能的警告f().
如果我尝试编译和链接:
gcc a.c -std=c99
Run Code Online (Sandbox Code Playgroud)
我有一个未定义的引用错误.一切都很好.
然后我添加另一个文件(文件bc):
double f(double x) {
return x;
}
Run Code Online (Sandbox Code Playgroud)
并调用下一个命令:
gcc a.c b.c -std=c99
Run Code Online (Sandbox Code Playgroud)
令人惊讶的是,一切都成功连接 当然在./a.out调用后我看到了垃圾输出.
所以,我的问题是:如何将隐式声明函数的程序链接起来?在编译器/链接器的引擎下我的例子会发生什么?
我A在main.c文件中定义了函数.我创建了三个使用该函数A而不导入任何内容的库.代码有效,但我只有一个警告:implicit declaration of function 'A' [-Wimplicit-function-declaration].
该函数如何在单独的文件中定义的函数中A工作B而不导入它?当A除了函数之外的其他函数调用函数时,我怎么可能只有一个警告B?
场景:
在Netbeans IDE中创建的AC应用程序,包含以下两个文件:
some_function.c
#include <stdio.h>
int function_1(int a, int b){
printf("Entered Value is = %d & %d\n",a,b);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
newmain.c
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char** argv) {
//function_2(); //Error //function name not allowed
function_1();
function_1(1);
function_1(1,2);
return (EXIT_SUCCESS);
}
Run Code Online (Sandbox Code Playgroud)
当在C程序中学习头文件的需要时,我尝试了上述应用程序(原样).它被编译并给出如下输出
输入值= 4200800&102
输入值= 1和102
输入值= 1和2
问题1 :(我意识到,在开始阶段,理解链接器程序的过程很难,因此我问这个问题.)我的假设是正确的,当链接时,"链接器将检查函数名称而不是参数"在没有使用头文件的情况下?
关于头文件的使用,我遇到了这个链接,它说,我们可以使用#include包含C文件本身.所以,我在newmain.c文件中使用了以下行
#include "some_function.c"
Run Code Online (Sandbox Code Playgroud)
正如所料,它显示了以下错误
错误:函数'function_1()'
错误的参数太少:函数'function_1(1)'的参数太少
而且,我得到了以下(意外)错误:
some_function.c:8:`function_1'some_function.c:8的多重定义
:首先在这里定义
问题2:包含'c'文件本身时我做了什么错误,因为它给出了上述(意外)错误?
接收错误:尝试编译时"六"的冲突类型.
void main(){
const int k = 4;
six(&k);
}
float * six(const int *x)
{
float *p = malloc(sizeof(float));
*p = (float)*x;
return p;
}
Run Code Online (Sandbox Code Playgroud)