extern

How to declare my very own CGRectZero like constant?

北慕城南 提交于 2019-12-04 07:50:15
问题 This is a newbie C/Objective-C question :-) Let say I want a CGRectOne and a CGRectTwo constants. How can I declare that? Thanks, Jérémy 回答1: The other answers are fine - in some cases -. A) declaring it static will emit a copy per translation. That is fine if it is visible to exactly one translation (i.e. its definition is in your .m/ .c file). Otherwise, you end up with copies in every translation which includes/imports the header with the static definition. This can result in an inflated

Are static class variables the same as extern variables, only with class scope?

匆匆过客 提交于 2019-12-04 05:58:04
It seems to me that a static class variable is identical to an extern variable, because you only declare it in the static int x / extern int x statement, and actually define it elsewhere (usually in a .cpp file) static class variable // .h file class Foo { static int x ; } ; // .cpp file int MyClass::x = 0 ; Extern variables: // .h file extern int y; // .cpp file int y = 1; In both cases the variable is declared once somewhere, and defined in a file that will not be included more than once in the compilation (else linker error) Yes, both have static storage duration and external linkage; they

C语言存储类别和链接

拈花ヽ惹草 提交于 2019-12-04 05:30:40
目录 C语言存储类别和链接 存储类别 存储期 五种存储类别 存储类别和函数 分配内存malloc()和free() C语言存储类别和链接 ​ 最近详细的复习C语言,看到存储类别的时候总感觉一些概念模糊不清,现在认真的梳理一下。C语言的优势之一能够让程序员恰到好处的控制程序,可以通过C语言的内存管理系统指定变量的作用域和生存周期,实现对程序的控制。 存储类别 基本概念 对象 :在C语言中所有的数据都会被存储到内存中,被存储的值会占用一定的物理内存,这样的一块内存被称为 对象 ,它可以储存一个或者多个值,在储存适当的值时一定具有相应的大小。(C语言对象不同于面向对象语言的对象) 标识符 :程序需要一种方法来访问对象,这就需要声明变量来实现,例如: int identifier = 1 ,在这里 identifier 就是一个标识符,标识符是一个名称并遵循变量的命名规则。所以在本例中 identifier 即是C程序指定硬件内存中的对象的方式并提供了存储的值的大小“1”。在其它的情况中 int * pt 、 int arr[10] ,pt就是一个标志符,它指定了储存地址的变量,但是表达式*p不是一个标志符,因为它不是一个名称。 arr 的声明创建了一个可容纳10个 int 类型元素的对象,该数组的每一个元素也是一个对象。 作用域 :描述程序中可访问标识符的区域

Difference extern“C” vs extern

随声附和 提交于 2019-12-04 05:20:21
Is there a difference whether I use the extern "C" specifier for the entire header, or specify extern for every function? As far as I know, there is none, since only functions and variables can be linked externally, so when I use the extern specifier before every function prototype and extern variable, I have no need to use the global extern "C" declaration!? Example A: #ifdef __cplusplus extern "C" { #endif void whatever(void); #endif Example B: extern void whatever(void); Dmitry Poroh extern "C" means to call C-library function from C++ code. What is the difference? A long, long time ago C

__attribute__机制介绍

邮差的信 提交于 2019-12-04 04:54:05
1. __attribute__ GNU C 的一大特色(却不被初学者所知)就是 __attribute__ 机制。 __attribute__ 可以设置函数属性 (Function Attribute) 、变量属性 (Variable Attribute) 和类型属性 (Type Attribute) __attribute__ 前后都有两个下划线,并且后面会紧跟一对原括弧,括弧里面是相应的 __attribute__ 参数 __attribute__ 语法格式为: __attribute__ ( ( attribute-list ) ) 函数属性( Function Attribute ),函数属性可以帮助开发者把一些特性添加到函数声明中,从而可以使编译器在错误检查方面的功能更强大。 __attribute__ 机制也很容易同非 GNU 应用程序做到兼容。 GNU CC 需要使用 –Wall ,这是控制警告信息的一个很好的方式。下面介绍几个常见的属性参数。 2. format 该属性可以使编译器检查函数声明和函数实际调用参数之间的格式化字符串是否匹配 。它可以给被声明的函数加上类似 printf 或者 scanf 的特征,该功能十分有用,尤其是处理一些很难发现的 bug 。 format 的语法格式为: format ( archetype, string-index,

Does C provide a way to declare an extern variable as 'read-only', but define it as writeable?

左心房为你撑大大i 提交于 2019-12-04 04:21:30
问题 I'm developing a hardware abstraction library for an embedded product using GCC C. Within the library there is a variable that should be read-only to the application that links the library, but can be modified from within the compilation unit that defines it. Is there a standard, acceptable way to declare the integer (in the library header file) that will allow the application to read the value in the variable, but tell the compiler to generate an error if any attempt is made to generate code

extern declaration and function definition both in the same file

核能气质少年 提交于 2019-12-04 02:51:39
I was just browsing through gcc source files. In gcc.c , I found something like extern int main (int, char **); int main (int argc, char **argv) { Now my doubt is extern is to tell the compiler that the particular function is not in this file but will be found somewhere else in the project. But here, definition of main is immediately after the extern declaration. What purpose is the extern declaration serving then? It seems like, in this specific example, extern seems to be behaving like export that we use in assembly, wherin we export a particular symbol outside of the module Any ideas? You

C++ global extern “C” friend can't reach private member on namespaced class

坚强是说给别人听的谎言 提交于 2019-12-04 02:06:59
Please consider the code: #include <iostream> using namespace std; extern "C" void foo( void ); namespace A { template< int No > class Bar { private: friend void ::foo( void ); static void private_func( int n ); }; template< int No > void Bar< No >::private_func( int n ) { cout << "A:Bar< " << No << ">::private_func( " << n << " )" << endl; } } extern "C" void foo( void ) { A::Bar< 0 >::private_func( 1 ); } int main( ) { cout << " ---- " << endl; foo( ); } G++ gives: > g++ -Wall -o extern_c extern_c.cpp extern_c.cpp: In function ‘void foo()’: extern_c.cpp:20:7: error: ‘static void A::Bar<No>:

Redefinition; different basic types (typedef struct)

时光毁灭记忆、已成空白 提交于 2019-12-04 02:02:11
I'm having a bit of trouble trying to get structs to work properly when they are defined in different files. From as far as I can tell, the error is telling me that the struct is being defined two different times. I believe that perhaps I may need to use extern somewhere? I've tried experimenting and looking for help on Google, but to no avail. Any help at all would be most appreciated, thank you. All four of my files are below. FILE: Foo.h typedef struct { int number; } my_struct; // Redefinition; different basic types FILE: Foo.c #include "Foo.h" #include "Bar.h" #include <stdio.h> my_struct

No linkage at block scope?

ぃ、小莉子 提交于 2019-12-04 01:55:32
Do all variables declared in a block have 'no linkage'? For example: 1: If I declare a static variable: void foo() { static int i; } Would it have an internal linkage or no linkage? If no linkage, then why make it static? 2: What happens if I use extern? /*global scope*/ static int i; void foo() { extern int i; } In this case, what will be the linkage of i ? Indeed, 'no linkage' at function scope. The goal is lifetime management: the static has the lifetime of a global static, while it has the visibility (scope) of a local. Note In C++ you can also declare statics ('globals') without linkage