我试图通过使用GCC的-Wunused-function标志在我的代码库中找到未使用的函数.
正如我所料,编译下面的代码会gcc -Wall -Wunused-function main.cpp打印一个unused variable警告:
warning: unused variable ‘x’ [-Wunused-variable]
Run Code Online (Sandbox Code Playgroud)
但是,编译器不会发出unused-function警告.
我需要做些什么才能使GCC注意到未使用的功能foo()?
// main.cpp
void foo(){ } //should (but doesn't) trigger 'unused function' warning
int main (int argc, char **argv){
int x; //correctly triggers 'unused variable' warning
return 0;
}
Run Code Online (Sandbox Code Playgroud)
请记住,我确实想要未使用的功能警告.这不是 "我怎么摆脱警告"的问题.
我正在研究全局变量的行为.
到目前为止,我认为全局变量的多重定义是一种非法方式,必须得到一个错误.但是我从Borland C/C++编译器得到了意想不到的结果,而GCC给了我预期的结果.
test1.c:
#include<stdio.h>
void func(void);
int num=1;
void main(){
func();
return;
}
Run Code Online (Sandbox Code Playgroud)
test2.c:
#include<stdio.h>
int num=2;
void func(){
printf("%d",num);
return;
}
Run Code Online (Sandbox Code Playgroud)
Borland C/C++:
c:\test>bcc32 test1.c test2.c
Run Code Online (Sandbox Code Playgroud)GCC:
c:\test>gcc test1.c test2.c
Run Code Online (Sandbox Code Playgroud)没有错误,编译和链接成功(对我来说这是意料之外的.)执行后test1.exe,控制台上打印出2.这是num定义的价值test2.c.
GCC给了我一个多重定义的错误num.当然,a.exe没有制作.(这是我所期待的)
为什么会这样?请告诉我.谢谢!
在hello.cpp文件中,我有这个静态函数。
static void hello()
{
std::cout << "hello" << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
我想从world.h文件中的其他静态函数调用这个函数,如下所示。
static void world()
{
hello();
std::cout << "world" << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,最推荐的公开hello()其他文件的方法是什么?
对于课堂,我们正在制作一个分析和经验计算T(n)的程序.我们的函数应该在一个单独的类f中,我们应该使用一个函数来读取文件中的输入以用作"n"并调用函数来打印值.当我尝试将分析函数作为我的print函数的参数调用时,我收到此错误:
p03.cpp:61:23: error: expected primary-expression before â.â token
p03.cpp:61:34: error: expected primary-expression before â.â token
Run Code Online (Sandbox Code Playgroud)
我确信这是一个愚蠢的错字,但我找不到它.是的,我在p03.cpp和F03.cpp中包含了F03.h.以下是导致错误的代码:
void analysis(istream& i) {
//Code Fragment 0 analysis
PrintHead(cout, "Code Fragment 0");
for(;;) {
int n;
i >> n;
if (i.eof()) break;
//Next line is line 61
PrintLine(cout, n, f.af00(n), f.ef00(n));
}
}
Run Code Online (Sandbox Code Playgroud)
以下是p03.cpp中的打印功能:
void PrintHead(ostream& o, string title) {
o << title << endl;
o << setw(20) << "n" << setw(20) << "Analytical" << setw(20) << "Empirical";
o << endl;
}
void PrintLine(ostream& …Run Code Online (Sandbox Code Playgroud) 可能重复:
什么是"静态"功能?
我在全局命名空间中看到了一个声明如下的函数:
static int function_name(int a, double* p, int c, float * u)
{
//do something with these arguments
}
Run Code Online (Sandbox Code Playgroud)
static关键字在这里意味着什么?
编辑:现在,当我知道什么是静态时,请解释一下什么优势使得函数的限制只在声明它的文件中可见?我的意思是为什么我应该限制我的功能可见性,它给我什么?
获取LNK2005"已在GUI.obj中定义"以获取PAL.h中的函数指针
//GUI.cpp
#include "PAL.h"
//PAL.h
#define PAL_INCLUDE
int (*addPAL)( int, void(*)(), void(*)() );
//main.cpp
#include "GUI.h"
#ifndef PAL_INCLUDE
#include "PAL.h"
#endif
Run Code Online (Sandbox Code Playgroud)
我误解了包含的本质#ifndef吗?
我有一个头文件,我想包含在另一个cpp文件中.我想知道如果我写这样的头文件有什么区别,
#include <iostream>
#include <string>
using namespace std;
string ret()
{
return "called";
}
Run Code Online (Sandbox Code Playgroud)
===================================
#include <iostream>
#include <string>
using namespace std;
static string ret()
{
return "called";
}
Run Code Online (Sandbox Code Playgroud)
ret()无论如何我都可以访问这个功能!那么,静态的用途是什么?
我正在看一些C语言的嵌入式固件.我注意到.c文件中使用了静态函数,但没有函数原型.始终将函数原型放在.c文件的顶部附近是一个好习惯吗?是否存在不放功能原型会更好的情况?
我正在此线程上阅读有关C中的静态函数的信息:https : //stackoverflow.com/a/558201/7997108
据我了解,基本上,您在其中定义静态函数的地方是可以调用它的唯一“位置” /文件(即fileA.c即文件),这一定会使该函数对那个.c或.h文件(或翻译单位)。但是,如果您将此文件#include到其他文件(fileB.c)中,您仍然可以在那里使用它?
因此,我试图了解在哪种情况下您希望函数对自己的.c保持静态,以及如果仅通过在其定义的文件中仍然可以使用该“私有” /静态函数,该函数的意义如何。
另外,据我了解,如果您不包括定义了某些功能的其他文件,则无论如何您将无法使用/调用该功能,对吗?
换句话说,我无法理解静态函数的典型用例是什么,以及它与非静态函数的基本区别。
我正在尝试将库链接到我的c程序,以便我的主程序可以使用我正在创建的库中的函数,但是我收到错误.
这是我的一些库代码(请调用此文件lib.c):
#include <bfd.h>
#include <stdio.h>
static void doDump ( bfd *abfd )
{
printf (_("\n%s: file format %s\n"), bfd_get_filename (abfd), abfd->xvec->name);
doHeaders ( abfd );
}
Run Code Online (Sandbox Code Playgroud)
这是我的主程序(请将此文件称为main.c):
#include "bfd.h"
static void getFile ( char *filename, char *target )
{
bfd *file;
file = bfd_openr (filename, target);
doDump (file);
bfd_close (file);
}
int main (int argc, char **argv)
{
char *target = NULL;
bfd_init ();
getFile ("a.out", target);
}
Run Code Online (Sandbox Code Playgroud)
这些是我用来链接库的命令:
cc -Wall -c lib.c
ar -cvq libdata.a lib.o
cc -o …
c++ ×6
c ×5
static ×4
gcc ×3
function ×2
include ×2
borland-c++ ×1
class ×1
non-static ×1
oop ×1
private ×1
return-value ×1