extern

C: What is the use of 'extern' in header files?

非 Y 不嫁゛ 提交于 2020-01-29 05:59:25
问题 Pardon me if this sounds a question that has been asked many times but I assure you this is a little different. I use Codeblocks for C programming and lately I have started to wonder why would someone use a header file in C. I understand it is used for declaring and/or defining variables structures. But here is something i tried and now I am confused. I have a header file named test1.h #ifndef TEST1_H_INCLUDED #define TEST1_H_INCLUDED static int testvar = 233; extern int one; extern void show

Thinking in C++前几章笔记(二)

≡放荡痞女 提交于 2020-01-28 15:52:08
15、动态计算数组的大小: int c[]={1,2,3,4}; for(int i=0;i<sizeof c/sizeof *c;i++) c[i]++; 16、type-safe linkage:Because all functions must be declared before they are used in C++, the opportunity for this problem to pop up is greatly diminished. The C++ compiler refuses to declare a function automatically for you, so it’s likely that you will include the appropriate header file. However, if for some reason you still manage to misdeclare a function, either by declaring by hand or including the wrong header file (perhaps one that is out of date), the name decoration provides a safety net that is often

c++(3)----变量的声明和定义

可紊 提交于 2020-01-28 13:52:00
分离式编译(separate compilation):   允许将程序分割为若干个文件,每个文件可独立编译。 声明: 使得名字为程序所知。 定义: 负责创建与名字关联的实体。    变量只能被定义一次,但可以被声明多次。 如果要在多个文件中使用同一个变量,就必须将声明和定义分离。此时,变量的定义必须出现在且只能出现在一个文件中,而其他用到该变量的文件必须对其进行声明。 extern int i ; // 声明i 而非定义i int j ; // 声明并定义 int c=3; // 声明、定义、初始化 extern int k=3; // 定义,此时extern 的作用被抵消 // 在函数体内部,如果试图初始化一个由extern关键字标记的变量,将引发错误 来源: https://www.cnblogs.com/feihu-h/p/12237877.html

extern"C"详解

与世无争的帅哥 提交于 2020-01-25 16:50:33
我们一般在 c++ 中使用 c 语言的库时,都会引用 c 库的头文件。例如 string.h , c 库头文件中我们经常会看到这样的代码。 #ifdef __cplusplus extern "C"{ #endif ...... #ifdef __cplusplus } #endif __cplusplus 定义着 c++ 编译器的版本,如果没有定义则表示当前编译器不是 c++ 。 上述代码的意思是如果编译代码的是 c++ 编译器,那么就将中间包含的内容以 c 语言的语法编译,为什么要这么做呢,下面我举个例子 add.h #ifndef _ADD_H #define _ADD_H int add(int a, int b); #endif // _ADD_H add.c int add(int a, int b) { return a + b; } main.cpp #include <iostream> #include "add.h" using namespace std; int main(int argc, char *argv[]) { cout << add(3, 5) << endl; return 0; } 执行编译 g++ main.cpp add.c -o main 这样的编译是没问题的,因为我们用 g++ 编译了所有文件,都按照 c++ 的编译规则。

Platform Invoke

此生再无相见时 提交于 2020-01-24 04:44:37
Finally ,找到工具来参考转换。 Source: https://github.com/jaredpar/pinvoke Tool: https://archive.codeplex.com/?p=clrinterop 书籍 参考: NET and COM: The Complete Interoperability Guide https://docs.microsoft.com/en-us/cpp/dotnet/calling-native-functions-from-managed-code 这篇新文章里面有详细的解释,包括数据类型的转换。 还可以参考这个文档: file:///C:/Users/wiyan/AppData/Local/Microsoft/Windows/INetCache/IE/Q0DGUWJP/traks_computerscience_2016.pdf Marshaling Arguments With PInvoke , no marshaling is needed between managed and C++ native primitive types with the same form. For example, no marshaling is required between Int32 and int, or between

C++11 外部模板

