头文件命名冲突,判断整数是否为2的幂次方,GDB多线程调试,makefile文件编写

1.在c++中,如果自己写的头文件(.h)与源程序中的代码的变量发生命名冲突,如何解决?

1).命名空间

2).extern

#ifndef     _GLOBLE_H     

  #define     _GLOBLE_H     

  extern   int     a;     

  extern   int     b;     

  extern   int     c;     

  #endif     

  然后在其他的某个.cpp文件中定义

  int     a;     

  int     b;     

  int     c;    

但是这种写法在具有大量的变量时候,容易造成声明的头文件和变量定义的cpp文件中变量的不一致。

一般情况下头文件中只放变量的声明,因为头文件要被其他文件包含(即#include),如果把定义放到头文件的话,就不能避免多次定义变量,C++不允许多次定义变量,一个程序中对指定变量的定义只有一次,声明可以无数次。

不过有三个例外,一下三中实体的定义也可放到头文件中。

1.值在编译时就已知的const 变量的定义可以放到头文件中

如:const int num(10);

2.类的定义可以放到头文件中

3.inline 函数

这三个实体可以定义在多个源文件中,只要在每个源文件中的定义相同。

原文链接:https://blog.csdn.net/skk18739788475/article/details/79643978

2.判断某个数是否为2的幂次方?(编程实现)

#include"stdafx.h"

#include <iostream>

#include <vector>

#include <numeric>

#include <limits>

#include<algorithm>

#include<map>

#include<cstring>

#include<math.h>

#include<string>

#include<iomanip>

#include <assert.h>

using namespace std;

bool fun(int v)

{

bool flag = 0;

if ((v>0) && (v&(v - 1)) == 0)

flag = 1;

return flag;

}

int main(void)

{

int a;

printf("请输入1个32位的整数:");

scanf_s("%d", &a);

if (fun(a))

printf("这个数是2次方幂\n");

else

printf("这个数不是2次方幂\n");

system("pause");

return 0;

}

https://blog.csdn.net/lwj103862095/article/details/12443915

3.Linux中,如何编写makefile文件(静态链接和动态链接)

Linux下Makefile中动态链接库和静态链接库的生成与调用

https://blog.csdn.net/u011964923/article/details/73297443

4.如何使用gdb调试线程程序,使用的命令有哪些?(在程序发生问题是,每个程序都有对应的pid ,然后根据程序的线程pid进行程序的调试)

使用gdb调试多进程,多线程。

gdb调试多进程与多线程

https://blog.csdn.net/snow_5288/article/details/72982594

gdb常用命令及使用gdb调试多进程多线程程序

https://blog.csdn.net/chenyulancn/article/details/79988410

gdb常用调试命令以及多线程堆栈的查看

https://www.cnblogs.com/lidabo/p/5629830.html

发布了81 篇原创文章 · 获赞 17 · 访问量 6046

猜你喜欢

转载自blog.csdn.net/hopegrace/article/details/102679778