以下extern说明符的示例用法如何表现.
我们在文件one.c和two.c中都有一个全局变量int x.我们想在three.c中使用它们所以在three.c中声明了这个变量as
extern int x;
编译和链接这些文件时会发生什么?
我的答案是:所有这些文件的编译应该成功,但是由于x的多个声明,链接器应该在链接时标记错误.C++中的行为会有什么不同吗?
这些是在C和C++中同时从两个文件引用int x(在three.c中)的任何方式.在C++中,我想我们可以使用命名空间来实现这一点.对?
我有以下错误
error : conflicting types for 'sprintf'
error : conflicting types for 'vsprintf'
error : conflicting types for 'vprintf'
error : conflicting types for 'select'
Run Code Online (Sandbox Code Playgroud)
在我的头文件中,代码是
extern char *sprintf(char*,const char*,... )
Run Code Online (Sandbox Code Playgroud)
实际上我包括 #include <stdio.h>
但对于我们写的太阳系
# ifndef HPUX
extern char *sprintf(char*,const char*,... )
Run Code Online (Sandbox Code Playgroud) 无法理解这里有什么问题:我有main.cpp文件,其中包括:
#include "lexan.h"
...
Run Code Online (Sandbox Code Playgroud)
lexan.h文件:
#ifndef _LEXAN_
#define _LEXAN_
enum Statements ...
//some function prototypes
...
struct TokensList {
Statements statement;
std::string value;
struct TokensList *next;
};
struct TokensList *tokens = NULL;
#endif _LEXAN_
Run Code Online (Sandbox Code Playgroud)
在lexan2.h中:
#include "lexan.h"
// and some function prototypes
Run Code Online (Sandbox Code Playgroud)
问题是我得到链接错误2005:
1>lexan2.obj : error LNK2005: "struct TokensList * tokens" (?tokens@@3PAUTokensList@@A) already defined in lexan.obj
1>main.obj : error LNK2005: "struct TokensList * tokens" (?tokens@@3PAUTokensList@@A) already defined in lexan.obj
Run Code Online (Sandbox Code Playgroud)
我的错误在哪里?我以为
#ifndef _LEXAN_
#define _LEXAN_
Run Code Online (Sandbox Code Playgroud)
在lexan.h文件中可以保护我免受这种链接问题的影响.
我有三个文件,一个主.cpp文件:
#include <stdio.h>
#include "myClass.h"
int main()
{
myClass mvar;
tryVar = 23; // why does this not work?
printf("%d ", mvar.readTryVar()); // This writes out 0, why??
return 0;
}
Run Code Online (Sandbox Code Playgroud)
一个myClass.cpp文件
#include "myClass.h"
myClass::myClass(void)
{
}
myClass::~myClass(void)
{
}
void myClass::setTryVar()
{
tryVar = 23334;
}
int myClass::readTryVar()
{
return tryVar;
}
Run Code Online (Sandbox Code Playgroud)
和myClass.h文件
#pragma once
static int tryVar;
class myClass
{
public:
myClass(void);
~myClass(void);
void setTryVar();
int readTryVar();
};
Run Code Online (Sandbox Code Playgroud)
它们是非常简单的文件,但我无法理解为什么静态变量不在main函数中设置,我需要通过myClass函数设置它.
我认为我不太清楚如何创建"翻译单元",我知道"include"指令只是在实际编译之前将头文件的内容复制到.cpp文件中..那么为什么不是'静态变量可见吗?
我在一个文件中定义了一个变量,并使用extern关键字在另一个文件中声明了它.但我已用不同的数据类型声明它..
file1.c
char i;
main()
{
foo();
}
Run Code Online (Sandbox Code Playgroud)
file2.c中
void foo()
{
extern int i;
i = 130;
printf("%d", i);
}
Run Code Online (Sandbox Code Playgroud)
在上面的程序中,内存在main函数中为'i'分配为char数据类型.执行后的答案应该是否定的(-127).但它打印130.无论在foo()函数中分配给'i'的值是什么,不仅打印130.
我在以正确的方式定义我在代码中使用的常量时遇到了一些麻烦.虽然我阅读了优秀的帖子Jonathan Leffler,但我如何使用extern在源文件之间共享变量?,我似乎误解了一些东西.这是设置:
/* constants.h */
extern int NUM_PARTICLES;
extern int LIGHTSPEED;
Run Code Online (Sandbox Code Playgroud)
#include "constants.h"
int NUM_PARTICLES=104;
Run Code Online (Sandbox Code Playgroud)
在random.h或
#include "constants.h"
int LIGHTSPEED=104;
Run Code Online (Sandbox Code Playgroud)
在main.c,分别.NUM_PARTICLES用于main.c中
30: double ghosts[NUM_PARTICLES][4];
31: double output[NUM_PARTICLES][3];
Run Code Online (Sandbox Code Playgroud)
虽然这件事有效但我收到以下警告,
main.c: In function ‘int main()’:
main.c:30:32: warning: ISO C++ forbids variable length array ‘ghosts’ [-Wvla]
main.c:31:32: warning: ISO C++ forbids variable length array ‘output’ [-Wvla]
Run Code Online (Sandbox Code Playgroud)
这很奇怪,因为在我看来,我确实给数组一个在编译时已知的常数值.(通常这些数组长度错误会导致一些段错误,在这种情况下它们不会.)任何想法?
我使用了一个二维数组的字符,应该由C中的多个函数编写和读取.
这是我的阵列:
static char array[3][6];
Run Code Online (Sandbox Code Playgroud)
让我们说我有一个函数'Function()'来修改这个数组.如果函数在main中定义没有问题(数组被正确写入然后读取),但如果我想将我的函数放在另一个文件中,那么数组是正确写入的,但是当我在main中返回时是神奇地空了!这是我的代码.
main.c中
#include "support.h"
int main(int argc, char *argv[])
{
Function();
unsigned i, j;
for(i = 0; i < 3; i++)
{
for(j = 0; j < 6; j++)
printf("[%c]", array[i][j]);
printf("\n");
}
system("PAUSE");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
support.h
static char array[3][6];
Run Code Online (Sandbox Code Playgroud)
support.c
void Function()
{
char hello[6];
hello[0] = 'H';
hello[1] = 'E';
hello[2] = 'L';
hello[3] = 'L';
hello[4] = 'O';
hello[5] = '\0';
strcpy(array[0], hello);
}
Run Code Online (Sandbox Code Playgroud)
没有编译错误也没有运行时错误.再一次,如果我尝试移动main.c中的所有内容都可以工作,如果我分成两个文件则不然(数组一旦从Function()返回就会正确,然后它被释放),怎么样有可能吗?
在file1上我将变量args定义为:
CLA args;
Run Code Online (Sandbox Code Playgroud)
在file2上我已将其声明为:
extern CLA* args;
Run Code Online (Sandbox Code Playgroud)
该程序将使用gcc和clang编译而没有错误,并且valgrind上也不会出现任何错误.但是有一个问题:在file2上,函数fprintf(args-> output,...)不会打印任何东西.为什么没有发出错误?
我发现我可以在不使用extern的情况下达到预期的效果(尽管我同意它会给读者一些关于变量的暗示).在某些情况下,使用extern会产生不希望的结果.
xyz.h
int i;
Run Code Online (Sandbox Code Playgroud)
在file1.c
#include "xyz.h"
....
i=10;
....
Run Code Online (Sandbox Code Playgroud)
file2.c中
#include "xyz.h"
main()
{
printf("i=%d\n",i);
}
Run Code Online (Sandbox Code Playgroud)
当然,这是一个大项目,为了简单的理解而将其分解.使用extern关键字,我无法获得理想的结果.实际上,我通过"extern"方法得到变量i的链接器错误.
代码采用"extern"方法,
在file1.c
int i;
main()
{
i=10;
}
Run Code Online (Sandbox Code Playgroud)
file2.c中
extern int i;
foo()
{
printf("i=%d\n",i);
}
Run Code Online (Sandbox Code Playgroud)
这给链接器错误.我只是想知道它为什么在第一种情况下工作,以及在没有使用关键字"extern"的情况下我们无法做到的实际情况.谢谢.
下面是一个代码段,
int var1;
extern int var2;
Run Code Online (Sandbox Code Playgroud)
这是一个多重选择。答案是,第一条语句声明并定义了var1,而第二条语句仅声明了var2。但是我认为应该是“两个语句都只声明变量,没有定义变量”。哪一个是正确的?