extern

VS2019静态/动态库制作 20199321

大兔子大兔子 提交于 2019-12-04 17:37:48
动态库 #ifndef PCH_H #define PCH_H #include "framework.h" #endif //PCH_H #ifdef IMPORT_DLL #else #define IMPORT_DLL extern "C" _declspec(dllimport) #endif IMPORT_DLL int add(int a, int b); IMPORT_DLL int minus(int a, int b); IMPORT_DLL int multiply(int a, int b); IMPORT_DLL double divide(int a, int b); // dllmain.cpp : 定义 DLL 应用程序的入口点。 #include "pch.h" int add(int a, int b) { return a + b; } int minus(int a, int b) { return a - b; } int multiply(int a, int b) { return a * b; } double divide(int a, int b) { double m = (double)a / b; return m; } // test20199321.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。

Can a variable be redeclared as auto that deduced to the same type? [duplicate]

六眼飞鱼酱① 提交于 2019-12-04 17:24:37
问题 This question already has answers here : Does a declaration using “auto” match an extern declaration that uses a concrete type specifier? (3 answers) Closed 3 years ago . Is the following allowed by the standard? #include <iostream> extern int a; auto a = 3; int main(int, char**) { std::cout << a << std::endl; return 0; } clang accepts the code. g++ complains for conflicting declaration. 回答1: Its not much clear to me from the standard, but then, there is this written section 7.1.6.4 auto

template External Linkage ?can anyone Explain this?

≡放荡痞女 提交于 2019-12-04 12:31:33
问题 A template name has linkage (3.5). A non-member function template can have internal linkage; any other template name shall have external linkage. Entities generated from a template with internal linkage are distinct from all entities generated in other translation units. I know about external linkage using the keyword extern "C" EX : extern "C" { template<class T> class X { }; } but they gave template shall not have a C linkage what actually meant for the above statement? can any one explain

How does extern work in namespaces?

女生的网名这么多〃 提交于 2019-12-04 12:17:26
问题 I'm running a simple program similar to what I found here. It's meant to reduce code bloat when including constants in multiple files. It does this by using const global variables within a namespace with their respective extern forward declarations. globals.h #ifndef GLOBALS_H_ #define GLOBALS_H_ namespace Constants { // forward declarations only extern const double pi; extern const double avogadro; extern const double my_gravity; } #endif globals.cpp namespace Constants { // actual global

Linux内核定时器

≡放荡痞女 提交于 2019-12-04 12:00:40
1、前言 Linux内核中的定时器是一个很常用的功能,某些需要周期性处理的工作都需要用到定时器。在Linux内核中,使用定时器功能比较简单,需要提供定时器的超时时间和超时后需要执行的处理函数。 2、API接口 在Linux内核中使用全局变量jiffies来记录系统从启动以来的系统节拍数,当系统内核启动的时候,会将该jiffies初始化为0,该定义在文件kernel/include/linux/jiffies.h文件中,如下: extern u64 __jiffy_data jiffies_64; extern unsigned long volatile __jiffy_data jiffies; #if (BITS_PER_LONG < 64) u64 get_jiffies_64(void); #else static inline u64 get_jiffies_64(void) { return (u64)jiffies; } #endif 在上面的代码中,jiffies_64与jiffies变量类似,jiffies_64用于64的系统,而jiffies用于32位系统,Linux内核使用HZ表示每秒的节拍数,使用jiffies/HZ可以获得系统已经运行的时间,单位为秒。 /* time_is_before_jiffies(a) return true if a is

C++ best way to define cross-file constants

情到浓时终转凉″ 提交于 2019-12-04 09:23:23
问题 I am working on a game and have an interesting question. I have some game-wide constant values that I want to implement in one file. Right now I have something like this: constants.cpp extern const int BEGINNING_HEALTH = 10; extern const int BEGINNING_MANA = 5; constants.hpp extern const int BEGINNING_HEALTH; extern const int BEGINNING_MANA; And then files just #include "constants.hpp" This was working great, until I needed to use one of the constants as a template parameter, because

Global variables in Objective-C - difference in extern and top of .m file declaration

空扰寡人 提交于 2019-12-04 09:23:21
问题 I know you can define a global variable in Objective-C by using "extern", but I just realized that the variables I had declared at the top of my .m file before my first method were also accidentally global (and that was causing some problems). I moved them into the @interface part of my header file, which I think correctly declares them as only existing within the class, which has solved some of my problems, but I am still a bit confused. What is the difference in declaring a variable as

Why doesn't this “undefined extern variable” result in a linker error in C++17?

对着背影说爱祢 提交于 2019-12-04 08:47:57
问题 I have compiled and ran the following program in a C++17 compiler (Coliru). In the program, I declared an extern variable, but did not define it. However, the compiler doesn't give a linker error . #include <iostream> extern int i; // Only declaration int func() { if constexpr (true) return 0; else if (i) return i; else return -1; } int main() { int ret = func(); std::cout<<"Ret : "<<ret<<std::endl; } Why doesn't the compiler give a linker error? 回答1: Because the variable isn't odr-used. You

extern keyword usage

谁都会走 提交于 2019-12-04 08:17:05
问题 I have three programs in which I am using extern keyword. I am not able to understand the result. Below are three examples: Example 1: I was expecting that below code will give compilation error that multiple declaration of k . But it works fine? int k; //works fine extern int k = 10; void main() { cout<<k<<endl; getchar(); } Example 2: When I am trying to initialize "k" in above example compiler gives error. Why? int k = 20; //error extern int k = 10; void main() { cout<<k<<endl; getchar();

“extern const” vs “extern” only

房东的猫 提交于 2019-12-04 08:06:45
问题 I've seen 2 ways of creating global variables, what's the difference, and when do you use each? //.h extern NSString * const MyConstant; //.m NSString * const MyConstant = @"MyConstant"; and //.h extern NSString *MyConstant; //.m NSString *MyConstant = @"MyConstant"; 回答1: the former is ideal for constants because the string it points to cannot be changed: //.h extern NSString * const MyConstant; //.m NSString * const MyConstant = @"MyConstant"; ... MyConstant = @"Bad Stuff"; // << YAY!