在Unix中,我有三个主要文件.其中一个是图书馆,另一个是程序.
MyLib.c并且MyLib.h是图书馆.main.c 是程序. 在MyLib.h我有一个声明(extern int Variable;).当我尝试使用Variable时main.c我不能.当然,我已经包含MyLib.h在MyLib.c在main.c,我也将它们链接.无论如何,该变量未被识别main.c.
如何在链接程序时获取变量?
什么是全局变量的默认存储类?
我在网上搜索时发现,有些网站说它是static.但是,静态意味着内部链接,并且变量在文件范围之外不可用,即它不应该对其他目标文件可用.但是,仍然可以使用声明等方式访问其他文件extern int i.
而且,如果我明确提到static全局变量,那么它在文件范围之外是不可用的.
那么,全局变量的正确默认存储类是什么?
只要我有一个包含声明为void g(void (*callback)());的函数的C库以下代码是优雅但非法的:
struct A
{
// error C2159: more than one storage class specified (VC++ Nov 2012 CTP)
static extern "C" void callback()
{}
};
g(A::callback);
Run Code Online (Sandbox Code Playgroud)
为什么C++ 11不支持这个?
请原谅我,如果这听起来是一个多次被问过的问题,但我向你保证这有点不同.
我使用Codeblocks进行C编程,最近我开始想知道为什么有人在C中使用头文件.我理解它用于声明和/或定义变量结构.但这是我尝试过的东西,现在我很困惑.
我有一个名为的头文件
test1.h
#ifndef TEST1_H_INCLUDED
#define TEST1_H_INCLUDED
static int testvar = 233;
extern int one;
extern void show();
#endif // TEST1_H_INCLUDED
Run Code Online (Sandbox Code Playgroud)
和另外两个文件
headertest.c
#include"test1.h"
#include<stdio.h>
int one = 232;
int main()
{
testvar = 12;
printf("The address of one is %p and value is %d",&testvar,testvar);
printf("value of one is %d",one);
show();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
headertest2.c
#include "test1.h"
#include<stdio.h>
extern int one;
extern void show()
{
//extern int one;
testvar = 34;
printf("\nThe address of one is %p and value …Run Code Online (Sandbox Code Playgroud) 我正在尝试用C++开发一个动态库,由用IDL(交互式数据语言)编写的现有程序调用.我知道我需要使用extern"C"来禁用名称修改,以便IDL可以调用它需要的函数(其余的调用机制非常简单).
但是,我总是对使用我不完全理解的语言的功能犹豫不决,所以我的问题是:如果有的话,我会通过恢复到C链接而丢失C++的哪些功能?我认为命名空间是一个显而易见的,但它是否完全禁用了C++的所有其他优点?我还可以使用C++ STL,以及我所依赖的所有各种语言功能(特别是C++ 11)吗?还是我坚持用C编码?
我猜我只会使用UIKIT_EXTERN,如果我的项目中有可能使用该变量的C++代码.
如果是这种情况,用UIKIT_EXTERN声明所有外部可用的常量是否安全?
为什么我不再看到这个?
维基说:
的
extern关键字的意思是"没有限定声明".换句话说,它是一种显式声明变量或强制声明而无需定义的方法.也可以明确定义变量,即强制定义.这是通过为变量分配初始化值来完成的.
这意味着,初始化变量的extern声明用作该变量的定义.所以,
/* Just for testing purpose only */
#include <stdio.h>
extern int y = 0;
int main(){
printf("%d\n", y);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
应该是有效的(在C++ 11中编译).但是当使用-Wall -Wextra -pedantic -std=c99GCC 4.7.2中的选项进行编译时,会产生警告:
[Warning] 'y' initialized and declared 'extern' [enabled by default]
Run Code Online (Sandbox Code Playgroud)
哪个不应该.据我所知,
extern int y = 0;
Run Code Online (Sandbox Code Playgroud)
实际上是一样的
int i = 0;
Run Code Online (Sandbox Code Playgroud)
这里出了什么问题?
我试图在extern template我的项目中添加一些内容,以加快构建时间并减少构建期间磁盘上的占用空间.
我通过列出我经常使用的专业化来做到这一点,例如
extern template class std::vector<std::string>;
extern template class std::vector<unsigned int>;
extern template class std::vector<uint32_t>;
extern template class std::vector<size_t>;
Run Code Online (Sandbox Code Playgroud)
问题是,unsigned int 是 size_t的,也许,和uint32_t 是 unsigned int的,也许.因此,根据构建目标,extern实际实例化特化的列表的"非"变体的编译失败:
template class std::vector<std::string>;
template class std::vector<unsigned int>;
template class std::vector<uint32_t>;
template class std::vector<size_t>;
Run Code Online (Sandbox Code Playgroud)
错误是这样的(行号和列号不准确):
templates-impl.h:15:36: error: duplicate explicit instantiation of ‘class std::vector<long unsigned int>’ [-fpermissive]
template class std::vector<size_t>;
Run Code Online (Sandbox Code Playgroud)
我的目标是能够列出我使用的类型,因为我使用它们,最小的麻烦,不需要为不同的目标硬编码列表的变体.
我想在C++ 14中我可以用if constexpr一些和一些来解决这个问题std::is_same,至少如果在命名空间范围内允许的话.
我怎么能在C++ 11中做到这一点?
尝试class与extern "C"函数结交朋友,此代码有效:
#include <iostream>
extern "C" {
void foo();
}
namespace {
struct bar {
// without :: this refuses to compile
friend void ::foo();
bar() : v(666) {}
private:
int v;
} inst;
}
int main() {
foo();
}
extern "C" {
void foo() {
std::cout << inst.v << std::endl;
}
}
Run Code Online (Sandbox Code Playgroud)
但是我很惊讶地发现,使用g ++ 4.6.1和4.4.4我必须明确写入::,friend void ::foo();否则友谊不起作用.这::只是在需要的时候才需要extern "C".
extern "C"而不是没有它?名称查找规则的变化如何使其成为必要?我很难过.可能有一些我无法找到的规则.
static关键字将全局变量的范围限制为该转换单元.如果我 static int x在.h文件中使用并且每隔一个文件包含该.h文件,它们是否都属于同一个翻译单元?那么,到处都不会出现x吗?那么静电的作用是什么?
另外,有没有使用static const int x,其中x是一个全局变量?默认情况下,并非所有const全局变量都是静态的 并且const变量的范围仅限于TU,即使它被限制在文件中的for循环中?
extern ×10
c ×6
c++ ×6
c++11 ×2
static ×2
c99 ×1
declaration ×1
default ×1
friend ×1
gcc-warning ×1
global ×1
initializer ×1
linkage ×1
objective-c ×1
storage ×1
templates ×1
visibility ×1