隐身守侯 提交于 2020-01-24 03:00:54
【1】引入外部模板为了解决什么问题? “外部模板”是C++11中一个关于模板性能上的改进。实际上,“外部”(extern)这个概念早在C的时候已经就有了。 常见的情况,在一个文件a.c中定义了一个变量int i,而在另外一个文件b.c中想使用它,这个时候就会在没有定义变量i的b.c文件中做一个外部变量的声明。比如: // 声明在b.c文件中 extern int i; 这样做的好处是,在分别编译了a.c和b.c之后,其生成的目标文件a.o和b.o中只有i这个符号(可简单理解为变量)的一份定义。 具体地,a.o中的i是实在存在于a.o目标文件的数据区中的数据,而在b.o中,只是记录了i符号会引用其他目标文件中数据区中的名为i的数据。 这样一来,在链接器(通常由编译器代为调用)将a.o和b.o链接成单个可执行文件(或者库文件)c的时候,c文件的数据区也只会有一个i的数据(供a.c和b.c的代码共享)。 而如果b.c中我们声明int i的时候不加上extern的话,那么i就会实实在在地既存在于a.o的数据区中,也存在于b.o的数据区中。 那么链接器在链接a.o和b.o的时候,就会报告错误,因为无法决定相同的符号是否需要合并。 而对于函数模板来说,现在我们遇到的几乎是一模一样的问题。 不同的是,发生问题的不是变量(数据),而是函数(代码)。这样的困境是由于模板的实例化带来的。

vs2015开发ffmpeg出现error LNK2019: 无法解析的外部符号

跟風遠走 提交于 2020-01-20 19:39:02
用vs2015开发ffmpeg时,头文件,lib文件都正确设置,但是在编译时候提示error LNK2019: 无法解析的外部符号,经研究发现ffmpeg的库都是c语言的,而我编译器是c++,故需要在#include头文件时需要加上关键字 extern "C" extern "C" { #include "libavutil/frame.h" #include "libavutil/mem.h" #include "libavformat/avformat.h" #include "libavcodec/avcodec.h" } 即可编译成功! 来源: CSDN 作者: songzhaorong 链接: https://blog.csdn.net/songzhaorong/article/details/104053255

不依赖插件 给 Unity 项目接入 Lua

三世轮回 提交于 2020-01-20 18:45:32
之前在公司给项目接入过 xLua .接入过程非常傻瓜. 又了解到 Unity 由于历史原因,有各种各样的 lua 接入插件。 slua,xlua,tolua 等等层出不穷。 如果是为了直接在 Unity 项目里使用 Lua,使用现成的插件肯定是最好的选择。如果是为了学习,就需要自己亲手实践一番 之前并不了解 unity 接入 lua 的原理 。 最近通过公司的项目,查阅看官方文档,了解到 Unity 能够使用 C# 代码调用 C++ 写的动态链接库,由此试验了一下从0开始接入 Lua 过程记录如下 #Unity 集成 Lua 文档参考Unity 官方文档 Working in Unity -> Advanced Development -> Plug-ins -> NataivePlug-ins 章节 https://docs.unity3d.com/Manual/NativePlugins.html [file:///D:/UnityDocumentation_2019/en/Manual/PluginsForDesktop.html](file:///D:/UnityDocumentation_2019/en/Manual/PluginsForDesktop.html) 参考示例 https://github.com/Unity-Technologies

Javascript and WebGL, external scripts

99封情书 提交于 2020-01-19 21:34:10
问题 Just curious; How do I place my webgl shaders, in an external file? Currently I'm having; <script id="shader-fs" type="x-shader/x-fragment"> #ifdef GL_ES precision highp float; #endif void main(void) { gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0); } </script> <script id="shader-vs" type="x-shader/x-vertex"> attribute vec3 aVertexPosition; uniform mat4 uMVMatrix; uniform mat4 uPMatrix; void main(void) { gl_Position = uPMatrix * uMVMatrix * vec4(aVertexPosition, 1.0); } </script> In my html header,

hook

坚强是说给别人听的谎言 提交于 2020-01-18 08:22:03
本脚本作用 拦截原有按键 修改成另一个按键 环境win10 打包参数 IL2CPP 2018.2.0f2 Windows X86 .NET 4.x 放到Unity里注意当unity失去焦点时 应取消键盘钩子 否则在程序外也会生效 using UnityEngine; using System; using System.Diagnostics; using System.Runtime.InteropServices; public class MonoPInvokeCallbackAttribute : Attribute { public MonoPInvokeCallbackAttribute() { } } public class WindowsKeyTool : MonoBehaviour { private const int WH_KEYBOARD_LL = 13; private const int WM_KEYDOWN = 0x0100; private static LowLevelKeyboardProc _proc = HookCallback; private static int _hookID; public static bool KeyOn = true; void Start() { UnhookWindowsHookEx((IntPtr)