我必须在代码中定义一个大型数组(查找表).它包含256个元素,占用近1个电脑屏幕.
有两个函数正在使用此数组.我想在函数下定义这个数组,所以我可以在开发过程中非常快速地访问它们.
但是如果我尝试在一个文件中执行它,编译器将在函数周围给出"未声明的标识符"错误 - 因为它们使用数组.
所以,我必须将函数和数组放在单独的文件中.
这是我的程序的结构:
main.cpp:
#include "lookup.h"
...uses two functions...
Run Code Online (Sandbox Code Playgroud)
-
lookup.h:
#ifndef SubMaster_lookup_h
#define SubMaster_lookup_h
void func1(void);
void func2(void);
char LookupTable[][3]={ "00", "01", "02" "03", "04", "05", "06", "07", "08", "09",
"0a", "0b", "0c", "0d", "0e", "0f", "00", "01", "02" "03", "04", "05", "06", "07",
"08", "09", "0a", "0b", "0c", "0d", "0e", "0f", "10", "11", "12", "13", and so on...}
Run Code Online (Sandbox Code Playgroud)
-
lookup.cpp:
#include "lookup.h"
void func1() {
...body of func1...
}
void func2() {
...body of …Run Code Online (Sandbox Code Playgroud) pthread_create的第一个参数是pthread_t指针.在下面的hello程序中,如果第一个参数是指向pthread_t(pthread_t*)而不是pthread_t()的指针,pthread_t则程序结束于Segmentation fault...... 为什么?
我不记得看到pthread_t*作为第一个参数的声明类型pthread_create.
Butenhof的书" 用POSIX线程编程 "的第2章说:
要创建线程,必须声明类型为
pthread_t[notpthread_t*] 的变量.
但根据规范,第一个参数pthread_create是指向pthread_t,所以为什么分段错误?
pthread_t* thr;
pthread_create(thr, NULL, &hello, NULL);
Run Code Online (Sandbox Code Playgroud)
pthread_t thr;
pthread_t* pntr = &thr;
pthread_create(pntr, NULL, &hello, NULL);
Run Code Online (Sandbox Code Playgroud)
#include <pthread.h>
#include <stdio.h>
void *
hello(void *arg){
printf("Hello\n");
pthread_exit(NULL);
}
int
main(int argc, char **argv){
pthread_t thr = 1;
pthread_create(&thr, NULL, &hello, NULL);
pthread_join(thr, NULL);
return 0;
} …Run Code Online (Sandbox Code Playgroud) 我检查了SO的重复,但无法找到我的问题的确切解决方案.
我有一个头文件NvCommon.h,我使用枚举NV_DATA_TYPE.这个枚举在另一个标题NvDefs.h中定义,我在其中使用了许多结构和枚举NvCommon.h.由于循环依赖,我无法构建它.我知道转发声明枚举是不可能的.
在这种情况下可以做些什么?这是我设计的问题吗?我是否必须引入另一个头文件来解决这个问题?
我不是C专家.请帮我.我的设计可能有问题,我知道可以通过引入另一个头文件来修复此循环依赖.我想知道的是"这是唯一的方法".寻找备用解决方案(如果有).
如果它有用,我会发布完整的代码.
我试图了解在多个文件(编译单元)之间共享 C 全局变量的方式。我读过优秀的问题和答案在这里。然而,在做了一些测试之后,我仍然有一些我没有得到的东西:
基本上我的问题是:如果在没有extern关键字的头文件中声明(但未定义)一个变量,是否可以简单地将该头文件包含在各种编译单元中,以便使该变量可用于所有这些编译单元?在这种情况下,这意味着一个(并且只有一个)编译单元包含用于初始化(定义?)该变量的代码,并且在其他编译单元尝试对该变量执行任何操作之前,它将首先被调用。如果这一切都是真的,那么这个过程是否称为“隐式外部”?
我将用一个例子来说明我的问题:
标题“MyCommonHeader.h”包含:
//MyCommonHeader.h
int* i; //pointer to an int
Run Code Online (Sandbox Code Playgroud)
文件 MyFirstHeader.h 包含:
//MyFirstHeader.h
void changeIt(int newValue);
Run Code Online (Sandbox Code Playgroud)
文件 MyFirstSource.c 包含:
//MyFirstSource.c
#include "MyFirstHeader.h"
void changeIt(int newValue) {
*i = newValue;
}
Run Code Online (Sandbox Code Playgroud)
文件 MySecondSource.c 包含:
//MySecondSource.c
#include "MyCommonHeader.h"
#include "MyFirstHeader.h"
void main() {
i = malloc(sizeof(int));
changeIt(10);
*i = 23;
}
Run Code Online (Sandbox Code Playgroud)
上面的代码是否到处都使用相同的 i 变量?我需要在extern任何地方添加吗?
我想知道子类是否可以从main.cpp文件中访问变量.例如:
Main.ccp
int x = 10;
int main()
{
return 0;
}
Run Code Online (Sandbox Code Playgroud)
示例类的cpp
Subclass::Subclass ()
{
x = 5;
}
Run Code Online (Sandbox Code Playgroud)
错误:
error: 'x' was not declared in this scope
Run Code Online (Sandbox Code Playgroud)
我是编码的新手,我想知道这是否可行,如果没有,我怎么能这样做?
我有一个数组,其大小在源文件中定义的编译时确定。
const int array[] = {1, 3, 3, 7};
Run Code Online (Sandbox Code Playgroud)
元素的数量将来可能会发生变化,因此我宁愿不在括号中对其进行硬编码。
这个数组需要从几个源文件中访问,所以我试图在头文件中添加一个 extern 声明。但是,由于隐式数组大小,我不确定这是否可行。我尝试了两种变体:
extern const int array[]; // Warning: size of symbol `array' changed from 8 to 16
extern const int *array; // error: conflicting types for 'array'
Run Code Online (Sandbox Code Playgroud)
是否可以这样做,或者我应该寻找解决方法?
我正在查看为STM32F微控制器编写的一些代码,我在初始化变量之前发现了这些关键字.我想知道使用这个" __IO "和" 静态 "关键字有什么意义?
代码行是这样给出的:
static __IO uint32_t sysTickCounter;
Run Code Online (Sandbox Code Playgroud) 为什么我应该extern在以下代码中使用关键字:
header.h
float kFloat; // some say I should write 'extern float kFloat;', but why?
Run Code Online (Sandbox Code Playgroud)
file.c
#include <stdio.h>
#include "Header.h"
float kFloat = 11.0f;
Run Code Online (Sandbox Code Playgroud)
main.c中
#include <stdio.h>
#include "Header.h"
int main(int argc, const char * argv[])
{
printf("The global var is %.1f\n", kFloat);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这段代码有效.全局变量kFloat默认为外部链接和静态生命周期.
输出是:
全局变量是11.0
我不明白在哪种情况下会出现问题,有人能举例说明它会崩溃吗?
这不是修复或其他问题.只想知道行为
#include <stdio.h>
extern int var;
int main()
{
var = 10;
printf("%d ", var);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在这里我们可以看到,var定义为函数extern内部和定义.但是当输出时它出错了main()varvar
未定义的引用
var.
那么价值在10哪里?它会去extern var或存储在垃圾内存位置吗?
那么在编译行var =10和下一行时到底发生了什么.
编译日志:
Compilation error time: 0 memory: 2156 signal:0
/home/PpnviQ/ccRtZapf.o: In function `main':
prog.c:(.text.startup+0x13): undefined reference to `var'
collect2: error: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud) 我知道在头文件中使用包含警戒是为了防止某些内容被定义两次.但是,使用此代码示例,完全没问题:
foo.c的
#include <stdio.h>
#include <string.h>
#include "bar.h"
int main() {
printf("%d", strlen("Test String"));
somefunc("Some test string...");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
bar.h
#ifndef BAR_H_INCLUDED
#define BAR_H_INCLUDED
void somefunc(char str[]);
#endif
Run Code Online (Sandbox Code Playgroud)
bar.c
#include <stdio.h>
#include <string.h>
#include "bar.h"
void somefunc(char str[]) {
printf("Some string length function: %d", strlen(str));
}
Run Code Online (Sandbox Code Playgroud)
上面的代码片段是用编译的,gcc -Wall foo.c bar.c -o foo没有错误.然而,无论是<stdio.h>和<string.h>被列入没有包括后卫.将bar.h删除到单个语句时仍然没有错误void somefunc(char str[]);.为什么没有错误?